I have an observer which is configured in my module

File : app\code\MStack\Exchange\etc\frontend\events.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="sales_quote_address_collect_totals_after">
        <observer name="changeTaxTotal" instance="MStack\Exchange\Observer\ChangeTaxTotal"/>

File : app\code\MStack\Exchange\Observer\ChangeTaxTotal.php

namespace MStack\Exchange\Observer;

use \Magento\Framework\Event\ObserverInterface;
use \Magento\Framework\Event\Observer;

class ChangeTaxTotal implements ObserverInterface
    public $additionalTaxAmt = 2;

    public function execute(Observer $observer)
        /** @var Magento\Quote\Model\Quote\Address\Total */
        $total = $observer->getData('total');
        $total->addTotalAmount('tax', $this->additionalTaxAmt);
        return $this;

As you can see, I am listening to the event sales_quote_address_collect_totals_after. I am checking this from cart page and obviously this event is firing from there.

I can see that, my event is listening when page loads. But it is not listening for api calls which are asserted by Magento in order to calculate totals (for example, if you try to use : Estimate Shipping and Tax). I can point out the urls which are not firing my observer.


So why my observer is not listening for such API request ? How can I change my code so that it will work for API request ?


You need to place your events.xml file additionally in etc/webapi_rest folder or move to root scope etc/.

Configuration files that are in the top level of that module’s etc directory are global to that component. Configuration files placed in subdirectories (adminhtml, frontend, webapi_rest, webapi_soap) apply only to those respective functional areas.


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.