cancel
Showing results for 
Search instead for 
Did you mean: 

How to get Product Id from Item Object in Observer?

How to get Product Id from Item Object in Observer?

Controller File:

public function editAction()
{                                                                                                                                                         
   $quoteId=$this->getRequest()->getParam("quoteid");
   $quote = Mage::getModel('sales/quote')->load($quoteId);
   Mage::dispatchEvent('checkout_cart_product_add_after', $quote);
}

Observer File:

public function changePrice($observer) {
   $item = $observer->getEvent()->getQuote();
   echo "<pre>";print_r($item->getProductId());
   exit();
}

How to get Product Id in Observer from Item?

If you have any idea then share here please.

Thank You.

2 REPLIES 2

Re: How to get Product Id from Item Object in Observer?

Hello @Ashish_k_php

 

public function changePrice($observer) {
   $item = $observer->getEvent()->getQuote();
   echo "<pre>";print_r($item->getProductId());
   exit();
}

it shold be

public function changePrice($observer) {
   $quote= $observer->getEvent()->getQuote();
   $item = $quote->getAllItems();
 foreach($item  as $_item){
   echo $_item->getProduct()->getId();
}
}

Do you want to change price using observer then use below event for magento1

<events>
    <sales_quote_add_item>
        <observers>
            <priceupdate_observer>
                <type>singleton</type>
                <class>mymodule/observer</class>
                <method>updatePrice</method>
            </priceupdate_observer>
        </observers>
    </sales_quote_add_item>
</events>

public function updatePrice($observer) {
    $event = $observer->getEvent();
    $quote_item = $event->getQuoteItem();
    $new_price = <insert logic>
    $quote_item->setOriginalCustomPrice($new_price);
    $quote_item->save();
}

Hope it will help you.

 

If it will work then mark as solution.


Problem solved? Click Kudos & Accept as Solution!
Sunil Patel
Magento 2 Certified Professional Developer & Frontend Developer

Re: How to get Product Id from Item Object in Observer?

@Sunil Patelthank you for the reply.

For Simple Product working fine but for bundle  product option have wrong value in cart page.