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

Magento 根据产品类别,在产品价格附近显示标签,magento,static,block,product,Magento,Static,Block,Product,我在view.phtml中有一行代码,它可以工作并显示接近product price的静态块的内容,仅适用于数组中指定的类别id的产品 有一个问题。如果我点击“目录”或者从搜索结果中获取产品页面,那么它就不起作用。我不能在数组中添加“目录”类别id,因为我只想在特定类别上显示静态块。 你能帮我吗 <?php $category = Mage::getModel('catalog/layer')->getCurrentCategory();?> <?php

我在view.phtml中有一行代码,它可以工作并显示接近product price的静态块的内容,仅适用于数组中指定的类别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; ?>

是,当您来自目录搜索或直接在浏览器中打开产品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; ?>