sales_quote_save_before 观察者执行两次

发布于 2024-11-08 21:25:18 字数 468 浏览 1 评论 0原文

我有一个观察者,它观察 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

愛放△進行李 2024-11-15 21:25:18

我最终解决该问题的方法是将 $observer 对象的 updated_at 值与当前时间进行比较。如果最后一次更新是在 3 秒以上(完全任意值),我让观察者执行,否则我返回。这对我有用,因为我的观察者的两个实例总是在 1-2 秒内触发。

我认识到这不是最好的解决方案,因为它没有考虑服务器负载或其他延迟问题,所以如果有人能想到更好的解决方案,我将不胜感激。

    $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

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
旧人 2024-11-15 21:25:18

我知道这个答案已经晚了,但我希望有用,因为我也遇到了同样的问题并且能够找到一个好的解决方案。

我正在使用 Magento 1.9 CE 进行测试,在 sales_quote_save_before 观察者中,使用以下代码:

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

return $this;

此解决方案还将禁用(删除)购物车页面中的结账按钮。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文