I want to create a second product view. The value of a custom attribute determines which template supposed to be used. Now I'm struggling about the best approach.

I've already overwritten the viewAction() of the ProductController. But I don't know how I can replace the template based on a custom_attribute. Could anyone please point me in the right direction?

Thank you so much for any help!

1 Answer 1


You can do this without changing core files, you need to create an observer that will create a new layout handle based on the attribute. Then you can create a layout update for that handle to change the template used for the product view page.

The following code is based on the article from http://magebase.com/magento-tutorials/creating-custom-layout-handles/

Module init file app/etc/modules/Example_AttributeHandle.xml

<?xml version="1.0" encoding="UTF-8"?>

Module configuration: app/code/local/Example/AttributeHandle/etc/config.xml

<?xml version="1.0" encoding="UTF-8"?>

Observer file: app/code/local/Example/AttributeHandle/Model/Observer.php

class Example_AttributeHandle_Model_Observer
     * Converts attribute value of current product to nice name ([a-z0-9_]+).
     * Adds layout handle PRODUCT_ATTRIBUTE_<attribute>_<value> after
     * PRODUCT_TYPE_<product_type_id> handle
     * Event: controller_action_layout_load_before
     * @param Varien_Event_Observer $observer
    public function addAttributeHandle(Varien_Event_Observer $observer)
        $product = Mage::registry('current_product');

         * Return if it is not product page
        if (!($product instanceof Mage_Catalog_Model_Product)) {

        $attributeName = 'myattribute';
         * Convert attribute value to alphanumeric + underscore string
        $niceName = str_replace('-', '_', $product->formatUrlKey($product->getData($attributeName)));

        /* @var $update Mage_Core_Model_Layout_Update */
        $update = $observer->getEvent()->getLayout()->getUpdate();
        $update->addHandle('PRODUCT_ATTRIBUTE_'.$attributeName.'_' . $niceName);

Replace the $attributeName value with the attribute that you would like to use. When the attribute is dropdown, it will use the numeric value of the selected option. Also this will not work for multiselect attributes.

Now you can change the product view template with the following code in your local.xml to select an other template when the product attribute myattribute has the value demotxt:

    <reference name="product.info">
      <action method="setTemplate"><template>my/custom/product/view.phtml</template></action>

Hope that this info is helpful, I did not had the option to test it completly.

  • Perfect answer :) If I had more upvotes to give I would Commented Nov 17, 2014 at 20:06
  • Works perfect! (After I added the layout update outside "catalog_product_view" in local.xml). Thank you so much!:)
    – Stefan
    Commented Nov 17, 2014 at 21:10
  • I've try above steps but my custom file not work. can you give path of local.xml file
    – Kailas
    Commented Oct 15, 2015 at 14:26

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.