Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / All-Topics

Extending and Overriding Magento Community Extension

5.00/5 (1 vote)
18 Jan 2016Ms-PL2 min read 5K  
Extending and Overriding Magento Community Extension

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

When we install those extensions, we also look for a 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 those extension functionalities.

To achieve this, we have 2 ways which are as given below:

  1. Directly change the source of community extension. This has the 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 a 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 next time.

Here it goes, how to build local module that extends 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 the following lines:

XML
<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 the above config.xml file.

XML
<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
<?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 a problem while implementing this, please feel free to drop a comment here.

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)