Custom layout based on the attribute set not working
-
12-12-2019 - |
Question
I've followed this tutorial to add attribute set
specific product page layout updates, but it doesn't work.
Here's my code:
file: app\code\local\Magebase\AttributeSetHandle\etc\config.xml
Code:
<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<Magebase_AttributeSetHandle>
<version>0.1.0</version>
</Magebase_AttributeSetHandle>
</modules>
<global>
<events>
<controller_action_layout_load_before>
<observers>
<attributesethandle>
<class>Magebase_AttributeSetHandle_Model_Observer</class>
<method>addAttributeSetHandle</method>
</attributesethandle>
</observers>
</controller_action_layout_load_before>
</events>
</global>
</config>
File: app\code\local\Magebase\AttributeSetHandle\Model\Observer.php
CODE:
<?php
class Magebase_AttributeSetHandle_Model_Observer
{
/**
* Converts attribute set name of current product to nice name ([a-z0-9_]+).
* Adds layout handle PRODUCT_ATTRIBUTE_SET_<attribute_set_nicename> after
* PRODUCT_TYPE_<product_type_id> handle
*
* Event: controller_action_layout_load_before
*
* @param Varien_Event_Observer $observer
*/
public function addAttributeSetHandle(Varien_Event_Observer $observer)
{
$product = Mage::registry('current_product');
/**
* Return if it is not product page
*/
if (!($product instanceof Mage_Catalog_Model_Product)) {
return;
}
$attributeSet = Mage::getModel('eav/entity_attribute_set')->load($product->getAttributeSetId());
/**
* Convert attribute set name to alphanumeric + underscore string
*/
$niceName = str_replace('-', '_', $product->formatUrlKey($attributeSet->getAttributeSetName()));
/* @var $update Mage_Core_Model_Layout_Update */
$update = $observer->getEvent()->getLayout()->getUpdate();
$handles = $update->getHandles(); // Store all handles in a variable
$update->resetHandles(); // Remove all handles
/**
* Rearrange layout handles to ensure PRODUCT_<product_id>
* handle is added last
*/
foreach ($handles as $handle) {
$update->addHandle($handle);
if ($handle == 'PRODUCT_TYPE_' . $product->getTypeId()) {
$update->addHandle('PRODUCT_ATTRIBUTE_SET_' . $niceName);
}
}
}
}
In the local.xml
inside the <layout></layout>
i've added:
<PRODUCT_ATTRIBUTE_SET_Informatica>
<reference name="product.info">
<action method="setTemplate"><template>catalog/product/view-informatica.phtml</template></action>
</reference>
</PRODUCT_ATTRIBUTE_SET_Informatica>
My attribute set is named Informatica
and the customized view.phtml is located in design/frontend/[my-custom-tample]/default/template/catalog/product
What am I missing here?
Please help :)
Thanks guys,
Solution
Hi first need debug your current page handler
by below code:
Zend_Debug::dump(Mage::app()->getLayout()->getUpdate()->getHandles());
I have modify code and add the handler only for product view page
<?php
class Magebase_AttributeSetHandle_Model_Observer
{
public function addAttributeSetHandle(Varien_Event_Observer $observer)
{
if($observer->getEvent()->getAction()->getFullActionName()=='catalog_product_view'){
$product = Mage::registry('current_product');
if($product):
$layout = $observer->getEvent()->getLayout();
$attributeSet = Mage::getModel('eav/entity_attribute_set')->load($product->getAttributeSetId());
$handle = str_replace('-', '_', $product->formatUrlKey($attributeSet->getAttributeSetName()));
$layout->getUpdate()->addHandle('PRODUCT_ATTRIBUTE_SET_'.$handle);
// check all Handler
//Zend_Debug::dump($layout->getUpdate()->getHandles());
endif;
}
return ;
}
}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange