I am trying to update product prices after adding to cart but the change on prices don't apply on it. I have created an event on checkout_cart_product_add_after while on observer I have written code like this :

class Cart implements ObserverInterface
     * @param \Magento\Framework\Event\Observer $observer
    public function execute(\Magento\Framework\Event\Observer $observer) {

        $item = $observer->getEvent()->getData('quote_item');
switch ($item->getQty()) {
                    case '2':
                   case '3':
                 case '4':
                case '5':



$newarray array have different prices ( $newarray[0] => 29.5,$newarray[1]=>29.00 etc), at the last line when I show $item->getOriginalCustomPrice(); it shows new price but on the cart, the old prices are shown. Thanks

  • check with this url - webkul.com/blog/magento2-set-custom-price-of-product Aug 26, 2019 at 6:56
  • @AnasMansuri I think both codes are the same. Aug 26, 2019 at 7:03
  • @Mahmood Rehman, have sure that this observer is hit for your add to cart. Code seems ok
    – Amit Bera
    Aug 26, 2019 at 7:18
  • @AmitBera Yes when I print something with exit it stops and shows the message on the console. It means code is working fine after adding to cart but prices didn't get updated. Aug 26, 2019 at 7:27

1 Answer 1


You have to remove the item and add a new one with the updated price :

//get the product from the observer 
$product = $observer->getEvent()->getData('product');

//get the item just added
$item = $observer->getEvent()->getData('quote_item');

//define the new price
$yourNewPrice = 100;

//get the current store
$storeId = $this->_storeManager->getStore()->getId();

//get the quote
$quote = $this->_checkoutSession->getQuote();

//create a new item
$quoteItemNewPrice = $this->objectManager->create('Magento\Quote\Model\Quote\Item');

//set data to this new item
$quoteItemNewPrice->setQuote($quote); //set quote
$quoteItemNewPrice->setStoreId($storeId); //set store id
$quoteItemNewPrice->setProduct($product); //set the product
$quoteItemNewPrice->setQty($item->getQty());  //set the quantity from the old item
$quoteItemNewPrice->setPrice($yourNewPrice); //set the price

//remove the current item

//add the new item to the quote and save it

Note: you must inject some missing classes such as the _checkoutSession

  • No luck with your code. Same original prices are added to cart. Aug 26, 2019 at 11:42
  • I may have made a mistake, try to first save the quote and then remove the old item ($item->delete()) Aug 27, 2019 at 2:05

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.