Magento class overloading, updateproof hack
Magento is a fantastic open source e-commerce platform. The system works and it is very easy to install but with young project like this one, the documentation is almost exclusively the forum.Thanks to the community you can find answers for the main problems everyone face.
Customization
Each Magento file contain a disclaimer at the top saying:
Do not edit or add to this file if you wish to upgrade Magento to newer versions in the future. If you wish to customize Magento for your needs please refer to http://www.magentocommerce.com for more information.
Why? Because if you hack straight into the core files, all your work will be wiped up at the next update…
After few hours browsing the forum I managed to find the solution: overloading. Magento allow you create a new class for example and to tell the core “Don’t use your class, use mine instead, it’s located there…”.
For this example we will overload the Catalog_Model_Convert_Adapter_Product class, in charge of a part of the import function. In the folder app you will find another folder called local. That’s where the custom code goes but first we need to tell Magento to go there. To do this we create a new module in the app/etc/modules folder.
A module is basically an xml file pointing to your module folder:
<?xml version="1.0"?>
<config>
<modules>
<moduleName_catalog>
<active>true</active>
<codePool>local</codePool>
</moduleName_catalog>
</modules>
</config>
You can paste this in a new file and call it something like “MyImport.xml“. The “_catalog” is the subfolder where the code for the module is.
Then in app/code/local/moduleName/catalog you place the files for the module. For my example I kept the same folder hierarchy as in the original core module. In the catalog folder you need a “etc” folder and a “Model” folder. In the etc folder goes the config file for the module: config.xml
<?xml version="1.0"?>
<config>
<global>
<models>
<catalog>
<rewrite>
<convert_adapter_product>CustomInport_Catalog_Model_Convert_Adapter_Product</convert_adapter_product>
</rewrite>
</catalog>
</models>
</global>
</config>
The final step is to create the Product.php file in moduleName/catalog/Module/Convert/Adapter. This file contain the code for the new classe, extended like the original one from Mage_Eav_Model_Convert_Adapter_Entity
class CustomInport_Catalog_Model_Convert_Adapter_Product
extends Mage_Eav_Model_Convert_Adapter_Entity
{
...
}
My new class is a copy of the existing one Catalog_Model_Convert_Adapter_Product but here I can rewrite the function the way I want, I know it wont disappear at the first update.
This particular case of overloading open many opportunities to customize the import process. For example setting default values for type, visibility, status or importing products categories from their names rather than ids.
Note: if the above technique doesn’t work and you have your cache system disabled, enable it and try again. The optio is situated in system>cache managment in your admin panel.
Benoit Gilloz
Programmer, Research and Development









