Specific view page for products (product info) from specific category in Magento 1.9
-
16-02-2021 - |
Question
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:
- 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!
- 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 :)
Solution
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');
}
}
}