2

I am trying to set 2columns-right.phtml layout for products under specific category while the default is 1column.phtml.(Please note i don't want to change Category page layout but layouts of products under that category)

I tried Admin >Manage categories >Select Category >Custom design >Change "Apply to Products" to yes and in "Custom Layout Updates" entered

<reference name="product.info">
<action method="setTemplate"><template>page/2columns-right.phtml</template></action>
</reference>

Didn't work

Then i went to rwd/default/layout/catalog.xml and added after

<!--
    Product view
    -->

Entered

<CATEGORY_3>
<reference name="product.info">
<action method="setTemplate"><template>page/2columns-right.phtml</template></action>
    </reference>
    </CATEGORY_3>

But even this didn't work, It would be of great help any ideas on this. Magento CE Version 1.9.1

2
  • product can be in multiple categories Feb 23, 2017 at 7:27
  • yes but not in my case Feb 23, 2017 at 7:31

5 Answers 5

6

Create a small module

Your config.xml should looks like

app/code/codePool/Package/Extension/etc/config.xml

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <Package_Extension>
            <version>0.1.0</version>
        </Package_Extension>
    </modules>
    <global>
        <events>
            <controller_action_layout_load_before>
                <observers>
                    <categoryhandle>
                        <class>Package_Extension_Model_Observer</class>
                        <method>cateHandle</method>
                    </categoryhandle>
                </observers>
            </controller_action_layout_load_before>
        </events>
    </global>
</config>

Your observer should looks like

app/code/codePool/Package/Extension/Model/Observer.php

<?php
class Package_Extension_Model_Observer
{

    public function cateHandle(Varien_Event_Observer $observer)
    {
        $product = Mage::registry('current_product');

        /**
         * Return if it is not product page
         */
        if (!($product instanceof Mage_Catalog_Model_Product)) {
            return;
        }
        $category= 3;
        $categories=$product->getCategoryIds();
        if(in_array($category, $categories)){
            $update = $observer->getEvent()->getLayout()->getUpdate();
            $update->addHandle('PRODUCT_CATEGORY_'.$category);
        }

    }
}

And in your themes local.xml add below code or as you mention rwd/default/layout/catalog.xml add below code

<PRODUCT_CATEGORY_3>
    <reference name="root">
      <action method="setTemplate"><template>page/2columns-right.phtml</template></action>
    </reference>
</PRODUCT_CATEGORY_3>
1

Use controller_action_layout_generate_xml_before Event

<events>
    <controller_action_layout_generate_xml_before>
        <observers>
            <add_Customer_handler>
                <class><VENDOR_NAME>_<MODULE_NAME>_Model_Observer</class>
                <method>changeTemplate</method>
            </add_Customer_handler>
        </observers>
    </controller_action_layout_generate_xml_before>  
</events>

Observer

public function changeTemplate($observer)
{
    $action = $observer->getEvent()->getAction();
    $fullActionName = $action->getFullActionName();
    $category=<YOUR CATEGORY ID>;

    if($fullActionName=='catalog_product_view')
    {
      $product=Mage::getModel('catalog/product')->load(Mage::registry('current_product')->getId());
      $categories=$product->getCategoryIds()();

      if(in_array($category, $categories);){
        $layout = $observer->getEvent()->getLayout();
        $update = $layout->getUpdate();
        $xml = "<reference name='root'><action method='setTemplate'><template>page/2columns-right.phtml</template></action></reference>";
        $update->addUpdate($xml);
      }
    }
}
1

You can set layout for any specific category as per your requirement from admin.

Follow the steps:

Login to admin >> catalog >> Manage categories >> select your category >> custom design tab >> change page layout >> save category.

0

The following setting should work:

Go to Admin >Manage categories >Select Category >Custom design then select

  1. Page Layout to 2columns-right.phtml and
  2. In Custom Layout Update put the following code.

    <reference name="product.info">
      //put the child block here that you want call.
    </reference>
    
0

Found a much simpler solution, if you only need the change for a couple of products:

create a fancy_name.phtml file in your_theme/default/template/page/ (from copy of 1column-product.phtml or something).

Edit fancy_name.phtml until it becomes really fancy.

Go to magento backend, go to your product, go to design -> Custom layout update. Paste the following code and save:

<reference name="root">
    <action method="setTemplate"><template>page/fancy_name.phtml</template></action>
</reference>

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.