Sales_quote_save_before observer running twice

I have an observer who watches an event sales_quote_save_before, and it runs twice when items are added to the cart, removed from the cart, or updated in the cart.

I assume that the event save_before(and, for that matter, the event save_after) is triggered somewhere more than once.

I would like to know why / where this is happening, and how to restrict the observer to only execute once.

I tried the solution proposed here: Magento - customer_save_after always starts twice , but my supervisor still runs twice (when I register execution with Mage::log(), the timestamp is 1 second).

Any help is greatly appreciated.

+3
source share
2 answers

What I did to solve the problem was to compare the value of the updated_atobject $observerwith the current time. If the last update was more than 3 seconds ago (a completely arbitrary value), I allow the observer to complete, otherwise I will return. This worked for me, because two instances of my observer always worked for 1-2 seconds.

I understand that this is not the best solution, since it does not take into account the load on the server or other problems with the delay, so if someone could think of a better solution, I would appreciate feedback.

    $updatedAt = date('U', strtotime($observer->getQuote()->getUpdatedAt()));
    $now = time();
    if(($updatedAt + 3) > $now){
        return $this; //the observer has already been executed in this request
    }
    .... execute observer code
0
source

I know this answer is recent, but I hope it will be useful, as I also have the same problem and can find a good solution.

Magento 1.9 CE, sales_quote_save_before observer, :

$quote = $observer->getEvent()->getQuote();
$quote->addErrorInfo('error', 'your_module_name', 1, 'your error message')->setHasError(true);

return $this;

() .

0

All Articles