【问题标题】:Magento : how to change item price when adding it into the cartMagento:将商品添加到购物车时如何更改商品价格
【发布时间】:2011-09-01 12:26:48
【问题描述】:

当我将商品添加到购物车时,我希望能够以编程方式(而不是通过目录或购物车规则)更改商品价格。

以下答案Programmatically add product to cart with price change 显示了如何在更新购物车时执行此操作,而不是在添加产品时。

谢谢

【问题讨论】:

    标签: magento cart


    【解决方案1】:

    您可以使用观察者类来监听 checkout_cart_product_add_after,并使用产品的“超级模式”针对报价项目设置自定义价格。

    在您的 /app/code/local/{namespace}/{yourmodule}/etc/config.xml 中:

    <config>
        ...
        <frontend>
            ...
            <events>
                <checkout_cart_product_add_after>
                    <observers>
                        <unique_event_name>
                            <class>{{modulename}}/observer</class>
                            <method>modifyPrice</method>
                        </unique_event_name>
                    </observers>
                </checkout_cart_product_add_after>
            </events>
            ...
        </frontend>
        ...
    </config>
    

    然后在 /app/code/local/{namespace}/{yourmodule}/Model/Observer.php 创建一个 Observer 类

    <?php
        class <namespace>_<modulename>_Model_Observer
        {
            public function modifyPrice(Varien_Event_Observer $obs)
            {
                // Get the quote item
                $item = $obs->getQuoteItem();
                // Ensure we have the parent item, if it has one
                $item = ( $item->getParentItem() ? $item->getParentItem() : $item );
                // Load the custom price
                $price = $this->_getPriceByItem($item);
                // Set the custom price
                $item->setCustomPrice($price);
                $item->setOriginalCustomPrice($price);
                // Enable super mode on the product.
                $item->getProduct()->setIsSuperMode(true);
            }
    
            protected function _getPriceByItem(Mage_Sales_Model_Quote_Item $item)
            {
                $price;
    
                //use $item to determine your custom price.
    
                return $price;
            }
    
        }
    

    【讨论】:

    • 如果我使用观察者将自定义价格添加到购物车,那么我将得到订单电子邮件模板中的单价乘以商品数量。如何修复该 app\design\frontend\default\rfg\template\email\order\items\order\default.phtml formatPrice($_item->getRowTotal()) ?>跨度>
    • 谁能从$item-&gt;getProduct()-&gt;setIsSuperMode(true);这一行解释setIsSuperMode?它有什么作用?
    • 这里是supermode的解释:“quote super mode flag mean what we work withquote without restrict” 例如:如果super mode设置为true,magneto不会检查产品是否可见在目录中
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多