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 MagentoFrameworkEventObserverInterface;
/**
* Class Cart
* @package VendorNameChangepriceObserver
*/
class Cart implements ObserverInterface
{
/**
* @param MagentoFrameworkEventObserver $observer
*/
public function execute(MagentoFrameworkEventObserver $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="VendorModuleObserverCart"/>
</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();
}