我想根据产品所属的属性集创建不同的产品视图: Magento提供了一种方法吗?
- 更新 -
在dan.codes建议之后我添加了
$update->addHandle('PRODUCT_ATTRIBUTE_SET_ID_'.$product->getAttributeSetId());
到Mage_Catalog_ProductController(我复制了ProductController.php并将其放在local / Mage / Catalog / controllers /)中。
然后我将其添加到catalog.xml
<PRODUCT_ATTRIBUTE_SET_ID_9> // PRODUCT ID of Book Attribute Set
<label>Catalog Product View (Book)</label>
<reference name="product.info">
<block type="catalog/product_view_type_book" name="product.info.book" as="product_type_data" template="catalog/product/view/attribute_set/book.phtml">
<block type="core/text_list" name="product.info.book.extra" as="product_type_data_extra"/>
</block>
</reference>
</PRODUCT_ATTRIBUTE_SET_ID_9>
之后
<PRODUCT_TYPE_virtual translate="label" module="catalog">
<label>Catalog Product View (Virtual)</label>
<reference name="product.info">
<block type="catalog/product_view_type_virtual" name="product.info.virtual" as="product_type_data" template="catalog/product/view/type/virtual.phtml">
<block type="core/text_list" name="product.info.virtual.extra" as="product_type_data_extra"/>
</block>
</reference>
</PRODUCT_TYPE_virtual>
然后我创建了catalog / product / view / attribute_set / book.phtml,但它没有显示在我的产品视图页面中。
- 更新MAGENTO 1.5 -
我注意到处理程序更新已在最后一个Magento版本中移动。
$update->addHandle('PRODUCT_TYPE_'.$product->getTypeId());
$update->addHandle('PRODUCT_'.$product->getId());
现在在Mage / Catalog / Helper / Product / View.php中 我已经测试过,它仍然很有用!
答案 0 :(得分:4)
不,但是您可以扩展功能,方法是在Mage_Catalog_ProductController中扩展_initProductLayout方法,代码在此处
$update = $this->getLayout()->getUpdate();
$update->addHandle('default');
$this->addActionLayoutHandles();
$update->addHandle('PRODUCT_TYPE_'.$product->getTypeId());
$update->addHandle('PRODUCT_'.$product->getId());
你可以添加
$update->addHandle('PRODUCT_ATTRIBUTE_SET_ID_'.$product->getAttributeSetId());
然后在你的layout.xml中你可以有
<PRODUCT_ATTRIBUTE_SET_ID_IDHERE>
<reference name="root">
<action method="setTemplate"><template>template/path/here.html</template></action>
</reference>
</PRODUCT_ATTRIBUTE_SET_ID_IDHERE>
答案 1 :(得分:2)
如果您想根据属性集切换view.phtml,那么您需要执行以下操作:
<PRODUCT_ATTRIBUTE_SET_ID_9>
<label>Catalog Product View (Default)</label>
<reference name="product.info">
<action method="setTemplate"><template>catalog/product/custom-view.phtml</template></action>
</reference>
</PRODUCT_ATTRIBUTE_SET_ID_9>
只需将它添加到catalog.xml或local.xml中即可
希望这有帮助。
感谢
答案 2 :(得分:2)
有一个很好的教程:http://magebase.com/magento-tutorials/creating-custom-layout-handles/
这使用以下事件: controller_action_layout_load_before
为此,我在config.xml中设置了以下内容
<events>
<controller_action_layout_load_before>
<observers>
<mymodule>
<class>mymodule/observer</class>
<method>addAttributeSetHandle</method>
</mymodule>
</observers>
</controller_action_layout_load_before>
</events>
在Observer.php中我会有
public function addAttributeSetHandle(Varien_Event_Observer $observer)
{
$product = Mage::registry('current_product');
/**
* Return if it is not product page
*/
if (!$this->isBookProduct($product)) {
return;
}
$niceName = 'book';
/* @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);
}
}
}
protected function isBookProduct($product)
{
if (null === $product || !($product instanceof Mage_Catalog_Model_Product)) {
return false;
}
// TODO instead of hardcoded value we could use here something neat to get by name thru eav/entity_attribute_set model, some config value which hold that ID or use some other approach...
$book_set_id = 9;
if ($product->getAttributeSetId() != $book_set_id) {
return false;
}
return true;
}
这样可以在布局xml中使用:
<?xml version="1.0"?>
<layout version="0.1.0">
<PRODUCT_ATTRIBUTE_SET_book>
<reference name="product.info">
<action method="setTemplate">
<template>mymodule/book/product/view.phtml</template>
</action>
</reference>
</PRODUCT_ATTRIBUTE_SET_book>
</layout>