Customize Magento Store With Magento Overriding

magento overriding

A ring that looks beautiful but doesn’t fit your finger is of no use – Right? Similarly, a development platform which is best tool for your business, but doesn’t satisfy your business needs is of no use. It’s quite vital for any system to support extendability and scalability in core itself. Magento – The Super Star sets this true. Magento programmers can easily create custom extensions to satisfy the custom needs. Moreover, rather than needs, Magento customization even helps you to standout amongst the crowd with ‘unique features’.

We believe that – “Succeed with platform that meets your need”.

Succeed with platform that meets your need

 

Magento allows customization in every way whether you need customization in theme, customization with extensions, customization with basic features – everything & anything. One method included in Magento customization is ‘Overriding’. It permits you to override core files to change the flow and behavior of the core features of Magento.

WAIT! Are you pondering that why to override the core classes instead of directly making changes?

changes?

 

Got an answer? No? Well by directly making changes in the core Magento classes can hamper you at the time of upgrading the Magento version. All your efforts made to change things will go in vain. As when you upgrade Magento the new core files will be automatically replaced with the old core files which were changed. Moreover, when we override the core classes, the alteration will be done in our local files and the main core files will remain intact. This means that the up-gradation will not affect any changes.

There are numerous ways in Magento that permits you to override core files. One of them is the implementation of the Event Observer Pattern. With this method you can set particular situations for particular events i.e. you will decide your viewers for desired events and they will be called when that event is raised in Magento. Confused? Well, in simpler terms, for example you can decide if you want to display products for a logged in user or not.

So what to start with? As blocks are the primitive building components for any layout in front end you can start overriding blocks. Later you can override the models, helpers, and controllers.

Great! But still not able to solve – how to override blocks? We are ready to serve the happiness of customizing.

In this article, we’ll make a custom module to comprehend how block overriding works in Magento. Well, overriding begins by setting up few XML format tags as per the conventions of Magento.

  • Blocks

The block class is Mage_Catalog_Block_Product and module’s class is Excellence_Test_Block_Catalog_Product. To make some changes we will override the block class with module class so that there is no need to change the core file. For this add few tags in config.xml file, as follow:

<blocks>

<catalog>

<rewrite>

<product>Excellence_Test_Block_Catalog_Product</product>

</rewrite>

</catalog>

</block>

We create a new class in our block folder at path Excellence/Test/Block/Catalog/Product.php

Whenever you create the product block’s object inside Magento, it will create object of the class instead of core class. Later you can add functions or update existing functions inside the defined class.

<?php

class Excellence_Test_Block_Catalog_Product extends Mage_Catalog_Block_Product

{

public function getPrice()

{

if(..some condition..)

{

return ..custom value..;

}

else

{

return parent::getPrice();

}

}

}

If noticed, the class has extended Mage_Catalog_Block_Product. It’s not essential to do same thing, but doing so benefits that all other functions of the main class are automatically inherited to our class. Hence it’s not essential to define all functions.

  • Models

Just like the block overriding, we require to override Mage_Catalog_Model_Product class. Just like blocks initially you need to add few tags in config.xml:

<models>

<catalog>

<rewrite>

<product>Excellence_Test_Model_Product</product>

</rewrite>

</catalog>

</models>

And the class method would be:

<?php

class Excellence_Test_Model_Product extends Mage_Catalog_Model_Product

{

public function getPrice()

{

if(..some condition..){

return ..some value..;

}else{

return parent::getPrice();

}

}

}

In between note that you can only override classes that Magento creates objects for. For instance, there are many abstract classes in Magento like Mage_Catalog_Block_Product_Abstract which cannot be overwritten.

  • Helpers

Similarly you can edit the config.xml for overriding Helpers.

<helpers>

<customer>

<rewrite>

<data>Excellence_Test_Helper_Data</data>

</rewrite>

</customer>

</helpers>

<?php

class Excellence_Test_Helper_Data extends Mage_Customer_Helper_Data

{

}

  • Controllers

Overriding controllers is a bit different way. In controllers you cannot override the class path instead you override the URL. Here you need to have the controller file which is to be extended.

So for overriding a controller:

<global>

<rewrite>

<test_cart> <!–This can be any unique id –>

<from><![CDATA[#^/checkout/cart/#]]></from> <!– the URL which u want to override–>

<to>/test/checkout_cart/</to> <!– destination url –>

</test_cart>

</rewrite>

</global>

Now create a controller at Excellence/Test/controllers/Checkout/CartController.php

<?php

require_once ‘Mage/Checkout/controllers/CartController.php’;

class Excellence_Test_Checkout_CartController extends Mage_Checkout_CartController{

}

Hope the article helped you. We would love to hear your comments or any queries in the feed below!

Leave a Reply