Observer doesn't catch event Magento 2
-
11-10-2020 - |
题
I have implemented 2 observers for checkout_cart_product_add_after, sales_order_place_after event. But these observers didn't catch events.
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="sales_order_place_after">
<observer name="websoftpro_utm_after_checkout_event" instance="WebSoftPro\UTM\Observer\SalesOrderAfterPlaceObserver" />
</event>
<event name="checkout_cart_add_product_complete">
<observer name="websoftpro_utm_after_add_to_cart_event" instance="WebSoftPro\UTM\Observer\CheckoutCartProductAddAfterObserver" />
</event>
</config>
Observer file
namespace WebSoftPro\UTM\Observer;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
class CheckoutCartProductAddAfterObserver implements ObserverInterface
{
/**
* @param Observer $observer
* @return void
*/
public function execute(\Magento\Framework\Event\Observer $observer)
{
$product = $observer->getEvent()->getProduct();
$quoteItem = $observer->getEvent()->getQuoteItem();
}
}
I have used xdebug to debug. It went through this code:
$this->_eventManager->dispatch(
'checkout_cart_product_add_after',
['quote_item' => $result, 'product' => $product]
);
But it doesn't go to the Observer file.
What is the problem ?
Somebody, please help me out. Please!
解决方案
I have found the solution in this post: Observer not working in Magento 2
For that to work:
Move the file events.xml to global (ModuleName/etc folder)
Clear all cache
run: php bin/magento setup:upgrade --keep-generated