How to delete products from shopping cart when user session ends
I want all products from the shopping cart deleted when a user logs out or session ends - irrespective of whether they ordered or not.
Is there way to do this?
Thanks!
Re: How to delete products from shopping cart when user session ends
It is easier to do this when the customer logs in as you do not know when the customer logs out ... or more specifically, you know the customer is logged out but you don't know who the customer is as the session dies first ... :unsure:
So, in the login header where you normally restore the cart you would want to empty it for the customer ...
/includes/modules/pages/login/header_php.php
You don't want to just skip the restore as it is getting what is in the database tables so you need to reset it ...
Changing:
PHP Code:
// bof: not require part of contents merge notice
// restore cart contents
$_SESSION['cart']->restore_contents();
// eof: not require part of contents merge notice
to read:
PHP Code:
// bof: not require part of contents merge notice
// restore cart contents
$_SESSION['cart']->restore_contents();
// eof: not require part of contents merge notice
$_SESSION['cart']->reset(true);
Re: How to delete products from shopping cart when user session ends
Thanks... that does seem to reset the existing cart... however all of the 'temporary' visitors cart items are also deleted.
I just want the previous shopping cart items deleted. So when the visitor shops again and adds items, then logs in he gets only the current products he's added and nothing from the previous session.
HELP!!
Re: How to delete products from shopping cart when user session ends
Good point.
Changing it like this should work:
PHP Code:
// restore cart contents
$_SESSION['cart']->restore_contents();
becomes:
PHP Code:
$current_cart = $_SESSION['cart']->contents;
$_SESSION['cart']->reset(true);
$_SESSION['cart']->contents = $current_cart;
$_SESSION['cart']->calculate();
Re: How to delete products from shopping cart when user session ends
Thanks very much! This has solved it!
Re: How to delete products from shopping cart when user session ends
For anybody doing this , when the customer logs out the message says "You have been logged off ....... If you had items in your cart they have been saved , they will be restored when you return"
Well if you made the changes above thats not the case anymore , you'll need to change the message they see by editing this file:
- includes/languages/english/logoff.php
Re: How to delete products from shopping cart when user session ends
WARNING: This script is flawed...
If a user logs in, and then adds products to the cart, all works fine. However, if a user browsing as a guest, adds a prodcut to cart and then logs in when prompted, the script kills the cart session but does NOT empty the cart so the user gets a Whoops error when they click "checkout".
I'd love a working version of this where the cart session is reset on login and the actual cart is emptied.
Re: How to delete products from shopping cart when user session ends
*** edited after gathering more information for testing ***
Re: How to delete products from shopping cart when user session ends
Quote:
Originally Posted by
neo2810
WARNING: This script is flawed...
If a user logs in, and then adds products to the cart, all works fine. However, if a user browsing as a guest, adds a prodcut to cart and then logs in when prompted, the script kills the cart session but does NOT empty the cart so the user gets a Whoops error when they click "checkout".
I'd love a working version of this where the cart session is reset on login and the actual cart is emptied.
You are partly correct. The "script" has no flaws when applied against the version for which it was written.
Between the time that code was posted 4 years ago and now, there have been numerous new releases and feature changes and security precautions added. One of those has to do with ensuring there's been no tampering with the shopping cart. And, since the code literally IS tampering with the shopping cart contents, it's triggering one of those safeties.
The fix is simple. Add another line above the calculate() line, as shown:
Code:
$current_cart = $_SESSION['cart']->contents;
$_SESSION['cart']->reset(true);
$_SESSION['cart']->contents = $current_cart;
$_SESSION['cart']->cartID = $_SESSION['cart']->generate_cart_id();
$_SESSION['cart']->calculate();
Re: How to delete products from shopping cart when user session ends
Hi, I can't seem to get the cart to work.
When I log in and add an item to the cart and log off and log in again, the item is still in the cart...
I've changed the code according to what was stated here.
So my code looks like that now:
// restore cart contents
$current_cart = $_SESSION['cart']->contents;
$_SESSION['cart']->reset(true);
$_SESSION['cart']->contents = $current_cart;
$_SESSION['cart']->cartID = $_SESSION['cart']->generate_cart_id();
$_SESSION['cart']->calculate();
// eof: not require part of contents merge notice
$_SESSION['cart']->reset(true);
What I want is:
The shopping cart to be emptied whenever a user logs in again.
(ie. user 1 logs in and adds stuff to the cart. then logs out. when he logs in again, the cart will be empty.)
Hope you'll help in solving the problem. Thanks! :smile: