php - Prevent infinite loop on update function that updates itself? -
i'm using drupal 7 commerce, although doesn't matter think.
there's hook (function) hook_entity_update()
called when entity (my shopping cart) updated.
i want recalculate shipping in hook, issue saves entity again once shipping recalculated , calls hook again, getting me stuck in infinite loop.
what best way solve this?
i thinking of using simple $_session variable, $_session['updating'] , set true @ start of hook, set false again @ end of hook , prevent function running return @ start if variable set true? i'm not sure if solution?
using session variable because it's form , function seems called multiple times when submitting. not sure if regular variable work?
one issue i'm running variable somehow not reset @ end of function , function never called again ...
using session variable seems work. stupid function somehow called 7 times when clicking 1 button ...
added @ start of function:
// check if entity commerce_order if ($type != 'commerce_order') { return; } // apply if order still in cart if ($entity->status != 'cart') { return; } if (isset($_session['bab_checkout']['updating']) && $_session['bab_checkout']['updating'] === true) { return; } $_session['bab_checkout']['updating'] = true;
and @ end of function:
// we're done updating $_session['bab_checkout']['updating'] = false;
still wonder if there no better solution?
Comments
Post a Comment