0

i am trying to update product price when product is added to cart

It works and the price is updated but it does not allow me to add any other product to the cart except the product where condition satisfy

below is my code

cart.php

use Magento\Framework\Event\ObserverInterface;

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

        $item = $observer->getEvent()->getData('quote_item');

        $product = $observer->getEvent()->getData('product');
        if($product->getCustomAttribute('test')->getValue() == 1){
            $price = $product->getCustomAttribute('custom_price')->getValue(); 
        }
        else{
            $price = $product->getFinalPrice();
        }
        $item->setOriginalCustomPrice($price);
        $item->setCustomPrice($price);
        return $this;
    }
}

etc/events.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="checkout_cart_product_add_after">
        <observer name="vendorname_checkout_cart_product_add_after" instance="Vendor\Module\Observer\Cart"/>
    </event>
</config>

Now the problem is, it only allow to add the product when if condition is true

if($product->getCustomAttribute('test')->getValue() == 1){
            $price = $product->getCustomAttribute('custom_price')->getValue(); 
        }
else{
            $price = $product->getFinalPrice();
        }
5
  • Why did you assign $price = $product->getFinalPrice(); in else condition?
    – Ankit
    Mar 29, 2022 at 15:46
  • to get the price of product that does not have that attribute values in if condition
    – Pramod
    Mar 29, 2022 at 17:03
  • Ohk sorry I misunderstood, did you check exceptions logs?
    – Ankit
    Mar 29, 2022 at 17:38
  • yeah nothing there
    – Pramod
    Mar 30, 2022 at 1:29
  • did you find below answer working? I think there would be any server logs or can see error in network tab
    – Ankit
    Mar 30, 2022 at 4:55

2 Answers 2

0

You don't have to do the else part at all, try below code with setIsSuperMode(true)

public function execute(\Magento\Framework\Event\Observer $observer) {

    $item = $observer->getEvent()->getData('quote_item');

    $product = $observer->getEvent()->getData('product');

    if($product->getCustomAttribute('test')->getValue() == 1){

        $price = $product->getCustomAttribute('custom_price')->getValue();
        $item->setOriginalCustomPrice($price);
        $item->setCustomPrice($price); 
        $item->getProduct()->setIsSuperMode(true);
    }
    
}
1
  • already tried that not working
    – Pramod
    Mar 30, 2022 at 5:40
0

Try this.

<?php
 namespace Vendor\Magento\Observer;
 use Magento\Framework\Event\ObserverInterface;
 use Magento\Framework\App\RequestInterface;

class Cart implements ObserverInterface
{
    public function execute(\Magento\Framework\Event\Observer $observer) 
    {
        $quote_item = $observer->getEvent()->getQuoteItem();
        $price = 10;
        $quote_item->setCustomPrice($price);
        $quote_item->setOriginalCustomPrice($price);
        $quote_item->getProduct()->setIsSuperMode(true);
    }
}

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.