I want to create an Observer for an event.
i.e. controller_action_postdispatch_checkout_onepage_saveBilling
How to create it in Magento2 ?
1 Answer
Create events.xml at app\code\YOURNAMSPACE\MODULENAME\etc
<?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="controller_action_postdispatch_checkout_onepage_saveBilling">
<observer name="YOUR_EVENT_NAME" instance="YOURNAMSPACE\MODULENAME\Observer\YourObserverClass" />
</event>
</config>
cretae YourObserverClass.php
at app\code\YOURNAMSPACE\MODULENAME\Observer
<?php
namespace YOURNAMSPACE\MODULENAME\Observer;
use Magento\Framework\Event\Observer as EventObserver;
use Magento\Framework\Event\ObserverInterface;
class YourObserverClass implements ObserverInterface
{
public function execute(EventObserver $observer)
{
//Do your code here
}
}