Codegento Who Let Mage Out Of The Cage?

12Mar/111

Rewriting a Helper Class

The ability to rewrite classes are a super valuable tool when you want to extend the business logic of Magento, or make tweaks to the core. And since we have already talked about rewriting models and blocks, lets talk about helpers!

For a third time, I will state that you SHOULD NEVER CHANGE THE CORE, and if you can help it, you shouldn't just copy whole classes down into the local pool. If you want a more extensive lesson on rewriting, read the Rewriting a Model Class post. Below we will show you what you need to do to rewrite a helper.

Just as with models, helpers have a specific way to instantiate them. To use a model, we simple say: Mage::helper('awesome'). The thing that is unique about helpers is that you don't NEED to put the class name in the alias. If you don't, it will automatically look for Mage::helper('awesome/data').

Rewriting helpers is done the SAME way that models and blocks are done, but within the "helpers" group element. In our example, we will pretend that we NEVER EVER EVER want ANYONE to check out as a guest,. Because this is the case, we will need to change the Mage_Checkout_Helper_Data class's isAllowedGuestCheckout() function. Here is how we rewrite that class to do what we want:

Contents of the config.xml for the rewrite:

<helpers>
	<awesome>
		<class>Super_Awesome_Helper</class>
	</awesome>
	<checkout>
		<rewrite>
			<data>Super_Awesome_Helper_Data</data>
		</rewrite>
	</checkout>
</helpers>

And here is our helper -

<?php

class Super_Awesome_Helper_Data extends Mage_Checkout_Helper_Data
{
    public function isAllowedGuestCheckout(Mage_Sales_Model_Quote $quote, $store = null)
    {
        return false;
    }
}

Now, whenever Mage::helper('checkout') or Mage::helper('checkout/data') is called, it will create an instance of Super_Awesome_Helper_Data instead of Mage_Checkout_Helper_Data.

Happy helping!

Posted by Ben Robie

Comments (1) Trackbacks (0)
  1. Hello I am so thrilled I found your site, I really found you by error, while I was searching on Google for something else, Anyhow I am here now and would just like to say thanks a lot for a remarkable
    post and a all round exciting blog (I also love the theme/design), I don’t
    have time to browse it all at the minute but I have saved it and also included your RSS feeds, so when I have time I will be back to read a lot
    more, Please do keep up the excellent b.


Leave a comment

(required)

Trackbacks are disabled.