PHP Magento Tips

How to Override Magento Core Catalog Product Model, ResourceModel, Blocks and Helper ?

Rate this posting:
{[['']]}

One of the best Awesome thing in Magento is change functionality of Core files and its really very easy. Lets actually get our hands on it. Lets do this. Now here we talk about override Catalog Product Model.

In this tutorial I will show you something very very easy and fast method. First create a file test.php in your Magento root and add below code.

<?php 
    include 'app/Mage.php';
    umask(0);
    Mage::app();
    
    $object = Mage::getModel('catalog/product')->load(864);
    
    var_dump( $object->getName() );
    echo get_class($object); 

This is a nice little Magento testing script. Here we just included app/Mage.php Which is exactly a index file and it does well. Instead of calling Mage::run() I am calling Mage::app(), the base script for instantiating Magento application and some basic setup. Its not a full load and full instantiation, I believe that setup script will not run. Here, umask(0) is for interacting the user with the file system. But it is not compulsory.

If you will run above script in your browser then you will find Mage_Catalog_Model_Product class.

Now, We want to change this and rewrite Mage_Catalog_Model_Product class. And also we write our code to make each and every product name in uppercase.

Step 1:

In  app/etc/modules/First_Module.xml add below code: 

<?xml version="1.0" encoding="UTF-8"?>
<config>
  <modules>
    <First_Module><!-- app/code/local/First/Module -->
      <active>true</active>
      <codePool>local</codePool>
    </First_Module>
  </modules>
</config>

Step 2:

In app/code/local/First/Module/etc/config.xml add below code: 

Note: Before adding below code check app/code/core/Mage/Catalog/etc/config.xml and find <global><models><catalog> node inside it.

<?xml version="1.0" encoding="UTF-8"?>
<config>
<!--  <global>
        <models>
            <catalog>
                <class>Mage_Catalog_Model</class>
            </catalog>
-->
          
 <global>
        <models>
            <catalog>
                <rewrite>
                    <product>First_Module_Model_Product</product>
                </rewrite>               
            </catalog>
         </models>
  </global>
</config> 

Step 3:

In app/code/local/First/Module/Model/Product.php add below code:


<?php

class First_Module_Model_Product extends Mage_Catalog_Model_Product {
    public function getName(){
        $name = parent::getName();
        return strtoupper($name);
    }
}

 Now clear your cache and check your test.php page in your browser. you will find your First_Module_Model_Product class.

Now to Override Blocks made some changes in test.php as per below:

<?php 
    include 'app/Mage.php';
    umask(0);
    Mage::app();
    
    //$object = Mage::getModel('catalog/product')->load(864);
    
    //var_dump( $object->getName() );
    $object = Mage::app()->getLayout()->createBlock('catalog/product_view');
    echo get_class($object); 

In app/code/local/First/Module/etc/config.xml Modified with below code:

<?xml version="1.0" encoding="UTF-8"?>
<config>
<!--  <global>
        <models>
            <catalog>
                <class>Mage_Catalog_Model</class>
            </catalog>
-->
          
 <global>
         <blocks>
             <catalog>
                 <rewrite>
                     <product_view>First_Module_Block_Product_View</product_view>
                 </rewrite>
             </catalog>
         </blocks>
        <models>
            <catalog>
                <rewrite>
                    <product>First_Module_Model_Product</product>
                </rewrite>               
            </catalog>
         </models>
  </global>
</config> 

In app/code/local/First/Module/Block/Product/View.php add below code:

<?php 

class First_Module_Block_Product_View extends Mage_Catalog_Block_Product_View
{
    
} 

Now clear your cache and check test.php page in your browser. you will find First_Module_Block_Product_View.

** Note: To Override adminhtml Block made below changes in config.xml file.
Suppose we want to override app/code/core/Mage/Adminhtml/Block/Customer/Form/Element/File.php. Made below change in your config.xml file.

<blocks>
        <adminhtml>
            <rewrite>
        <customer_form_element_file>First_Module_Block_Adminhtml_Customer_Form_Element_File</customer_form_element_file>
            </rewrite>
        </adminhtml>
    </blocks>


