【问题标题】:Add static block in product detail page only for certain categories仅在某些类别的产品详细信息页面中添加静态块
【发布时间】:2014-10-11 05:54:48
【问题描述】:

我想在类别 4 的产品详细信息页面中添加一个静态块。我这样做了:

$catid = $this->helper('catalog/data')->getProduct()->getCategoryIds();
?>
<?php $blockID = "free_shipping_" + $catid ?>
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId($blockID)->toHtml()  
?> 

然后在管理员中创建了一个带有 URL 和块标题 free_shipping_4 的静态块。 (free_shipping_4 中的 4 代表类别 4)。

我得到了错误:

致命错误:第 140 行 C:\wamp\www\mydomain\app\design\frontend\default\mytheme\template\catalog\product\view.phtml 中不支持的操作数类型

【问题讨论】:

  • 第 140 行是什么样的?
  • $this-&gt;helper('catalog/data')-&gt;getProduct()-&gt;getCategoryIds(); 返回一个数组,其中包含与当前产品关联的所有类别的 ID,而不是单个 ID,因此 "free_shipping_" + $catid 不起作用(我猜这是第 140 行) .
  • 我正在使用我自己的主题。最初第 40 行是 但现在我发现 $catid 是数组。所以现在使用 $catid[1] 就可以了。

标签: magento


【解决方案1】:

是的,你是对的。我发现现在 $catid 是一个数组。所以我用 $catid[1] 获取目录 ID。所以我的代码是这样的

<?php $blockID = "free_shipping_" . $catid[1]?>
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId($blockID)->toHtml();

为了在静态块名称 (free_shipping_4) 中找到我感兴趣的目录 ID,我使用了以下代码

 SELECT entity_id AS categoryID, value AS categoryName
    FROM catalog_category_entity_varchar
    WHERE attribute_id =4
    LIMIT 0 , 30

现在解决了。尽管必须有更好,更简单的方法来做到这一点。

【讨论】:

    【解决方案2】:

    $catid = $this->helper('catalog/data')->getProduct()->getCategoryIds(); ?> getLayout()->createBlock('cms/block')->setBlockId($blockID)->toHtml() ?>

    $catid will contain array of id product is associated with.
    

    我认为一种方法是创建一个产品属性,其中包含需要显示的类别 ID,然后您可以检索产品属性。

    <?php $blockID = "free_shipping_" + $productattributevalue ?>
    

    【讨论】:

      猜你喜欢
      • 2022-08-19
      • 1970-01-01
      • 1970-01-01
      • 2014-08-16
      • 1970-01-01
      • 2022-01-13
      • 2014-01-02
      • 1970-01-01
      • 2022-07-07
      相关资源
      最近更新 更多