I am using magento

I am working on functionality in which I need to change Product Image Dynamically from third Part extension, It can be easily done using overriding media.phtml file.

But I am seeking for another method that can override block Mage_Catalog_Block_Product_View_Media or something like that can avoid me to change or interact with .phtml files.


Actually, I want to give more about my Question, actually I want to change on Product Detail page without any changes in template section, or without adding or overriding any phtml file.

please let me know, Is this possible? if yes, then,Iin what way i can do this?

1 Answer 1


You need to change the template dynamically. For this you can observe the event controller_action_layout_generate_blocks_after and your observer should somewhat look like this.

class Magento_Study_Model_Observer
    const NEW_MEDIA_TEMPALTE = 'your/new/media/template.phtml';

    public function changeProductImageTemplate(Varien_Event_Observer $observer)
        //get layout object from event
        $layout = $observer->getEvent()->getLayout();

        //make sure it is a product page or category page
        if (Mage::registry('current_product') != ''
            || Mage::registry('current_category') != ''
        ) { 

            //changing template

        return $this;

As you can see, we are first make sure we are inside a product page. Then we grab the media block and assign a new template to it. Now all you need to do is change the constant NEW_MEDIA_TEMPLATE with your custom media template file path and then define that file and do modification there.

To find complete code reference, you can refer this answer.


This is the layout update xml way of doing this operation

        <reference name="product.info.media">
            <action method="setTemplate">

        <reference name="product.info.media">
            <action method="setTemplate">
  • thanks @Rajeev K Tomy but I dont want to set any template file for this purpose, I just want to override the block or models, is that possible? what is your view on this? Sep 22, 2015 at 7:01
  • @amit_game rewriting should be avoided if an event observer is available to do this. In your question you are saying you want to customize media.phtml file but without touching the original file. The solution to this use this event or use a layout udpate xml file to overwrite the template (I will put this right now as an edit). But if you want to customize the behaviour of the class Mage_Catalog_Block_Product_View_Media, you need to rewrite that block class Sep 22, 2015 at 7:05
  • @amit_game see my edit Sep 22, 2015 at 7:14
  • Sorry it may be my fault, but I dont want to use phtml to change my images on Product detail page? is that possible ? Sep 22, 2015 at 7:31
  • @amit_game sorry I didnt get you. then what are you trying to achieve then ? Sep 22, 2015 at 7:33

Your Answer

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

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