我希望属于属性集X的产品使用不同的模板,而不是逐个在所有产品中设置布局更新。
我在布局文件中创建了这个新句柄:
<product_view_bis>
<reference name="product.info">
<action method="setTemplate"><template>catalog/product/view7.phtml</template></action>
<block type="core/template" name="replacements" template="catalog/product/replacements.phtml">
<block type="replacements/replacement" name="replacement_options" template="catalog/product/replacement_options.phtml">
<action method="addOptionRenderer"><type>select</type><block>replacements/options</block><template>catalog/product/view/options/type/replacement.phtml</template></action>
</block>
</block>
</reference>
</product_view_bis>
然后我扩展了相关助手(Mage_Catalog_Helper_Product_View)以适应这个:
if ($product->getAttributeSetId()==X) $update->addHandle('product_view_bis');
这应该用我自己的文件替换catalog / product / view.phtml,该文件调用一些自定义块
根本不起作用。但如果我这样说:
<reference name="product.info">
<action method="setTemplate"><template>catalog/product/view7.phtml</template></action>
<block type="core/template" name="replacements" template="catalog/product/replacements.phtml">
<block type="replacements/replacement" name="replacement_options" template="catalog/product/replacement_options.phtml">
<action method="addOptionRenderer"><type>select</type><block>replacements/options</block><template>catalog/product/view/options/type/replacement.phtml</template></action>
</block>
</block>
</reference>
在后端的产品自定义布局更新中,它可以正常工作。
任何人都可以帮助我吗?
答案 0 :(得分:1)
我设法让它通过Observer
运行我在controller_action_layout_generate_blocks_after
上设置了一个事件观察者public function generateBlocksAfter($event)
{
$controller = $event->getAction();
//limit to the product view page
if($controller->getFullActionName() != 'catalog_product_view')
{
return;
}
$layout = $controller->getLayout();
$root = $layout->getBlock('root');
$product_info = $layout->getBlock('product.info');
if(!$product_info)
{
Mage::log('Could not find product.info block');
return;
}
$id = Mage::registry('current_product')->getId();
$prod = Mage::getModel('catalog/product')->load($id);
if ($prod->getAttributeSetId()==X) {
$product_info->setTemplate('catalog/product/view7.phtml');
$root->setTemplate('page/view7.phtml');
$replacement = $layout->createBlock('core/template')->setBlockAlias('replacements')->setTemplate('catalog/product/replacements.phtml')->setLayout($layout)->setNameInLayout('replacements');
$options = $layout->createBlock('replacements/replacement')->setBlockAlias('replacement_options')->setTemplate('catalog/product/replacement_options.phtml')->setLayout($layout)->setNameInLayout('replacement_options');
$options->addOptionRenderer('select','replacements/options','catalog/product/view/options/type/replacement.phtml');
$product_info->setChild('replacements',$replacement);
$replacement->setChild('replacement_options',$options);
}
}