1 回答

TA貢獻(xiàn)1824條經(jīng)驗(yàn) 獲得超8個(gè)贊
最簡(jiǎn)單的方法是將已經(jīng)顯示的類別存儲(chǔ)在 an 中,Array并使用in_array檢查您要顯示的類別是否已經(jīng)在其中
編輯:存儲(chǔ) id 可能比存儲(chǔ)名稱更好,因?yàn)槿绻呀?jīng)獲取,您可以避免獲取名稱:
<?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 關(guān)注
- 279 瀏覽
添加回答
舉報(bào)