1

So I have a custom module that is simply one function that during Commerce checkout goes through the order or cart to create an array saying how many "products" of type 'event','donation' and 'product' there are in the cart.

Now this all works great and I use it to do multiple things such as:

  • Change the title page of the checkout if someone is "donating" rather than "purchasing"
  • Show or hide certain checkout panes depending on whether a 'donation' or 'event' is in the cart

  • Render a checkout progress bar which shows certain checkout pages, again dependent on the product types currently in the cart.

My issue now is that on render of the Checkout Details page I have this one function executing 4 separate time. How would I go about triggering it once for each load of a page and storing the array in a place that can be accessed by multiple modules and template files?

  • Check out this answer. You could cache the array and use the same in all 4 places. – GoodSp33d Oct 29 '12 at 14:31
  • I'm not sure if cache is the best option? Because the function needs to be triggered on each page load, as the items in the cart might change. – Scott Lawrence Oct 29 '12 at 14:37
  • You could use global variable, sessions etc. but you cannot store the result, you can store the results in array or object format. – GoodSp33d Oct 29 '12 at 14:47
1

Seeing as how the data is not really an item that is cachable sitewide, you could quite easily use the $_SESSION variable.

Example:

function my_expensive_function($foo, $bar) {
  if (isset($_SESSION['my_really_cool_stuff'])) {
    return $_SESSION['my_really_cool_stuff'];
  }
  else {
    //Do what you would normally do to get everything you need
    //and put it in a $my_really_cool_stuff variable
    $_SESSION['my_really_cool_stuff'] = $my_really_cool_stuff;
    return $my_really_cool_stuff;
  }
}

This way, if the data already exists in the user session, it'll return that. If not, then it'll generate it, put it in the session, and then return it.

You can put the $_SESSION data into a theme function and make it available to templates as well if you wish, but that's probably a separate question (and you'd have to make sure you had a fallback in case the session data had not been set).

  • Great thanks, I'll have a play with on our beta site. – Scott Lawrence Oct 29 '12 at 14:45
  • @Chapabu can you store mysqli result object in sessions ? – GoodSp33d Oct 29 '12 at 15:08
  • 1
    You can't directly cache or store a database object. You need read out all of the results and make sure you are saving a proper PHP array or object. – mpdonadio Oct 29 '12 at 15:38
  • Apologies, I misunderstood what Kantu was asking! – Chapabu Oct 29 '12 at 15:42
  • Either way, the question asks about storing an array of data, so I'm not sure how this is relevant =/ – Chapabu Oct 29 '12 at 15:44
1

It looks like storing the array in a global or static variable would be a good solution for this use case. For example you could rebuild the function to make use of drupal_static() similarly to this:

function my_module_function() {
  $my_data = &drupal_static('my_static_variable');
  if (!isset($my_data)) {
    // Do your calculations here, and populate $my_data
    // with the correct stuff.
  }
  return $my_data;
}

For some more detailed explanations I'd suggest reading A beginner's guide to caching data in Drupal 7 (took the code example from there as well).

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.