【问题标题】:Show label near product price, depending on product category在产品价格中显示标签,具体取决于产品类别
【发布时间】:2015-10-11 10:28:04
【问题描述】:

我在 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; ?>

【问题讨论】:

    标签: magento static block product


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2019-07-09
      • 2015-10-08
      • 1970-01-01
      • 1970-01-01
      • 2021-05-19
      • 1970-01-01
      • 2022-12-14
      • 1970-01-01
      • 2018-06-14
      相关资源
      最近更新 更多