How to override Magento Observers?

Posted by Damodar Bashyal on November 24, 2016

 

How to override Magento Observers?

Today's task was to show/hide M2ePro's 'Show eBay Order' button based on permission. Earlier I had done the similar task for 'Create RMA' button which was quite straight forward. But that rewrite didn't work this time as it was an Observer.

This is how I was able to hide 'RMA' tab and 'Create RMA' button from Magento order view page.

// file: app/code/local/Lowes/Rma/etc/config.xml
<config>
	<global>
		<blocks>
			<lowesrma>
				<class>Lowes_Rma_Block</class>
			</lowesrma>
			<enterprise_rma>
				<rewrite>
					<adminhtml_order_view_tab_rma>Lowes_Rma_Block_Adminhtml_Order_View_Tab_Rma</adminhtml_order_view_tab_rma>
					<adminhtml_order_view_buttons>Lowes_Rma_Block_Adminhtml_Order_View_Buttons</adminhtml_order_view_buttons>
				</rewrite>
			</enterprise_rma>
		</blocks>
	</global>
</config>

Show/hide RMA tab based on user permission

// file: app/code/local/Lowes/Rma/Block/Adminhtml/Order/View/Tab/Rma.php
class Lowes_Rma_Block_Adminhtml_Order_View_Tab_Rma extends Enterprise_Rma_Block_Adminhtml_Order_View_Tab_Rma
{
	public function canShowTab()
	{
		return Mage::getModel('admin/session')->isAllowed('rma');
	}
}

Show/hide 'Create RMA' button based on user permission

// file: app/code/local/Lowes/Rma/Block/Adminhtml/Order/View/Buttons.php
class Lowes_Rma_Block_Adminhtml_Order_View_Buttons extends Enterprise_Rma_Block_Adminhtml_Order_View_Buttons
{
	public function addButtons()
	{
		parent::addButtons();
		$canShow = Mage::getModel('admin/session')->isAllowed('rma');
		$container = $this->getParentBlock();
		if(!$canShow){
			$container->removeButton('create_rma');
		}
		return $this;
	}
}

Now, back to original question. How to override Magento Observers?

You need to follow exactly as above like rewrite original observer that you are trying to extend or override except one step at the end here:

// Observer I'm trying to override:
// app/code/community/Ess/M2ePro/Model/Observer/Dispatcher.php
// My Observer file: 
// app/code/local/Lowes/M2ePro/Model/Observer/Dispatcher.php
class Lowes_M2ePro_Model_Observer_Dispatcher extends Ess_M2ePro_Model_Observer_Dispatcher
{
	public function orderView(Varien_Event_Observer $eventObserver)
	{
		// Proceed further only if user has permission to M2ePro pages.
		$canShow = Mage::getModel('admin/session')->isAllowed('ebay');
		if(!$canShow){
			return;
		}

		parent::orderView($eventObserver);
	}
}
// file: app/code/local/Lowes/M2ePro/etc/config.xml
<global>
	<models>
		<lowes_m2epro>
			<class>Lowes_M2ePro_Model</class>
		</lowes_m2epro>
		<M2ePro>
			<rewrite>
				<observer_dispatcher>Lowes_M2ePro_Model_Observer_Dispatcher</observer_dispatcher>
			</rewrite>
		</M2ePro>
	</models>
	<events>
		<!-- Copy this event from M2ePro and replace only class with ours -->
		<core_block_abstract_prepare_layout_after>
			<observers>
				<M2ePro_observer_view_order>
					<type>singleton</type>
					<class>lowes_m2epro/observer_dispatcher</class>
					<method>orderView</method>
				</M2ePro_observer_view_order>
			</observers>
		</core_block_abstract_prepare_layout_after>
	</events>
</global>

The only difference here is, we copied exact event config block from M2ePro and replace <class> with our observer class. As we are overriding event here, we need to load M2ePro extension before our module like this:

// file: app/etc/modules/Lowes_M2ePro.xml
<config>
	<modules>
		<Lowes_M2ePro>
			<active>true</active>
			<codePool>local</codePool>
			<depends>
				<Ess_M2ePro />
			</depends>
		</Lowes_M2ePro>
	</modules>
</config>

Nadeem Arif posted on - Thursday 8th of December 2016 02:56:22 AM

Fixed the problem, thanks

Best Magento Extensions posted on - Tuesday 13th of June 2017 12:25:59 AM

Our online Magento 2 Store offers variety and most updated extensions for Magento E-Commerce platform. Our pioneer motive is to make the ideas of clients, more innovative. Our Magento store leverages its rich experience to deliver customized solutions to all the clients worldwide. Our goal is to emerge as a global information technology company delivering world class solutions and services to the clients through relentless pursuit of innovation, excellence and human values.

Hire Magento Developers posted on - Wednesday 30th of August 2017 11:49:43 AM

Helpful post!! Such sort of posts are not quite useful for Magento developers, but for all long term developers who can never be bothered learning the syntax, and for once-overs to verify everything seems ok!
 
not published on website


QR Code: How to override Magento Observers?