1

I have two event observer in my custom module :

// 1st : checkout_cart_product_add_after
    $item = $observer->getQuoteItem();
    $item->setIsNew(true);

// 2nd : checkout_cart_add_product_complete
    $quote = Mage::getSingleton('checkout/cart')->getQuote();
    $quote_id = $quote->getId(); // quote_id
    foreach($quote->getAllItems() as $quote_item)
    {
        if($quote_item->getIsNew() == true)
        {
            $item = $quote_item;
        }
    }
    // here I want to set custom quote item price and tried below code but it is not working
    $item->setCustomPrice($current_tour_price);
    $item->setOriginalCustomPrice($current_tour_price);
    $item->getProduct()->setIsSuperMode(true);
    $item->save(); // updated

I want to set custom price to quote item in checkout_cart_add_product_complete event.

If I am using the code setCustomPrice and setOriginalCustomPrice in checkout_cart_product_add_after event then it is working.

But I need quote_id as well with dynamic price for some other use and I can't get quote_id in checkout_cart_product_add_after event, so I am using checkout_cart_add_product_complete event.

How to set quote item custom price in event checkout_cart_add_product_complete?

EDIT : With $item->save() it save custom price for quote_item but on change currency custom price not going to update.

I also tried https://stackoverflow.com/a/38243913/4073217 but it is not working in my case.

How to set custom price for quote_item which work with currency change?

2 Answers 2

1

You can always update quote, hack it, change value directly in database, but it would be overkill and so far from best practice.

If you need quote_id, you can easily grab it from checkout_card_product_add_after event, because it set quote_item during event dispatch:

Mage::dispatchEvent('checkout_cart_product_add_after', array('quote_item' => $result, 'product' => $product));

All you have to do in your observer is:

$quote_id = $this->getEvent()->getQuoteItem()->getQuote()->getId();
5
  • Please check my question I'm able to get quote_id in event checkout_cart_add_product_complete and want to set custom price for quote_item. and not able to get quote_id in event checkout_cart_product_add_after. Commented Mar 2, 2017 at 10:21
  • There is no reason why you cannot achieve the same thing in checkout_cart_product_add_after as you're doing in checkout_cart_add_product_complete. Just use $this->getEvent()->getQuoteItem()->getQuote() and do whatever you need with quote items. Commented Mar 2, 2017 at 10:24
  • 1
    please check this : magento.stackexchange.com/a/156952/14039. Commented Mar 2, 2017 at 10:25
  • I still think that your business logic can be defined on the other way. But, let's not go further with it, you can try to rewrite Mage_Checkout_Model_Cart, and in addProduct() function, just add new event dispatch before return $this;. Something like: Mage::dispatchEvent('checkout_cart_product_add_after_custom', array('quote' => $this->getQuote(), 'quote_item' => $result, 'product' => $product));. If there is no quote_id set, just add check and call save(), and live with that little overhead. Commented Mar 2, 2017 at 10:34
  • Please check my edit question. Commented Mar 2, 2017 at 12:49
0

In Magento2:

You can achieve this with Event and Observer. There are following steps:

  • Step 1: Create Simple Extension
  • Step 2: Declaration of Module
  • Step 3: Registration of Module
  • Step 4: Activate the Module
  • Step 5: Declare event
  • Step 6: Create Observer for Custom Price

We need to catch the event - checkout_cart_product_add_after in step 5. In the Observer, write the code to set the custom price.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.