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

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()
		$canShow = Mage::getModel('admin/session')->isAllowed('rma');
		$container = $this->getParentBlock();
		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');

// file: app/code/local/Lowes/M2ePro/etc/config.xml
		<!-- Copy this event from M2ePro and replace only class with ours -->

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
				<Ess_M2ePro />

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

Fixed the problem, thanks
not published on website

QR Code: How to override Magento Observers?