I'm trying to run some code whenever an order is updated. I've found the function hook_commerce_order_presave which fulfils this purpose, but it seems this also fires on page load. Is this normal, or is there another module that would be causing this? Is there a more suitable function I could use?


This is unusual. The hook should not be executed on every page load. You might want to check if some code in a custom module, custom theme, rule is causing an update of commerce_order which could trigger the hook.

To ensure that the order is updated on every page load, you can implement hook_entity_update(), in your custom module

function MODULE_entity_update($entity, $type) {
  if ($type == 'commerce_order') {
    // Execute your code here.

If the code inside if loop is still executed, you can be sure that the something is triggering update of commerce order on each page load. And you can proceed to find out what is the actual cause of the problem.

  • Yeah I've just tried hook_entity_update and the same thing occurs - the type is commerce_order. I thought it was unusual. – Chris Feb 10 '17 at 8:56
  • @Chris Hmm. Is it occurring on all pages? Or only on specific pages, like cart? – AjitS Feb 10 '17 at 9:07
  • All pages, but strangely not every time. Every 2 or 3 refreshes. Running out of ideas here... – Chris Feb 10 '17 at 9:19
  • Ok I've narrowed it down to a call to commerce_cart_order_load in my template.php. Surely this shouldn't fire hook_entity_update??? – Chris Feb 10 '17 at 9:38
  • @Chris : commerce_cart_order_load shouldn't trigger this either. Is anything changing in the order when the cart is loaded? That could trigger the hook. – AjitS Feb 10 '17 at 12:17

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.