【问题标题】:How to show content on the product view depending on category ID?如何根据类别 ID 在产品视图上显示内容?
【发布时间】:2012-09-10 14:04:56
【问题描述】:

我有这个代码 sn-p 用于根据产品所属的顶级类别显示内容:

<?php $categoryIds = $_product->getCategoryIds(); if($categoryIds[0]!= '42'):?>
   CONTENT HERE...
<?php endif; ?>

这是放在 Magento 1.6.1 社区版的目录/产品下的 list.phtml 和 view.phtml 中。此代码适用于一个 ID,但当我尝试添加时:

<?php $categoryIds = $_product->getCategoryIds(); if($categoryIds[0]!= '42' || $categoryIds[1]!= '43'):?>
   CONTENT HERE...
<?php endif; ?>

它不再起作用了。你们中的一些 Magento 巫师可以帮助一个可怜的灵魂吗?! 提前致谢。

【问题讨论】:

  • 您可以尝试将|| 更改为&amp;&amp; 吗?否则,您将不得不指定它不再工作的意思......
  • 不工作是指没有任何反应。即使我属于“42”或“43”类别,内容仍会显示。我马上试试!谢谢!
  • 试过了!仍然显示类别“43”的东西:)
  • 哦,等等 - 您正在访问 $categoryIds 中的两个单独条目 - 对吗?您将$categoryIds[0] 与42 进行比较,将$categoryIds[1] 与43 进行比较。对吗?

标签: php magento


【解决方案1】:

Guerra 之前的帖子几乎是正确的。他只是搞砸了一点逻辑——你需要逻辑 AND 而不是 OR:

if(!in_array('42',$categoryIds) && !in_array('43',$categoryIds))

否则,对于 42 和 43 类别,case 将返回 true(因为 OR)。

【讨论】:

  • 太棒了!我尝试了 OR,当然错了! :) 再次感谢
【解决方案2】:

您可以尝试更改 if 语句

if(!in_array('42',$categoryIds) || !in_array('43',$categoryIds))

它比固定位置更好。因为如果你的函数返回 43 和 42 以便你不能在位置 0 和 1 上正确。

【讨论】:

  • 以前试过这个。没有为我工作。在 cat 42 和 43 中时,HTML 仍填充在 DOM 中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多