根据产品类别在产品价格附近显示标签

时间:2015-10-11 10:28:05

标签: magento static block product

我在view.phtml中有这些代码行,它们可以在产品价格附近显示静态块的内容,仅适用于数组中指定的类别id的产品。

有一个问题。如果我点击"目录"它不会起作用。或者如果我从搜索结果中获取产品页面。我无法添加"目录"我的数组中的类别ID,因为我只想在特定类别上显示静态块。 你能救我吗?

    <?php $category = Mage::getModel('catalog/layer')->getCurrentCategory();?>
    <?php $arr = array(116, 118, 119, 120, 121, 122, 123, 126, 128, 129, 130, 132, 133, 136);?>

        <?php if(in_array($category->getId(),$arr)): ?>
        <?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('prezzo-metro')->toHtml(); ?> 
        <?php endif; ?>

1 个答案:

答案 0 :(得分:0)

是当您来自目录搜索或直接在浏览器中打开产品URl时,它将不起作用。因为在这种情况下,您将无法获得当前的类别ID。

您需要使用以下代码。

<?php
if (Mage::registry('current_category')) {
    $category = Mage::registry('current_category');
} else {
    $categoryIds = $_product->getCategoryIds();
    if (count($categoryIds)) {
        $firstCategoryId = $categoryIds[0];
        $category = Mage::getModel('catalog/category')->load($firstCategoryId);
    }
}
?>

<?php $arr = array(116, 118, 119, 120, 121, 122, 123, 126, 128, 129, 130, 132, 133, 136); ?>

<?php if (in_array($category->getId(), $arr)): ?>
    <?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('prezzo-metro')->toHtml(); ?> 
<?php endif; ?>