I have create event controller_action_predispatch_checkout_cart_add
as given below:
Vendor/Module/etc/frontend/events.xml
<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="controller_action_predispatch_checkout_cart_add">
<observer name="bialcheckout_before_product_add_to_cart" instance="Vendor\Module\Observer\ProductBeforeAddToCart" />
</event>
</config>
Vendor/Module/Observer/ProductBeforeAddToCart.php
<?php
namespace Vendor\Module\Observer;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
class ProductBeforeAddToCart implements ObserverInterface {
public function __construct(
// dependencies
){
....
}
public function execute(Observer $observer)
{
$productId = $observer->getRequest()->getParam('product');
$superAttributes = $observer->getRequest()->getParam('super_attribute');
// Rest of the code
}
}
Above code works fine if I add product from product details page. Now in one of my custom module, I am adding product to cart programatically. After product is added to cart programatically, above method is not executed automatically. From my custom module I want to call my code written in the above observer with product data. How I can do that?
I mean, How to trigger a observer programatically with it's required data from a custom controller file ?