In my hook_init functions I often check what the current page is before loading js or css files and in some cases control actions on module stored session info based on values in the session and the current path.
The module I'm currently working on stores extra information for orders users choose to use Checkout by Amazon to pay for. I haven't spent the time to figure out how to make CbA work as a payment method yet so right now I have to alter the Ubercart checkout form to include their widgets and then hijack the form submission to handle the API calls and build its own review and completion pages.
I store the necessary information in a module specific session variable. If the user reviews their order then goes back out to any other page, I keep the session going, only destroying the order so it can be rebuilt when they go to checkout again.
Now the issue I'm having is that I have two cases where the whole session needs to be reset.
- If the user completes the order then moves to any page other than the order complete page
- If the user goes to any page other than the checkout page from the review page
The second is usually handled because they'll likely hit the shopping cart page to checkout again if they go somewhere else (I'll be adding a tracker to force people to the cart page if they leave the review page sometime soon).
The issue I'm having is that I am doing this with code like the following:
function uc_cba_init(){
if(strpos($_SERVER['REQUEST_URI'], '/amazon/') !== FALSE || (arg(0) == 'cart' && !arg(1))){
if(isset($_SESSION['uc_cba']['order']['complete'])){
// if on order complete page, remove unnecessary session items for the order, including cart products
// otherwise delete the module session
}
}else if(isset($_SESSION['uc_cba']['order']['complete'])){
// remove unnecessary session items, cart products, and module session
}
}
The problem with this is that there are a lot of ajax calls flying around and they tend to trigger the init function which means that when the user reaches the order complete page, the ajax call just before that page clears out the session which destroys the order complete information (no order id given to the user for reference or any other instructions for managing their order).
My question would be, is there a simple way to ensure the init only executes its code if the page it is being executed from is not being called from an AJAX hit?
I can test for the shopping cart page, checkout pages and node pages but that won't be fool proof since it won't catch views, taxonomy and other module generated pages that do not have a source path of node/X.