John Davidson

php - Keeping items in cart for logged in users on their log off and cart of user could not be seen by others on logoff

0 comments
Message:


I have a WordPress website and we want that if someone browses the website and adds some items to the cart then log in while checkout or vice versa. In case the user logs off or closes the browser then those items should remain in the cart for that particular login only until the order has been completed or has been removed by the customer from the cart.
We want that if the user logs off from the website then if someone else sits on his pc and browses the same website then the other person could not see what the user had in his cart. It can only be viewable once the user logs in back.
Could you please suggest the best way to achieve this target?
Thanks!



My Answer:
One way to achieve this is by using session variables in PHP. When a user adds items to their cart, you can store the cart items in a session variable. This session variable will be unique to each user and will persist even if the user logs off or closes the browser.

When the user logs in, you can retrieve the cart items from the session variable and display them in the cart. This way, the items will remain in the cart for that particular user until they complete the order or remove the items from the cart.

To ensure that the cart items are not visible to other users, you can store the session variable in a database table along with the user's ID. This way, the cart items will be associated with the user's account and will not be accessible to other users.

Additionally, you can also use cookies to store the cart items for a longer period of time, even if the user logs off or closes the browser. This way, the user can come back to the website and still see the items in their cart.

Overall, using session variables and cookies in combination with user authentication can help you achieve the desired functionality of keeping items in the cart for logged in users and ensuring that the cart is not visible to other users.

Rate this post

5 of 5 based on 3876 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us