1

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

  • Is it triggering while add to cart manually in the website? – Prathap Gunasekaran May 30 '19 at 11:12
  • its website is not in use. so I working through API calls, its not triggering – Praveen Negimani May 30 '19 at 11:20
0

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.

$this->_eventManager->dispatch('sales_quote_add_item', ['quote_item' => $item]);
| improve this answer | |
  • Thank you for reply ill add and test it – Praveen Negimani May 30 '19 at 11:43
  • I updated the code please check, its not working – Praveen Negimani May 30 '19 at 12:18
  • what change you have done? – Amit Bera May 30 '19 at 12:29
  • added dispatch event in observer – Praveen Negimani May 30 '19 at 12:54

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.