Zen Cart Logo
Forums / Bug Reports / Add to cart button sometimes doesn't work

Add to cart button sometimes doesn't work

Locked

Views: 3,049

Results 1 to 5 of 5
This thread is locked. New replies are disabled.
14 Oct 2009, 11:18 PM
#1
eastwood avatar

eastwood

New Zenner

Join Date:
Oct 2009
Posts:
2
Plugin Contributions:
0

Add to cart button sometimes doesn't work

My problem is, that sometimes if I click on the Add_to_cart button it redirects me to the cart but doesn't add the item to it. This problem can not be reproduced however it happens more often if I press the button really fast (before page load finished). Maybe it happens on slower pc-s more often, but I can reproduce it at least once in 10 add_to_cart actions.
I haven't found the solution in the forum so I've searched it myself and now I write it down, if anybody suffers from the same problem.

So after I click the button the product is added to the shopping_cart in the session and the zen_redirect function in functions_general.php redirects me to the shopping cart. But somehow the session has not enough time to be saved before the call of the redirect function (named header).
If I debug it, I can see that the product is added to the cart before the call of the header, but it is missing from the cart after the session is started on the shopping_cart page.
This session behavior is a known malfunction in php.

I can solve the problem if I put session_regenerate_id(true); before the call of the header function in zen_redirect(). At least it solved the problem for me...

eastwood

2 Nov 2009, 8:49 PM
#2
myshol avatar

myshol

New Zenner

Join Date:
Jul 2006
Location:
Poland
Posts:
31
Plugin Contributions:
0

Re: Add to cart button sometimes doesn't work

I have got a same problem with my zen cart installation. Could You describe with more details what and where do I have to change?

TIA

3 Nov 2009, 1:22 AM
#3
ajeh avatar

ajeh

Oba-san

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

Re: Add to cart button sometimes doesn't work

Gee ... if I hit the add to cart as fast as I can I can get 10 added to the cart before the page changes to the cart ... :eek:

Don't do that ...

If someone is going to sit there and hit a button as fast as they can, what do they expect to happen? :blink:

NOTE: I cannot get that to not add at least 1 to the cart when I hit the add to cart multiple times ...

3 Nov 2009, 11:25 AM
#4
eastwood avatar

eastwood

New Zenner

Join Date:
Oct 2009
Posts:
2
Plugin Contributions:
0

Re: Add to cart button sometimes doesn't work

Don't hit the add_to_cart button multiple times. Just hit it once.
Imagine a fast customer who knows you webshop very well and wants to buy for example 10 different products. So this customer will navigate through your site and drop the products in the cart really fast. He will not wait for finishing the page load as soon as he notice the add_to_cart button he will click it (once). And he will be redirected to the shopping cart always but sometimes the selected product will not be there.

And this behavior is because php sometimes doesn't save the session when it use the header function to redirect. Maybe it has no time for it and maybe itt happens on overloaded servers more often, I don't know. The fact is that I could reproduce this problem both on my pc (php 5.2.3) and on the server (php 5.2.8) where my webshop runs.

All in all it is a known php problem (google for it) and you can solve it by calling the session_regenerate_id() function before the call of the header() function.

More precisely: search for zen_redirect() in ../includes/functions/functions_general.php and make the modification below.

function zen_redirect($url) {
    global $request_type;
    // Are we loading an SSL page?
    if ( (ENABLE_SSL == true) && ($request_type == 'SSL') ) {
      // yes, but a NONSSL url was supplied
      if (substr($url, 0, strlen(HTTP_SERVER . DIR_WS_CATALOG)) == HTTP_SERVER . DIR_WS_CATALOG) {
        // So, change it to SSL, based on site's configuration for SSL
        $url = HTTPS_SERVER . DIR_WS_HTTPS_CATALOG . substr($url, strlen(HTTP_SERVER . DIR_WS_CATALOG));
      }
    }

  // clean up URL before executing it
    while (strstr($url, '&&')) $url = str_replace('&&', '&', $url);
    while (strstr($url, '&&')) $url = str_replace('&&', '&', $url);
    // header locates should not have the & in the address it breaks things
    while (strstr($url, '&')) $url = str_replace('&', '&', $url);

    session_regenerate_id(true);
    header('Location: ' . $url);

    zen_exit();
  }
3 Nov 2009, 2:51 PM
#5
ajeh avatar

ajeh

Oba-san

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

Re: Add to cart button sometimes doesn't work

Thanks for the update ... we will look into this further to see what we can break ... er ... fix ... :smile:

Now where are those smart boys hiding ...