0

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':
                        $item->setOriginalCustomPrice($newarray[0]);
                        $item->setCustomPrice($newarray[0]);
                        $item->getProduct()->setIsSuperMode(true);
                        break;
                   case '3':
                        $item->setOriginalCustomPrice($newarray[1]);
                        $item->setCustomPrice($newarray[1]);
                        $item->getProduct()->setIsSuperMode(true);
                        break; 
                 case '4':
                        $item->setOriginalCustomPrice($newarray[2]);
                        $item->setCustomPrice($newarray[2]);
                        $item->getProduct()->setIsSuperMode(true);
                        break;
                case '5':
                        $item->setOriginalCustomPrice($newarray[3]);
                        $item->setCustomPrice($newarray[3]);
                        $item->getProduct()->setIsSuperMode(true);
                        break;

                }


    }
}

$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 – Anas Mansuri Aug 26 at 6:56
  • @AnasMansuri I think both codes are the same. – Mahmood Rehman Aug 26 at 7:03
  • @Mahmood Rehman, have sure that this observer is hit for your add to cart. Code seems ok – Amit Bera Aug 26 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. – Mahmood Rehman Aug 26 at 7:27
0

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
$quoteItemNewPrice->setOriginalCustomPrice($yourNewPrice);

//remove the current item
$item->delete();

//add the new item to the quote and save it
$quote->addItem($quoteItemNewPrice);
$quote->save();

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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