2

我已经在 Magento 中成功设置了一个绑定到自定义添加到购物车事件的观察者。现在,在某种情况下,我需要停止添加到购物车事件(或者甚至删除它?),并向用户发送一条消息。

目前,该产品设置为允许每个订单的最大数量为 1。如果用户尝试添加超过 1 个,Magento 会显示错误消息并阻止将项目添加到购物车。

截图:[链接] http://cl.ly/image/3V2C2j1S0f37

理想情况下,我想从我的观察者那里模仿相同的功能,但我完全不知道如何解决这个问题,因为我是 Magento 的新手。

我读过我需要抛出一个异常。但是当我这样做时,它只是把我带到了一个错误页面。想知道我是否需要以某种方式添加此修改版本:

Mage::helper('cataloginventory')->__('The maximum quantity allowed for purchase is %s.', '<span class="sku-failed-qty" id="sku-stock-failed-' . $item->getId() . '">' . ($item->getQtyMaxAllowed()  * 1) . '</span>');

任何帮助或指针将不胜感激。

4

2 回答 2

5

您可以尝试以下方法。这将检查数量是否大于 1。如果是这种情况,我们会将数量更新为 1。我们还添加了一条通知消息,以便通知用户数量变化。

<checkout_cart_product_add_after>
<observers>
    <company_module_name>
        <type>singleton</type>
        <class>Company_Module_Model_Observer</class>
        <method>checkItem</method>
    </company_module_name>
</observers>

public function checkItem($obj) {       
    //check if qty is more then 1
    if($obj->getProduct()->getQty() > 1) {
    //set notice to inform the visitor that there is a maximum
    Mage::getSingleton('core/session')->addNotice('The maximum quantity allowed for purchase is 1.');

    //get the event 
    $event = $obj->getEvent();

    //get the product we have added
    $product = $event->getProduct();

    //get the quote item
    $quoteItem = $event->getQuoteItem();

    //change the qty to 1 allowed
    $quoteItem->setQty(1);

    //recalc totals
    $quoteItem->getQuote()->collectTotals();

    //save the item
    $quoteItem->getQuote()->save();
}

===== 编辑 ======

另一种选择是:

<controller_action_predispatch_checkout_cart_add>
    <observers>
        <aquait_aquait_name>
            <type>singleton</type>
            <class>Aquait_Aquait_Model_Observer</class>
            <method>checkfunction</method>
        </aquait_aquait_name>
    </observers>
</controller_action_predispatch_checkout_cart_add>


public function checkfunction($observer) 
{
    if ($observer->getEvent()->getControllerAction()->getFullActionName() == 'checkout_cart_add') {
        $productId = Mage::app()->getRequest()->getParam('product');
        $product = Mage::getModel('catalog/product')->load($productId);
        if (Mage::app()->getRequest()->getParam('qty') > 1) {
            Mage::getSingleton('core/session')->addNotice('The maximum quantity allowed for purchase is 1.');
            Mage::app()->getResponse()->setRedirect($product->getProductUrl());
        }
    }

}
于 2013-01-07T21:51:04.887 回答
1

特征:

  • 之后清除错误信息
  • 跳过所有“添加到购物车”代码
  • 之后重定向到您选择的页面

在尝试了所有可以想象的事情以尝试优雅地中止“添加到购物车”之后,我设法将各个部分组合起来,并通过单步调试源发现了标志 FLAG_NO_DISPATCH。其他更改参数或设置重定向的尝试将被“添加到购物车”过程中的某些代码覆盖。

    Mage::app()->getResponse()->setRedirect($store_product->getProductUrl());
    Mage::getSingleton('checkout/session')->addError(Mage::helper('checkout')->__('Sorry, the price of this product has been updated. Please, add to cart after reviewing the updated price.'));
    $observer->getControllerAction()->setFlag('', Mage_Core_Controller_Varien_Action::FLAG_NO_DISPATCH, true);  

您可以收到通知,而不是错误:

Mage::getSingleton('core/session')->addNotice('Sorry, this product is currently not available...');
于 2013-02-06T20:14:13.370 回答