AJAX and Shopping Cart In Sessions
Hello everyone, I am having quite a frustrating issue.
I've managed to develop a little system using AJAX to implement an AJAX shipping estimator tab on the product page, as well as an AJAX add to cart for the product page.
In themselves, these modules work fine. Here is the general skeleton of how the loading flows:
1) The product page finishes loading in a second, and then you are allowed to execute the jQuery operations, such as using AJAX add to cart.
2) At the same time it finishes loading, it then sends an AJAX request to the shipping estimator to load it into the appropriate tab. This is done so that the product page can completely load quickly without being tied down with the cURL requests.
3) After 4-5 seconds the shipping estimator finishes execution and is loaded into the appropriate tab.
So understand that, here is the issue:
1) When the page finishes loading, I can add products to the cart by pressing the add to cart button, the sidebox then reflects the appropriate changes.
2) 4-5 seconds after the page finishes loading, the shipping estimator loads in. Now if I press add to cart, the ones that I added during the load time are gone. So, if my starting quantity in the cart was 0, and I added 2 during the loading time, now if I add 2 more to the cart, my final quantity will be 2 again.
3) If I change the address in the AJAX shipping estimator, it then takes another 4-5 seconds to load in the new results. During that time, if I add the product to the cart, it will then be reset after the loading completes, like above.
I have tried numerous methods of debugging this, and the only thing that I've managed to find out with certainty is that the server-side script being called by AJAX basically reverts the cart just by calling application_top.php, not by any of my own code.
Does anyone have any idea where I could look in order to solve this problem?
My current hypothesis is that the PHP $_SESSION variable is inherently not live, that is to say that if you change a session variable in Page B while Page A is loading, and after that change Page A calls that variable, it will still hold the same value as when the session was started on Page A.
The thing is though, even though that's true, I cannot find anywhere in application_top where the $_SESSION['cart'] is being set to anything. Even though the AJAX estimator uses the cart for the cURL requests, it only accesses the object and doesn't really modify it. So after it loads, it shouldn't have modified the cart at all, right? Well, obviously it's doing that somewhere, but I haven't been able to figure out where. The autoload_func isn't doing it, so I've run out of ideas for now.
Thank you all in advance!
User of zencart
Re: AJAX and Shopping Cart In Sessions
Well low and behold I did manage to get it to work, a very simple fix too haha. It always is!
After doing proper research (which should have been done several hours ago), I read here that once one file opens the session, all other files are locked out.
So, the fix was simple to use session_write_close towards the top of the file I was "AJAXing", and then store any of the information I actually need in the $_SESSION variable manually. And it worked fabulously.
User of zencart
By discoverytdi in forum All Other Contributions/Addons
Last Post: 28 Apr 2012, 01:29 AM
By skrillnet in forum Contribution-Writing Guidelines
Last Post: 27 Feb 2012, 03:33 PM
By magicpants in forum Addon Sideboxes
Last Post: 16 May 2011, 04:43 AM
Content and Graphics Copyright (c) 2003 - 2016 Zen Ventures, LLC - all rights reserved
Zen Cart® is a Registered Trademark of Zen Ventures, LLC