Extending and Overriding Magento Community extension

Magento provides option to develop extension, which can extend core features and perform some custom operations. This enables third party vendor to develop extension and distribute them on Magento Marketplace, they might have some price as well.

When we install those extension, we also look for way to update the extension, whenever there is an update available for the extension. At the same time, we also need to customize the community extension to meet our requirement or may be extend or override that extension functionalities.

To achieve this, we have 2 ways. Following are those.
1. Directly change the source of community extension. This has following pros and cons.
Pros
– Easier to implement changes. Meaning simply changes community extension source code.
    Cons
– Migration of extension will be difficult. Whenever there is an update for extension and you are up for it, you have to list down code changes and make those in new one.
2. Extend the community extension and build a local module.
    Pros
– Migration will be easier. However if there is huge change (example given below), you still need to customize your local module.
        E.g.  If upgraded extension, has changes the old class name to new one, then you need to update that object name in local module.
Cons
– You have to follow a convention to build a local module that extend community extension. Once you learned it, it will be easier for nexttime.

Here it goes, how to build local module that extend community extension.

Assumption:

  • Say community extension is “Oldcommunity“. And new local module name is “Custom_Extn” that will extend “Oldcommunity“.
  • All file path given below are relative to [Magento_Root]/app/code.
  • When I did for real project, it has actual meaning, but I can’t disclose that here, and to make it pretty simple, here is the purpose of change.
  • Purpose for extending the extension:
    • Precondition – Oldcommunity has a Model (Demo) and it has a method, say “getData()” which takes param ( $key ) and if key=”sample”, then return “samplevalue”.
    • Expectation: If key = “new_sample”, then value would be “new_sample_value”
      Since magento is very sensitive to casing of words, please take care of that as well.

Step – 1 :
In config.xml file (/local/Custom/Extn/etc/config.xml), add following lines.

 
<modules>
  <Custom_Extn>
    <version>1.0.0</version>
    <depends>
      <Oldcommunity />
    </depends>
  </Custom_Extn>
</modules>

Step – 2 :
Extend the model. For this you need to specify the same in above config.xml file.

<global>
   <models>
      <extn>
         <class>Custom_Extn_Model</class>
      </extn>
      <oldcommunity>
         <rewrite>
            <demo>Custom_Extn_Model_Demo</demo>
         </rewrite>
      </oldcommunity>
   </models>
</global>

Step – 3:
Create Demo.php in /local/Custom/Extn/Model/Demo.php.

<?php
require_once(Mage::getModuleDir('Model','Oldcommunity') . DS . 'Model' . DS . 'Demo.php'); // need to include original file here
class Custom_Extn_Model_Demo extends Oldcommunity_Model_Demo {
   public function getData($key){
      if($key == "new_sample")
         return "new_sample_value"; // injected new functionality here
      else
         return parent::getData($key); // retained older one here
   }
}

You are done with extension, you can deploy and test by instantiating Oldcommunity‘s Model (Demo) and pass param as “new_sample”, and you should get “new_sample_value”.

Similarly you can extend and override Blocks, Controllers etc…

I hope, this explains how to extend community extension. If you face any issue or face problem while implementing this, please feel free to drop a comment here.

Advertisements
Extending and Overriding Magento Community extension

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s