After Create file in app/code/local/First/Module/Block/Adminhtml/Customer/Form/Element/File.php and add below code in it.

<?php

class First_Module_Block_Adminhtml_Customer_Form_Element_File extends Mage_Adminhtml_Block_Customer_Form_Element_File
{
   // create method to override.
}


Now to override ResourceModel made some changes in test.php as per below:

<?php 
    include 'app/Mage.php';
    umask(0);
    Mage::app();
    
    //$object = Mage::getModel('catalog/product')->load(864);
    
    //var_dump( $object->getName() );
    //$object = Mage::app()->getLayout()->createBlock('catalog/product_view');
    
    $object = Mage::getResourceModel('catalog/product');
    echo get_class($object); 
In app/code/local/First/Module/etc/config.xml add below code:

<?xml version="1.0" encoding="UTF-8"?>
<config>
<!--  <global>
        <models>
            <catalog>
                <class>Mage_Catalog_Model</class>
            </catalog>
-->
          
 <global>
         <blocks>
             <catalog>
                 <rewrite>
                     <product_view>First_Module_Block_Product_View</product_view>
                 </rewrite>
             </catalog>
         </blocks>
        <models>
            <catalog>
                <rewrite>
                    <product>First_Module_Model_Product</product>
                </rewrite>               
            </catalog>
            <catalog_resource>
                <rewrite>
                    <product>First_Module_Model_Resource_Product</product>
                </rewrite>
            </catalog_resource>
         </models>
  </global>
</config> 

In app/code/local/First/Module/Model/Resource/Product.php add below code:


<?php 

class First_Module_Model_Resource_Product extends Mage_Catalog_Model_Resource_Product
{
}

Now clear your cache and check test.php page in your browser. You will find First_Module_Model_Resource_Product.

Now to override Helper made some changes in test.php as per below

<?php 
    include 'app/Mage.php';
    umask(0);
    Mage::app();
    
    //$object = Mage::getModel('catalog/product')->load(864);
    
    //var_dump( $object->getName() );
    //$object = Mage::app()->getLayout()->createBlock('catalog/product_view');
    
    //$object = Mage::getResourceModel('catalog/product');
    $object = Mage::helper('sales/data');
    //$object = Mage::helper('customer/address');
    echo get_class($object); 

In app/code/local/First/Module/etc/config.xml add below code:

<?xml version="1.0" encoding="UTF-8"?>
<config>
<!--  <global>
        <models>
            <catalog>
                <class>Mage_Catalog_Model</class>
            </catalog>
-->
          
 <global>
         <blocks>
             <catalog>
                 <rewrite>
                     <product_view>First_Module_Block_Product_View</product_view>
                 </rewrite>
             </catalog>
         </blocks>
         <helpers>
             <customer>
                 <rewrite>
                     <address>First_Module_Helper_Address</address>
                 </rewrite>
             </customer>
            <sales>
                <rewrite>
                    <data>First_Module_Helper_Data</data>
                </rewrite>
            </sales>
        </helpers>
        <models>
            <catalog>
                <rewrite>
                    <product>First_Module_Model_Product</product>
                </rewrite>               
            </catalog>
            <catalog_resource>
                <rewrite>
                    <product>First_Module_Model_Resource_Product</product>
                </rewrite>
            </catalog_resource>
         </models>
  </global>
</config> 

In app/code/local/First/Module/Helper/Data.php add below code:

<?php 

class First_Module_Helper_Data extends Mage_Sales_Helper_Data {
    
} 

In app/code/local/First/Module/Helper/Address.php add below code:

<?php 

class First_Module_Helper_Address extends Mage_Customer_Helper_Address {
    
} 
Now clear your cache and check test.php page in your browser. You will find First_Module_Helper_Data. Also check by commenting/uncommenting line no. 12 and 13 in test.php
Tag : Magento
0 Comments On "How to Override Magento Core Catalog Product Model, ResourceModel, Blocks and Helper ?"

Back To Top