Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Examples: Override Magento Blocks, Models & controllers

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 472
    Comment on it

    Hello Readers,
    This tutorial will guide you about overriding mage core file using config.xml. Here i am gonna give an examples which guide you about "how to override magento blocks, Models & controllers. Always remember, it is not a good habit if core files are modified.
    Location of config file: "app/code/local/Namespace/Module/etc/config.xml"

    Example: Override Magento's Blocks

     <config>
         <global>
              <blocks>
                   <catalog>
                    <rewrite>
                     <view>Namespace_Module_Block_View</view>
                    </rewrite>
                   </catalog>
            </blocks>
         </global>
       </config>

    <catalog> and <rewrite> node tells magento that we're going to override 'blocks' of 'Catalog' core module.

    Now, create a block file "app/code/local/Namespace/Module/block/View.php"

        class Namespace_Module_Block_View extends Mage_Catalog_Block_Category_View
        {
            public function getProductListHtml()
              {
            return $this>getChildHtml('product_list');
              }
        }

    Example: Override Magento Models
      

      <config>
          <global>
         <models>
               <checkout>
                <rewrite>
                     <cart>Namespace_Module_Model_Cart</cart>
                </rewrite>
               </checkout>
         </models>
         </global>
        </config>

    Now, create a Model file "app/code/local/Namespace/Module/Model/Cart.php" and paste the following code.
     

    
       class Namespace_Module_Model_Cart extends Mage_Checkout_Model_Cart
        {
            // override code in new class
        }

    <checkout> and <rewrite> node tells magento that we're going to override 'model' of 'Checkout' core module.

    Example:  Override Magento's Controllers:
    In this example we are overidding "Cart" controller class of the core "checkout" module

        <config>
         <frontend>
            <routers>
                    <checkout>
                        <args>
                            <modules>
                                <Namespace_Module before="Mage_Checkout">Namespace_Module</Namespace_Module>
                            </modules>
                        </args>
                    </checkout>
                </routers>
         </frontend>
        </config>

    <Namespace_Module before="Mage_Checkout">Namespace_Module</Namespace_Module> tag tell Magento that firstly load our custom module's controller files before the mage_checkout core files.
    Now, create a controller file "app/code/local/Namespace/Module/controllers/CartController.php" and paste the following contents in that file.

        <?php
        require_once 'Mage/Checkout/controllers/CartController.php';
        class Namespace_Module_CartController extends Mage_Checkout_CartController
        {
         public function indexAction()
        {
             // override code
        }
        }

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: