0

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 1

7

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
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.