dongqiao8421 2014-03-15 11:29
浏览 26
已采纳

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

I have tried to use the code I use in catalog/product/view.phtml file and where it works

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

but that gives me this error:

Fatal error: Call to a member function getAvailableInCategories() on a non-object in /home/profitel/public_html/app/design/frontend/default/hellowired/template/page/2columns-right.phtml on line 25

I tried also

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

but also didn't work.

  • 写回答

1条回答 默认 最新

  • dongxiong1941 2014-03-17 00:23
    关注

    If you are in 2columns-right.phtml you cannot access directly $_product, as it is defined in the catalog block.

    If you want to get the current product or current category, simply access them in this way:

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

    or

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

    So, your code becomes:

    $yourCatIds = array(6,12);
    $currentCategory = Mage::registry('current_category');
    error_log('CC '. $currentCategory->getId());
    if (in_array($currentCategory->getId(), $yourCatIds)) {
        echo "text";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部