13

Note: This is intended as a canonical question that fully explains how block rewrites work and can be used as duplicate target for more specific "How do I override block X" or "Why does my rewrite not work" questions.

See also: Looking for canonical questions about Magento 1 overrides

Let's say, I have to make changes to a core block class in a custom module (change methods or add methods). How do I do this, step by step?

3 Answers 3

25

Every block, or group of blocks is declared in the config.xml file of a module like this (inside the <global> tag).
Here is an example from the catalog module

    <blocks><!-- marks definition of a block group -->
        <catalog><!-- unique alias for blocks in the module -->
            <class>Mage_Catalog_Block</class><!-- class prefix for all blocks -->
        </catalog>
    </blocks>

This means that a block can be instantiated using the alias catalog/class_name_here where class_name_here is the rest of the class path starting from the prefix.
This means catalog/class_name_here will be mapped by default to Mage_Catalog_Block_Class_Name_Here.

To rewrite a block you need to create a module that depends on the module you are trying to change (Magento_Catalog) in my example.
And you need to add this in the config.xml under the <global> tag.

<blocks>
    <catalog><!-- alias of the block group you are rewriting -->
        <rewrite><!-- reserved tag: specify that you are rewriting something -->
             <class_name_here>YourNamespace_YourModule_Block_Your_New_Class_Here</class_name_here> <!-- tag: the rest of the alias of the class you are rewriting. value: the name of your class that rewrites the core class -->
        </rewrite>
    </catalog>
</blocks>

Then create the class YourNamespace_YourModule_Block_Your_New_Class_Here (following the ZF folder structure) and make this class extend the original class.

class YourNamespace_YourModule_Block_Your_New_Class_Here extends Mage_Catalog_Block_Class_Name_Here
{
    //your awesome code here
}

When you are done, disable compilation and enable it again (if needed) and clear the cache.

This will not work for abstract blocks.
It only works for classes that get instantiated.

Example

Let's assume that you want to rewrite the file app\code\core\Mage\Catalog\Block\Product\View\Options\Type\Select.php which has the class Mage_Catalog_Block_Product_View_Options_Type_Select in your own module Marius_Test.

Then you would need this entry in your config.xml:

<blocks>
    <catalog>
        <rewrite>
            <product_view_options_type_select>Marius_Test_Block_Catalog_Block_Product_View_Options_Type_Select</product_view_options_type_select>
        </rewrite>
    </catalog>
</blocks>

app\code\local\Marius\Test\Block\Catalog\Product\View\Options\Type\Select.php:

class Marius_Test_Block_Catalog_Product_View_Options_Type_Select extends Mage_Catalog_Block_Product_View_Options_Type_Select
{
    //your awesome code here
}
5
  • Does not work. I try to overwrite the class Mage_Catalog_Block_Product_View_Options_Type_Select in app\code\local\WR\EPO\Block\Catalog\Block\Product\View\Options\Type\Select.php. I tried it like this: codepen.io/anon/pen/WYOqBr
    – Black
    Nov 16, 2018 at 16:20
  • And if it doesn't work, you think that my answer is wrong so you downvote it instead of thinking that maybe you are doing something wrong. Anyway... replace this <Mage_Catalog_Block_Product_View_Options_Type_Select> WR_EPO_Block_Catalog_Block_Product_View_Options_Type_Select </Mage_Catalog_Block_Product_View_Options_Type_Select> with <product_view_options_type_select>WR_EPO_Block_Catalog_Block_Product_View_Options_Type_Select</product_view_options_type_select> and make sure that there are no spaces inside product_view_options_type_select
    – Marius
    Nov 16, 2018 at 17:00
  • I downvoted because your answer was not precise and even though I followed it step by step did not produced the right result. You wrote that we have to use the class name, so I used it and it did not worked. We have to use product_view_options_type_select instead of the real class name Mage_Catalog_Block_Product_View_Options_Type_Select. If you change your answer accordingly then I will upvote.
    – Black
    Nov 16, 2018 at 17:34
  • If you read it step by step you missed a step. The one where i explain what the class alias is. If you just copy paste replace you will get it working. 17 people understood. I think i explained it right
    – Marius
    Nov 16, 2018 at 17:48
  • Yes, but a good example is missing so we can assure that we were understanding your theory correctly
    – Black
    Nov 16, 2018 at 18:06
4

For My Point of view override and rewrite these two are different things ,

Override:

When we use design fallback mechanism then we are doing override

Rewrite:

When we rewrite magento Core classes in our class then we are doing rewrite.

1) Example of Override :

If i need to override app/code/core/Mage/Catalog/Block/Product/List.php file then I copy in my local module with same path shown below app/code/local/Mage/Catalog/Block/Product/List.php

This is not Suggested By magento But you can do that way.

2)Example Of Rewrite :

If I want to rewrite this block class Mage_Adminhtml_Block_Sales_Order_Create then I code in my module config.xml Like Below

    <global>
        <blocks>
            <adminhtml>
                <rewrite>

                    <sales_order_create>Trimantra_Smallchanges_Block_Adminhtml_Sales_Order_Create</sales_order_create>
                </rewrite>
            </adminhtml>
        </blocks>
    </global>

And In my class Trimantra_Smallchanges_Block_Adminhtml_Sales_Order_Create

I code Like Below

class Trimantra_Smallchanges_Block_Adminhtml_Sales_Order_Create extends Mage_Adminhtml_Block_Sales_Order_Create {
      My Function Or funcions That I want to Rewrite..
}
2

Important to add here is that block rewrites (as well as all other rewrites of Magento modules) imply the higher maintenance effort and therefore should be considered as a last chance to extend the functionality after configuration manipulation, events and theme customization.

Potential problem 1: Rewritten template will not get updated when you or other maintainer will update Magento's source files. Means the security fix or improvement will not get applied to your code. Same applies to other re-written classes including Blocks, but depends on how much of rewrite was done (see below).

Potential problem 2: Rewritten Block (or other class) might appear to be rewritten by another extension which you or another maintainer will try to install. Then you will have to resolve this conflict.

Alternative 1: Use events, i.e. dig through the code you are just about to rewrite and check if there are events that might be used to achieve the desired functionality.

Alternative 2: Rewrite smart, i.e. look around: maybe check the place where the class you are going to rewrite is instantiated and check if you can affect which class is picked via config or events; maybe there is a class around which allows you to override a 3-lines method to replace the classname instead of copying the 30 lines method from original class to the rewritten one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.