0

I am using magento 1.9.2.1

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.

EDITED

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?

2

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.

<?php
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
            $layout->getBlock('product.info.media')
                ->setTemplate(self::NEW_MEDIA_TEMPALTE);
        }

        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.

EDIT

This is the layout update xml way of doing this operation

<layout>
    <catalog_category_view>
        <reference name="product.info.media">
            <action method="setTemplate">
                <template>path/to/your/custom/media.phtml</template>
            </action>
        </reference>
    </catalog_category_view>

    <catalog_product_view>
        <reference name="product.info.media">
            <action method="setTemplate">
                <template>path/to/your/custom/media.phtml</template>
            </action>
        </reference>
    </catalog_product_view>
</layout>
  • 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? – amit_game Sep 22 '15 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 – Rajeev K Tomy Sep 22 '15 at 7:05
  • @amit_game see my edit – Rajeev K Tomy Sep 22 '15 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 ? – amit_game Sep 22 '15 at 7:31
  • @amit_game sorry I didnt get you. then what are you trying to achieve then ? – Rajeev K Tomy Sep 22 '15 at 7:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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