Magento 2 Create custom event and observer using events.xml programmatically.

Magento 2 Supports Create Custom Event Observer with your event name.

Under your module with the event defines using dispatch() method and events.xml file. You can Handle the Events data using Observer Class. Continue reading “Magento 2 Create custom event and observer using events.xml programmatically.”

How to redirect from Observer in Magento 2?

Magento 2, You can redirect to any custom URL from Observer file. When you have created event observer for any specific event and you want to redirect to any URL in a site, You can redirect from observer simply by calling below code,

Create an observer file under app/code/{Packagename}/{Modulename}/YourEventObserver.php

Continue reading “How to redirect from Observer in Magento 2?”

How to fetch getUrl() in Plugin or Observer file in magento 2?

By default you cant call $this->getUrl() method in Plugin or Observer file. When you need to call getUrl() function of Magento in plugin or observer file you need to pass dependancy as Magento\Framework\UrlInterface to construct() method.

Now In plugin or Observer function we can get getUrl() as below way,
$this->urlBuilder->getUrl(‘sales/order/view’, [‘order_id’ => 1]);

Run below command for get effect of our Dependancy Injection to code,

Above url will return as,