Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Templates Magento:基于属性集的模板_Templates_Magento_Attributes_Product - Fatal编程技术网

Templates Magento:基于属性集的模板

Templates Magento:基于属性集的模板,templates,magento,attributes,product,Templates,Magento,Attributes,Product,我想根据产品所属的属性集创建不同的产品视图: Magento是否提供了这样做的方法 -更新- 下面是我添加的dan.codes建议 $update->addHandle('PRODUCT_ATTRIBUTE_SET_ID_'.$product->getAttributeSetId()); 到Mage_Catalog_ProductController(我复制了ProductController.php并将其放在local/Mage/Catalog/controllers/) 然后我

我想根据产品所属的属性集创建不同的产品视图: 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>
现在位于Mage/Catalog/Helper/Product/View.php中。

我已经测试过了,它仍然工作得很好

不,没有,但是您可以通过扩展Mage\u Catalog\u 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>

模板/path/here.html

如果要根据属性集切换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>

目录产品视图(默认)
目录/产品/自定义视图.phtml
只需将其添加到catalog.xml或local.xml中即可
希望这有帮助。

谢谢

这方面有一个很好的教程:

这将使用以下事件:控制器\u操作\u布局\u加载\u之前

为此,我在config.xml中设置了以下内容


mymodule/观察员
添加属性服务器
在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;
}
public function addattributessethandle(变量事件观察者$Observer)
{
$product=Mage::注册表(“当前产品”);
/**
*如果不是产品页,则返回
*/
如果(!$this->isBookProduct($product)){
返回;
}
$niceName='book';
/*@var$update Mage\u Core\u Model\u Layout\u update*/
$update=$observer
->getEvent()
->getLayout()
->getUpdate();
$handles=$update->getHandles();//将所有句柄存储在变量中
$update->resetHandles();//删除所有句柄
/**
*重新排列布局手柄以确保产品_
*最后添加句柄
*/
foreach($handles作为$handle处理){
$update->addHandle($handle);
如果($handle=='PRODUCT\u TYPE.'$PRODUCT->getTypeId()){
$update->addHandle('PRODUCT\u ATTRIBUTE\u SET.'$niceName);
}
}
}
受保护功能isBookProduct($product)
{
if(null==$product | |!($product instanceof Mage_Catalog_Model|U product)){
返回false;
}
//TODO,而不是硬编码的值,我们可以在这里通过eav/entity\u attribute\u set model,一些保存该ID的配置值,或者使用其他方法,通过名称获得一些整洁的值。。。
$book\u set\u id=9;
如果($product->getAttributeSetId()!=$book\u set\u id){
返回false;
}
返回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>

mymodule/book/product/view.phtml

好的,你的建议听起来不错,但我只是想申请,但没有成功。我将ProductController.php从core/Mage/Catalog/controllers复制到local/Mage/Catalog/controllers。然后我试图插入新的布局,但什么也没发生。我犯了一些错误吗?你能用你所做的更新你的帖子吗?你把它放进了什么xml文件,不是新的吧?仅供参考,代码在Magento 1.5中的位置不同。我已经更新了我的帖子,以调整您的代码以适应更改。如果其他人无意中发现了这一点,我们在博客上有一个完整的教程,它使用事件观察者来实现相同的目标。这对我很有用,也许是你在你的街区所做的,只是为了确保代码正常工作,请更改xml以引用根,并将模板设置为其他内容,然后查看页面是否发生了更改`
<events>
    <controller_action_layout_load_before>
        <observers>
            <mymodule>
                <class>mymodule/observer</class>
                <method>addAttributeSetHandle</method>
            </mymodule>
        </observers>
    </controller_action_layout_load_before>
</events>
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 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>