sales_quote_save_before observer executing twice
I have an observer that observes the sales_quote_save_before
event and it executes twice when items are added to the cart, removed from the cart, or updated in the cart.
I assume that the save_befo开发者_如何转开发re
(and, for that matter, the save_after
event) are somewhere being triggered more than once.
I would like to know why / where this happens, and how to limit the observer to only executing once.
I have tried the solution offered here: Magento - customer_save_after always fired twice, but my observer still executes twice (when I log execution with Mage::log()
, the timestamp is 1 second different).
Any help is much appreciated.
What I ended up doing to solve the issue was comparing the updated_at
value of the $observer
object to the current time. If the last update was more than 3 seconds ago (completely arbitrary value), I let the observer execute, otherwise I return. This worked for me because the two instances of my observer always fired within 1-2 seconds.
I recognize this is not the best solution, since it does not account for server load or other latency issues, so if somebody can think of a better solution, I would appreciate the 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
I know this answer is to late, but I hope will be usefull as I'm also having the same issue and able found a good solution.
I'm testing it using Magento 1.9 CE, in sales_quote_save_before observer, use following codes :
$quote = $observer->getEvent()->getQuote();
$quote->addErrorInfo('error', 'your_module_name', 1, 'your error message')->setHasError(true);
return $this;
This solution will also disable (remove) the checkout button in shopping cart page.
精彩评论