here am trying to add custom price during adding product to cart but checkout_cart_product_add_after event is not getting fired.
my API to add to cart is
http://localhost/magento/rest/V1/carts/636/items/ [POST]
this is my events.xml
vendor/Cart/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="customprice" instance="Vendor\Cart\Observer\CustomPrice" />
</event>
</config>
this is My Observer
Vendor\Cart\Observer
<?php
namespace Vendor\Cart\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\App\RequestInterface;
class CustomPrice implements ObserverInterface
{
protected $_request;
protected $_helper;
protected $cart;
public function __construct(\Magento\Framework\App\RequestInterface $request,\TW\Product\Helper\Data $helper,\Magento\Checkout\Model\Cart $cart)
{
$this->_request = $request;
$this->_helper = $helper;
$this->cart= $cart;
}
public function execute(\Magento\Framework\Event\Observer $observer) {
$this->_eventManager->dispatch('sales_quote_add_item', ['quote_item' => $item]);
$writer = new \Zend\Log\Writer\Stream(BP.'/var/log/test.log');
$logger = new \Zend\Log\Logger();
$logger->addWriter($writer);
$logger->info("success !!!!");
}
}
Am not understanding where am wrong, can i get help? , Thank you in advance
Best Answer
checkout_cart_product_add_after
is a frontent event.It is not work with Rest.You can try with below event which can fire for all area frontend, backend, Rest API, rest soap.