1

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.

  1. If the user completes the order then moves to any page other than the order complete page
  2. 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.

1

You can check if you're processing an AJAX request or not in your hook_init() and skip your code if you need it.

function uc_cba_init(){
  if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
    // Is an AJAX request...
  }
  else {
    // Is not an AJAX request...
  }
}

Hope it helps!

  • I ended up adding a check if the page was a node or taxonomy term page to the if I had up there for now. I added your code to a separate module that needs to do the same thing and will likely use it in other places as I move forward with other projects. Thanks! – Jason Gray Sep 17 '13 at 16:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.