1

我有一个观察员正在观看活动 sales_quote_item_set_product。在其中,我正在检查一些条件以确保该项目仍然可用。如果不是,我运行以下代码:

Mage::helper('checkout/cart')->getCart()->removeItem($item->getId())->save();
Mage::getSingleton('message/session')->addError($item->getName() . ' is no longer available.');

我遇到的问题是,如果某个商品不可用并且客人在产品查看页面上,购物车说该商品在购物车中,但购物车的总数会更新以反映被删除的产品。也不会显示错误消息。如果您转到另一个页面或刷新产品视图页面,则会显示错误消息,并且购物车中的商品数量将是正确的。

所以我的想法是我需要在执行周期的早期运行这段代码,但我不知道我应该观察什么事件,或者我是否根本不应该使用观察者。我尝试使用 sales_quote_load_after,但不知何故导致了递归错误。谁能告诉我应该在何时/何地运行此代码?

另一个疯狂的想法可能是因为我使用的是数据库会话而不是文件系统?

4

2 回答 2

1

问题是在呈现消息块之后添加了错误。在添加错误后,我通过向购物车页面添加重定向来修复它。

$request = Mage::app()->getRequest();
if($request->getModuleName() != 'checkout' && $request->getControllerName() != 'cart' && $request->getActionName() != 'index') {
    Mage::app()->getResponse()->setRedirect(Mage::getModel('core/url')->getUrl('checkout/cart/index'))
        ->sendResponse();
    exit;
}
于 2013-08-27T17:08:23.120 回答
0

You didn't mention it, but this sounds like you're running code during an AJAX request. When you say

Mage::getSingleton('message/session')->addError($item->getName() . ' is no longer available.');

You're adding an error to Magento's session object. Every time a Magento page renders, it checks the session object for errors, and displays them all. This allows multiple developers to add multiple errors to the session, redirect Magento back to the original form.

This doesn't work during an ajax request, because (typically) the rendering process is skipped in lieu of a JSON object or simple, errorless HTML chunk being rendered (leaving the errors in the session).

Knowing what the full request cycle looks like (what's ajax, what's not) would help someone come up with a more concrete answer to your question.

于 2013-08-26T23:14:05.983 回答