1

I have faced issue in checkout_cart_product_add_after event using add to cart rest API.

Same add to cart event is working in website. But it is not working in Add to cart rest API.

Add to cart API URL: http://magento2/rest/V1/carts/mine/items

Method: POST

Below is my event file path:

File path: magento/app/code/Vendor/CustomModule/etc/webapi_rest/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_subscrption_product_add" instance="Vendor\CustomModule\Observer\CustomPrice" />
    </event>
</config>

File path: magento/app/code/Vendor/CustomModule/Observer/CustomPrice.php

<?php
namespace Vendor\CustomModule\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\Exception\InputException;

class CustomPrice implements ObserverInterface
{
    protected $request;

    public function __construct(
        \Magento\Framework\App\RequestInterface $request
    )
    {
        $this->request = $request;
    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        echo "Calling observer.....";die;
    }
}

Let me know if any have idea about to call add to cart event in rest API.

Any help would be appreciated. Thanks.

10
  • Please try to add events.xml inside /etc/events.xml and check once
    – Jack
    Feb 18 '20 at 2:37
  • @Jack I have tried with to placed event.xml in the etc directory but not working. /etc/events.xml Feb 18 '20 at 4:44
  • any clue in a log file?
    – Jack
    Feb 18 '20 at 8:06
  • No any log related to API. Feb 18 '20 at 9:12
  • are you trying with fresh Magento or customized Magento project?
    – Jack
    Feb 18 '20 at 12:17
0

This is the list of events you can match during 'magento add_product rest api'

sales_quote_item_set_product
sales_quote_item_qty_set_after
sales_quote_product_add_after
sales_quote_item_save_before
sales_quote_item_save_after

"sales_quote_add_item" works only on add product, it doesn't work on update qty

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.