Forums / Customization from the Admin / How to delete products from shopping cart when user session ends

How to delete products from shopping cart when user session ends

Started by rohitsax on 4 Nov 2007, 7:39 AM · Views: 11,859

Results 1 to 12 of 12
4 Nov 2007, 7:39 AM
#1
rohitsax avatar

rohitsax

New Zenner

Join Date:
May 2006
Posts:
33
Plugin Contributions:
0

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!

5 Nov 2007, 3:37 PM
#2
ajeh avatar

ajeh

Oba-san

Join Date:
Sep 2003
Location:
Ohio
Posts:
62,757
Plugin Contributions:
1

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:

        // 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:

        // 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);

Linda McGrath
If you have to think ... you haven't been zenned ...

**Did YOU buy the Zen Cart Team a cup of coffee and a donut today? Just click here to support the Zen Cart Team!!**

Are you using the latest? Perhaps you've a problem that's fixed in the latest version: [Upgrade today!]
Officially PayPal-Certified! Just click here

Try our Zen Cart Recommended Services - Hosting, Payment and more ...
Signup for our Announcements Forums to stay up to date on important changes and updates!

8 Nov 2007, 6:41 AM
#3
rohitsax avatar

rohitsax

New Zenner

Join Date:
May 2006
Posts:
33
Plugin Contributions:
0

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!!

8 Nov 2007, 7:44 AM
#4
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,511
Plugin Contributions:
176

Re: How to delete products from shopping cart when user session ends

Good point.

Changing it like this should work:

        // restore cart contents
        $_SESSION['cart']->restore_contents();

becomes:

        $current_cart = $_SESSION['cart']->contents;
        $_SESSION['cart']->reset(true);
        $_SESSION['cart']->contents = $current_cart;
        $_SESSION['cart']->calculate();

.
Zen Cart - putting the dream of business ownership within reach of anyone!
Donate to: DrByte directly or to the Zen Cart team as a whole

Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

19 Nov 2007, 5:19 AM
#5
rohitsax avatar

rohitsax

New Zenner

Join Date:
May 2006
Posts:
33
Plugin Contributions:
0

Re: How to delete products from shopping cart when user session ends

Thanks very much! This has solved it!

2 Mar 2011, 6:51 AM
#6
trees2wood avatar

trees2wood

Zen Follower

Join Date:
Mar 2009
Posts:
114
Plugin Contributions:
0

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

All of my wood is cut from recycled trees

15 Dec 2011, 11:45 PM
#7
neo2810 avatar

neo2810

New Zenner

Join Date:
Mar 2011
Posts:
9
Plugin Contributions:
0

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.

16 Dec 2011, 1:08 AM
#8
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,511
Plugin Contributions:
176

Re: How to delete products from shopping cart when user session ends

**** edited after gathering more information for testing ****

.
Zen Cart - putting the dream of business ownership within reach of anyone!
Donate to: DrByte directly or to the Zen Cart team as a whole

Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

16 Dec 2011, 4:39 AM
#9
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,511
Plugin Contributions:
176

Re: How to delete products from shopping cart when user session ends

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:```
$current_cart = $_SESSION['cart']->contents;
$_SESSION['cart']->reset(true);
$_SESSION['cart']->contents = $current_cart;
[B] $_SESSION['cart']->cartID = $_SESSION['cart']->generate_cart_id();
[/B] $_SESSION['cart']->calculate();

.
Zen Cart - putting the dream of business ownership within reach of anyone!
Donate to: DrByte directly or to the Zen Cart team as a whole

Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.

17 Jan 2012, 4:48 PM
#10
lalaaaa avatar

lalaaaa

New Zenner

Join Date:
Jan 2012
Posts:
1
Plugin Contributions:
0

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:

22 May 2012, 2:23 AM
#11
tstanley avatar

tstanley

New Zenner

Join Date:
Oct 2008
Posts:
20
Plugin Contributions:
0

Re: How to delete products from shopping cart when user session ends

DrByte:

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:```
$current_cart = $_SESSION['cart']->contents;
$_SESSION['cart']->reset(true);
$_SESSION['cart']->contents = $current_cart;
[B] $_SESSION['cart']->cartID = $_SESSION['cart']->generate_cart_id();
[/B] $_SESSION['cart']->calculate();


I am running v 1.3.9h and a customer called with the login error "Whoops! Sorry, but you are not allowed to perform the action requested" I am getting this error as well with my test account. This appears to be a recent problem. I searched for the above mentioned code and was unable to locate it in header_php.php file. Thanks in advance for the help.
22 May 2012, 3:55 AM
#12
drbyte avatar

drbyte

Sensei

Join Date:
Jan 2004
Posts:
63,511
Plugin Contributions:
176

Re: How to delete products from shopping cart when user session ends

You're posting in a thread discussing how to change normal operation of the store software.
Why are you posting in this discussion? Did you attempt to make a similar change? Or are you just posting here because you're encountering an error message that happened to come up in search results?
What exactly do you mean by "this appears to be a recent problem"? What have you changed in your site "recently"?
Further, please answer all the questions in the Posting Tips section which appears when you click Reply.

.
Zen Cart - putting the dream of business ownership within reach of anyone!
Donate to: DrByte directly or to the Zen Cart team as a whole

Remember: Any code suggestions you see here are merely suggestions. You assume full responsibility for your use of any such suggestions, including any impact ANY alterations you make to your site may have on your PCI compliance.
Furthermore, any advice you see here about PCI matters is merely an opinion, and should not be relied upon as "official". Official PCI information should be obtained from the PCI Security Council directly or from one of their authorized Assessors.