Magento: Override Models

You can override model in Magento in following way.
Steps to override Model in Magento:
1) When we override the model in the module, we should not change the core model files directly. To do this, we add following xml in config.xml inside our custom module:
<config>
  <global>
    <models>
      <catalog>
        <rewrite>
          <product>Mynamespace_Catalog_Model_Product</product>
        </rewrite>
      </catalog>
      <product_resource>
        <rewrite>
          <product>Mynamespace_Catalog_Model_Resource_Product</product>
          <product_collection>Mynamespace_Catalog_Model_Resource_Product_Collection</product_collection>
        </rewrite>
      </product_resource>
    </models>
  </global>
</config>

 

2) We create a new custom class in our model folder in our custom module.
For Model to be overrided: app/code/local/Mynamespace/Catalog/Model/Product.php
<?php
class Mynamespace_Catalog_Model_Product extends Mage_Catalog_Model_Product
{
}

 

For Resource Model to be overrided: app/code/local/Mynamespace/Catalog/Model/Resource/Product.php
<?php
class Mynamespace_Catalog_Model_Resource_Product extends Mage_Catalog_Model_Resource_Product
{
}

For Collection Model to be overrided: app/code/local/Mynamespace/Catalog/Model/Resource/Product/Collection.php
<?php
class Mynamespace_Catalog_Model_Resource_Product_Collection extends Mage_Catalog_Model_Resource_Product_Collection
{
}

Thats’s it! You have overrided the core models.
Note: We can only override the classes for which objects can be created. We can not override the abstract classes for which no object is created.

Leave a Reply