1 回答
TA贡献1824条经验 获得超8个赞
最简单的方法是将已经显示的类别存储在 an 中,Array并使用in_array检查您要显示的类别是否已经在其中
编辑:存储 id 可能比存储名称更好,因为如果已经获取,您可以避免获取名称:
<?php
$diplayed_categories = []; //initializing array
foreach ($_productCollection as $_product):
?>
<div class="bk-all-products">
<?php
$bk_product_id = $_product->getCategoryIds();
$bk_category_id = $bk_product_id[1];
if(!in_array($bk_category_id, $diplayed_categories)){ //testing if not in array
$diplayed_categories[] = $bk_category_id; //filling the array
//moved inside the if, no need to fetch it again if it exists
//$categoryId = $bk_category_id; //useless var
$_objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$category = $_objectManager->create('Magento\Catalog\Model\Category')
->load($bk_category_id); //replaced by $bk_category_id
$bk_category_id_name = $category->getName();
echo $bk_category_id_name;
echo "<br><br>";
}
?>
</div>
<?php endforeach; ?>
- 1 回答
- 0 关注
- 262 浏览
添加回答
举报