Magento“如果类别是X”则在2columns-right.phtml中显示文本

时间:2014-03-15 19:29:00

标签: php magento

我试图使用我在catalog / product / view.phtml文件中使用的代码及其工作原理

$yourCatIds = array(6,12); //telkide või peoinventari rent
$productCats = $_product->getAvailableInCategories();
if (count(array_intersect($yourCatIds,$productCats))) {
    echo "text";
}

但这给了我这个错误:

致命错误:在第25行的/home/profitel/public_html/app/design/frontend/default/hellowired/template/page/2columns-right.phtml中的非对象上调用成员函数getAvailableInCategories()< / p>

我也尝试了

$parentCategory = Mage::getModel('catalog/category')->load($parentCategoryId);

但也没有用。

1 个答案:

答案 0 :(得分:2)

如果您在2columns-right.phtml中,则无法直接访问$ _product,因为它在目录块中定义。

如果您想获得当前产品或当前类别,只需以这种方式访问​​它们:

$current_product = Mage::registry('current_product');

$currentCategory = Mage::registry('current_category');

因此,您的代码变为:

$yourCatIds = array(6,12);
$currentCategory = Mage::registry('current_category');
error_log('CC '. $currentCategory->getId());
if (in_array($currentCategory->getId(), $yourCatIds)) {
    echo "text";
}