This is my first time playing around with Magento. I'm building a module and I want to listen when a product is added to the cart so, I've tried to use the controller_action_postdispatch_checkout_cart_add
event but it doesn't seem to be firing.
In the events.xml file I have:
<?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="catalog_controller_product_view">
<observer name="test_mymodule_catalog_controller_product_view" instance="Test\MyModule\Observer\CatalogProductView" />
</event>
<event name="controller_action_postdispatch_checkout_cart_add">
<observer name="test_mymodule_controller_action_postdispatch_checkout_cart_add" instance="Test\MyModule\Observer\ControllerAddToCart" />
</event>
</config>
and in the observer file, I have the following code:
<?php
namespace Test\MyModule\Observer;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Store\Model\StoreManagerInterface;
class ControllerAddToCart implements ObserverInterface
{
protected $storeManager;
public function __construct(
StoreManagerInterface $storeManager
) {
$this->storeManager = $storeManager;
}
public function execute(Observer $observer) {
var_dump('hello ATC');
$console = '<script>console.log("action ATC");</script>';
echo $console;
$currency = $this->storeManager->getStore()->getCurrentCurrencyCode();
$consoleCurrency = '<script>console.log("'. $currency .'");</script>';
echo $consoleCurrency;
}
}
I know my module is enabled and it is working fine because I also added the catalog_controller_product_view
event and this one is firing.
I have to mention that I've tried the checkout_cart_add_product_complete
event, checkout_cart_save_after
and checkout_cart_product_add_after
. None of these work for me.
Also, not sure if it's relevant to say that I installed the Magento store following the instructions from here and the store has the Luma theme installed.
p.s: I don't see any errors in the console
Thanks in advance to all.