sales_quote_save_before 观察者执行两次
我有一个观察者,它观察 sales_quote_save_before
事件,当商品添加到购物车、从购物车中删除或在购物车中更新时,它会执行两次。
我假设 save_before
(以及就此而言,save_after
事件)在某个地方被多次触发。
我想知道为什么/在哪里发生这种情况,以及如何限制观察者只执行一次。
我已经尝试过这里提供的解决方案: Magento - customer_save_after 总是触发两次 ,但我的观察者仍然执行两次(当我使用 Mage::log()
记录执行情况时,时间戳相差 1 秒)。
非常感谢任何帮助。
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_before
(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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我最终解决该问题的方法是将
$observer
对象的updated_at
值与当前时间进行比较。如果最后一次更新是在 3 秒以上(完全任意值),我让观察者执行,否则我返回。这对我有用,因为我的观察者的两个实例总是在 1-2 秒内触发。我认识到这不是最好的解决方案,因为它没有考虑服务器负载或其他延迟问题,所以如果有人能想到更好的解决方案,我将不胜感激。
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.
我知道这个答案已经晚了,但我希望有用,因为我也遇到了同样的问题并且能够找到一个好的解决方案。
我正在使用 Magento 1.9 CE 进行测试,在 sales_quote_save_before 观察者中,使用以下代码:
此解决方案还将禁用(删除)购物车页面中的结账按钮。
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 :
This solution will also disable (remove) the checkout button in shopping cart page.