Home » Magento9 June 2010

Magento: Rewrite/Override Block Controller Model Helper

This article will show how you can override/rewrite Magento Block, Controller, Model and Helper files. We will be dealing with the config XML files and the class files to override.

We override Magento core classes to update/modify the core functionalities according to our need. We can directly makes changes in the core Magento classes but doing so will hamper us when we upgrade the Magento version. All our changes will go away then. Because, when we upgrade Magento, the new core files will replace the old core files which we had changed.

But when we override the Magento core class files, all changes will be done in our local files and the core files remain intact. So, this will not do affect our changes when we upgrade Magento.

Block Override

Scenario: I want to display 3 products per page in product listing.

For this we need to use setPageSize function.

Mage_Catalog_Block_Product_List class is responsible for getting product items in product listing page.

I will override this Block class with my module’s class MyNamespace_MyModule_Block_Product_List. So, the path of class MyNamespace_MyModule_Block_Product_List is MyNamespace/MyModule/Block/Product/List.php

Here is the xml code which is to be written in etc/config.xml for my custom module.

MyNamespace_MyModule_Block_Product_List class.
I have used setPageSize(3). This means that, 3 products are displayed per page.

Controller Override

Scenario: I want to display products of ‘Living Room’ category to logged in customer only. When guest users try to view the ‘Living Room’ category, they should be redirected to login page.

For this I need to override the Mage_Catalog_CategoryController class.

config.xml

There are 2 ways to write the xml code for controller override. You can use any one of the following:-

The First way

The Second way

Now, you need to create a controller class MyNamespace_MyModule_Catalog_CategoryController
The path for the controller file will be MyNamespace/MyModule/Catalog/CategoryController.php

Model Override

Scenario: I want to add some text e.g. ‘Product:’ in front of every product name. And, I want fixed price for every product e.g. 100.

For this you need to override Mage_Catalog_Model_Product class.

config.xml

Now, you need to create a model class MyNamespace_MyModule_Model_Catalog_Product
The path for the model file will be MyNamespace/MyModule/Model/Catalog/Product.php

Helper Override

Scenario: When any logged in customer reviews any product, his/her review (after admin approval) can be viewed from My Account –> My Product Reviews. In this page, you will see list of reviews done by the customer. The review detail is truncated to 50 characters. I want to display more. I want to truncate to 100 characters and display it.

For this you need to override Mage_Review_Helper_Data class.

config.xml

Now, you need to create a helper class MyNamespace_MyModule_Helper_Review_Data
The path for the helper file will be MyNamespace/MyModule/Helper/Review/Data.php

Hope this helps. Thanks.

Magento

Get New Post by Email

Find me on

FacebookTwitterGoogle+LinkedInRSS Feed
  • Pingback: Tweets that mention Magento: Block Controller Model Helper Override | Mukesh Chapagain's Blog -- Topsy.com()

  • Pingback: Social Software Development()

  • Pingback: RONALD()

  • Darren

    Your global ending tags are missing a forward slash.

    I’m having trouble getting the controller class overrides to work. I’m trying to add another product view action. I assume I can just replace where it says category with product.

    Has anyone had success doing it this way? Method 1 gives me a 404 error and method 2 didn’t change anything. I’d prefer to do it the method 1 way.

  • Rohit Avasthi

    Hi,

    I have override saveAction() method of catalog/product controller successfully. Now i want to call another action method created by me in my overridden controller say delimgAction(), i got success in loading controller but it is not calling my action method, it redirects to the home page do you know why this happens. Or how to call my action method?

    My router is as bellow:

    admin

    Cap_ColorSwitch
    colorswitch

    Thank you

  • Pingback: Dagblastit » Blog Archive » Fix core Magento in an upgrade-safe way - Rants from a Web developer, musician, dad()

  • Hi,

    In your blog there are lots of helpful thing in magento I am really thankfull of you.

    I am integrating paypal with external web service module in magento.
    and i have to insert paypal express method response into web service database tables.

    I am new in magento and i am not able to do that.

    So please help me on it.
    ajay_mca07026@yahoo.in(This is my email address. On it you can reply)

    Thank You.

  • Hi,

    In your blog there are lots of helpful thing in magento I am really thankfull of you.

    I am integrating paypal with external web service module in magento.
    and i have to insert paypal express method response into web service database tables.

    I am new in magento and i am not able to do that.

    So please help me on it.
    ajay_mca07026@yahoo.in(This is my email address. On it you can reply)

    Thank You.

  • Steward

    “This article will show how you can override Magento Block, Controller, Model and Helper files.”

    Not really.
    It would be a great help if I needed to do something similar.
    It gives a good example.

  • Kamal Joshi

    Can you please explain how have u override productcontroller’s saveAction(), I also want to override. TIA

  • Vishal Sanwar

    it;s article   is very  help ful for us, but i have a problem
    :::

    in admin pannel goes to system->permission->user->add new user
    in these it show user,user role
    but
    i want  to append third tab i.e. user business profile
    i have to done these to show the business profile and corresponding filels

    —————->but in database it didnt store

    table can bt made in DB

    these is block overriding

    plz solve these problem

    but in database it didnt store

  • Vishal Sanwar

    hey rohit,how to override saveAction() method.plz give me details ………..

  • Pingback: The 54 Most Useful Magento Articles of All Time « Custom Magento Development – Collins Harper()

  • Khamar_pratik

    Thanx Darren…

  • Khamar_pratik

    Anybody Override Controller I geting Problem…Pls Help…

  • Jasper

    Hi,
    This is the knowledge i need, thanks

  • Chetan

    Controller overriding is not working for me

    I have set this code in global section of config.xml file

    /test/catalog_category/

    Please solve my issue

  • Pingback: The 54 Most Useful Magento Articles of All Time | @nexcess()