Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/magento/5.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
Magento:在body类中显示自定义属性_Magento_Magento 1.7 - Fatal编程技术网

Magento:在body类中显示自定义属性

Magento:在body类中显示自定义属性,magento,magento-1.7,Magento,Magento 1.7,我有一个自定义的category属性,我想添加到body类中。据我所知,人们所做的是 覆盖CategoryController并添加类似于$root->addBodyClass($category->getMyAttribute())的内容但我不想覆盖核心类 在管理面板中,他们不使用属性本身而是直接添加类,而是向每个类别添加类似于caravan motorhome lighting的内容。因为我已经有了一个属性,我当然不想克隆它并以这种方式添加类 所以我最喜欢的解决方案是一些布局更新,我可以添加

我有一个自定义的category属性,我想添加到body类中。据我所知,人们所做的是

  • 覆盖CategoryController并添加类似于
    $root->addBodyClass($category->getMyAttribute())的内容但我不想覆盖核心类

  • 在管理面板中,他们不使用属性本身而是直接添加类,而是向每个类别添加类似于
    caravan motorhome lighting
    的内容。因为我已经有了一个属性,我当然不想克隆它并以这种方式添加类

  • 所以我最喜欢的解决方案是一些布局更新,我可以添加到local.xml中

    <reference name=”root”>
        <action method=”addBodyClass”>
            <className>
                get value of my custom attribute here dynamically
            </className>
        </action>
    </reference>
    
    
    在此处动态获取自定义属性的值
    

    有人知道这是怎么回事吗?或者有人知道我没有想到的其他想法吗?

    您可以使用Magento layout XML的一个非常酷的功能来实现这一点。你需要一个模块来实现它。或者专门为此创建一个模块,或者使用一个主题模块(如果有的话)——这取决于你自己决定什么是最好的

    我将向您展示一个示例,其中我将向body标记添加一个包含类别ID的类:

    在我的布局XML中,我将通过
    catalog\u category\u default
    句柄添加。这样,我可以在以后使用
    Mage::registry('current_category')
    检索当前类别。因此,在布局XML中,请执行类似的操作:

    <catalog_category_default>
        <reference name="root">
            <action method="addBodyClass">
                <className helper="mymodule/my_helper/getCategoryClass" />
            </action>
        </reference>
    </catalog_category_default>
    
    您需要更改代码,以便它检索属性的值。e、 对由
    Mage::registry('current_category')
    返回的类别执行g
    getMyAttribute()

    此外,您还需要确保返回的内容适合作为CSS类。在本例中,我们不需要做任何事情,因为ID始终只是将附加到
    类别ID-
    的数字。如果属性的值并不总是安全的,那么您可能需要考虑使用“

    ”。

    这听起来太棒了——正是我想要的。我要试试看@RiaElliger非常好,很高兴听到这个消息。已经使用相同的功能基于相同的属性添加主题CSS。这真是太神奇了:)
    public function getCategoryClass() {
        return 'category-id-' . Mage::registry('current_category')->getId();
    }