0

We devided all the products into two categories:

  • "Main products"
  • "Accessories"

All listed products from the category "Main products" are opened with our custom template: catalog/product/view_main_producs.phtml

Here it comes the tricky part :)

All products from the category "Accessories" needs to be opened with different custom template catalog/product/view_accessories.phtml, because the products from this category have different attributes.

In some way we managed (partly) to acchive this with the following lines of code:

<reference name="product.info">
<action method="setTemplate"> <template>catalog/product/view_accessories.phtml</template></action>
</reference>

We inserted the above code into "Accessories" category Custom Design -> Custom Layout Update

With the above code we managed to open products from category "Accessories" into on our custom view_accessories.phtml template!

Here is what is the problem. Above code only works if a person came to our web site through the links on our web site.

Now we have two issues:

  1. If person come to our web site and search for accessories products (search toolbar), the product will not be opened in view_accessories.phtml template!
  2. If person click on the products while searching for it on google, bing (SEO search), the product will not be opend in "view_accessories.phtml"!

Our question is: how to achieve for all products from "accessories" category to be opened in "view_acccessories.phtml" - no matter how come the person access the link?

Thanks in advenced :)

1

Try following way:

app/etc/modules/SR_MagentoCommunity.xml


<?xml version="1.0"?>
<config>
    <modules>
        <SR_MagentoCommunity>
            <active>true</active>
            <codePool>local</codePool>
        </SR_MagentoCommunity>
    </modules>
</config>

app/code/local/SR/MagentoCommunity/etc/config.xml


<?xml version="1.0"?>
<config>
    <modules>
        <SR_MagentoCommunity>
            <version>0.0.0.1</version>
        </SR_MagentoCommunity>
    </modules>
    <global>
        <models>
            <sr_magentocommunity>
                <class>SR_MagentoCommunity_Model</class>
            </sr_magentocommunity>
        </models>
    </global>
    <frontend>
        <events>
            <controller_action_layout_render_before_catalog_product_view>
                <observers>
                    <sr_magentocommunity_controller_action_layout_render_before_catalog_product_view>
                        <type>singleton</type>
                        <class>sr_magentocommunity/observer</class>
                        <method>controllerActionLayoutRenderBeforeCatalogProductView</method>
                    </sr_magentocommunity_controller_action_layout_render_before_catalog_product_view>
                </observers>
            </controller_action_layout_render_before_catalog_product_view>
        </events>
    </frontend>
</config>

app/code/local/SR/MagentoCommunity/Model/Observer.php


<?php

class SR_MagentoCommunity_Model_Observer
{

    public function controllerActionLayoutRenderBeforeCatalogProductView(Varien_Event_Observer $observer)
    {
        $layout = Mage::getSingleton('core/layout');
        $request = Mage::app()->getRequest();
        $productId  = (int) $request->getParam('id');
        /** @var Mage_Catalog_Model_Product $product */
        $product = Mage::getModel('catalog/product')
            ->setStoreId(Mage::app()->getStore()->getId())
            ->load($productId);
        $categoryIds = $product->getCategoryIds();
        // suppose 14 is your category id
        if (in_array(14, $categoryIds)) {
            $layout->getBlock('product.info')->setTemplate('catalog/product/view_accessories.phtml');
        }
    }
}
  • Dear Sohel Rana, your are my Idol :) This just works perfect. Thank you 1000 times. – user15151 Feb 3 at 14:42
  • 1
    Great to know. Happy to help always. – Sohel Rana Feb 3 at 15:22
  • thanks again :) – user15151 Sep 5 at 18:03
  • hi Sohel Rana, I encountered to some issue regarding similar functionality. Would you please be so kind to look at the question at the link: magento.stackexchange.com/questions/291847/… – user15151 Oct 8 at 8:46

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.