事件观察者没有获得产品详细信息

时间:2021-09-26 16:26:23

I have created a observer event like below inside my config.xml file

我在config.xml文件中创建了一个类似下面的观察者事件

<events>          
              <sales_quote_add_item>
                  <observers>
                     <CustomModule_model_observer>
                        <type>singleton</type>
                        <class>CustomModule_Model_Observer</class>
                        <method>updatePrice</method>
                     </CustomModule_model_observer>
                 </observers>
              </sales_quote_add_item>
    </events>

In my Model i have created Observer.php

在我的模型中,我创建了Observer.php

require_once 'Mage/Checkout/controllers/CartController.php';

class CustomModule_Observer
{
    public function _construct()
      {
      }

    public function getNewPrice()
      {

        $newprice = "5";

        return $newprice;
      }

     public function updatePrice($observer)
     {
        $event = $observer->getEvent();
        $product = $event->getProduct();// not getting any values how can i get this.
        $quote_item = $event->getQuoteItem();
        $new_price = $this->getNewPrice();
        $quote_item->setOriginalCustomPrice($new_price);
        $quote_item->save();
      }
  }

Reference Changing the price in quote while adding product to cart: magento

参考在将产品添加到购物车时更改报价中的价格:magento

2 个解决方案

#1


You did not get any value in $product = $event->getProduct(); because this object is not set when add item event is dispatched.

你没有在$ product = $ event-> getProduct()中获得任何价值;因为在调度添加项目事件时未设置此对象。

Please check Mage_Sales_Model_Quote class line number 939.

请检查Mage_Sales_Model_Quote类的行号939。

$item->setQuote($this);
    if (!$item->getId()) {
        $this->getItemsCollection()->addItem($item);
        Mage::dispatchEvent('sales_quote_add_item', array('quote_item' => $item));
    }

Its only return quote_item object. If want products so you have to find some other way like :

它只返回quote_item对象。如果想要产品,那么你必须找到其他方式:

 $quote_item = $event->getQuoteItem(); // Which return quote item 
 $quote_item->getProduct();

I hope this is work for you Thanks.

我希望这对你有用谢谢。

#2


I have tried this, and its working, it gives us the product id.

我试过这个,它的工作,它给了我们产品ID。

$quote = Mage::getSingleton('checkout/session')->getQuote();
$cartItems = $quote->getAllVisibleItems();
foreach ($cartItems as $item){
   $productId = $item->getProductId();
   $product = Mage::getModel('catalog/product')->load($productId);
   // Do something
}

#1


You did not get any value in $product = $event->getProduct(); because this object is not set when add item event is dispatched.

你没有在$ product = $ event-> getProduct()中获得任何价值;因为在调度添加项目事件时未设置此对象。

Please check Mage_Sales_Model_Quote class line number 939.

请检查Mage_Sales_Model_Quote类的行号939。

$item->setQuote($this);
    if (!$item->getId()) {
        $this->getItemsCollection()->addItem($item);
        Mage::dispatchEvent('sales_quote_add_item', array('quote_item' => $item));
    }

Its only return quote_item object. If want products so you have to find some other way like :

它只返回quote_item对象。如果想要产品,那么你必须找到其他方式:

 $quote_item = $event->getQuoteItem(); // Which return quote item 
 $quote_item->getProduct();

I hope this is work for you Thanks.

我希望这对你有用谢谢。

#2


I have tried this, and its working, it gives us the product id.

我试过这个,它的工作,它给了我们产品ID。

$quote = Mage::getSingleton('checkout/session')->getQuote();
$cartItems = $quote->getAllVisibleItems();
foreach ($cartItems as $item){
   $productId = $item->getProductId();
   $product = Mage::getModel('catalog/product')->load($productId);
   // Do something
}