【问题标题】:How to Change the price before adding to cart in magento?如何在magento添加到购物车之前更改价格?
【发布时间】:2015-05-05 09:14:18
【问题描述】:

如何在添加到购物车 Magento 时更改产品价格,示例:

假设我想添加 1 件价格为 10 美元的产品,我想要 以 $10*5 = $50 显示价格,当我添加 2 个我想要的产品时 显示 $10*10 = $100。所以它的公共乘数是 5.(x 的倍数)。

【问题讨论】:

标签: magento observers magento-1.9.1


【解决方案1】:

在 app/community/Custom_Module/Modulename/Model 中创建 Observer.php 文件,将以下代码复制到该文件中。

class Custom_Module_Modulename_Model_Observer
{
    public function _construct()
    {
    }

    public function getNewPrice()
    {

        $login  = Mage::getSingleton('customer/session')->isLoggedIn();
        $roleId = Mage::getSingleton('customer/session')->getCustomerGroupId();
        $userrole   = Mage::getSingleton('customer/group')->load($roleId)->getData('customer_group_code');
        $userrole   = strtolower($userrole);
        $quote = Mage::getSingleton('checkout/session')->getQuote();
        $cartItems = $quote->getAllVisibleItems();
        foreach ($cartItems as $item) {
            $productId = $item->getProductId();
            $product = Mage::getModel('catalog/product')->load($productId);
        }
        $batch_qty = $product->getBatchQty();
        $actualPrice = $product->getPrice();
        $specialPrice = $product->getFinalPrice();
        if (isset($batch_qty) && $userrole=="retailer") {
            if (isset($specialPrice)) {
                $newprice = $specialPrice*$batch_qty;
            } else {

                $newprice = $actualPrice*$batch_qty;
            }

        } else {
            $newprice= $actualPrice;
        }

        return $newprice;
    }

    public function updatePrice($observer)
    {
        $event = $observer->getEvent();
        $product = $event->getProduct();
        $quote_item = $event->getQuoteItem();
        $new_price = $this->getNewPrice();
        $quote_item->setOriginalCustomPrice($new_price);
        //$quote_item->save();
        $quote_item->getQuote()->save();
        //Mage::getSingleton('checkout/cart')->save();
    }

}

复制以下代码并将其粘贴到您的 app/community/Custom_Module/Modulename/etc/config.xml 标记内

> <events>            
>           <sales_quote_add_item>
>               <observers>
>                  <Custom_Module_Modulename_model_observer>
>                     <type>singleton</type>
>                     <class>Custom_Module_Modulename_Model_Observer</class>
>                     <method>updatePrice</method>
>                  </Custom_Module_Modulename_model_observer>
>              </observers>
>           </sales_quote_add_item>
>       </events>

这对我来说很好。

【讨论】:

  • 我正在使用您建议的方法,但现在面临 Magento 错误。该页面显示“无法将产品添加到购物车”(尽管可以)。此外,exception.log 文件说:“DEBUG (7): Exception message: Cannot send headers; headers already sent in /var/www/html/[dev folder]/app/code/local/[namespace]/Price/模型/Observer.php,第 1 行 ... 跟踪:#0 /var/www/html/[dev 文件夹]/lib/Zend/Controller/Response/Abstract.php(148):Zend_Controller_Response_Abstract->canSendHeaders(true)" .
  • 这个方法对我有用,请您尝试启用注释行并检查,同时清除您的浏览器缓存和磁力缓存并检查
  • 在 Chrome 和 FireFox 中,我删除了缓存和 cookie 并重新加载了页面,但它根本无法工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-09-04
  • 1970-01-01
  • 2012-04-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-30
相关资源
最近更新 更多