2016-05-13 92 views
1

如何在產品詳細信息頁面中獲取類別集合。我試過使用下面的代碼,但它沒有得到集合。 如何得到它?如何在magento 2的產品詳細信息頁面中使用產品ID獲取類別集合?

$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); 
    $category = $objectManager->get('Magento\Framework\Registry') 
     ->registry('current_category'); 
    $categoryProducts = $category->getCategoryProducts($categoryId); 

回答

1

產品詳細頁面上,你可以叫getCategoryCollection產品的目標函數得到分類收集如下:

$_product = $block->getProduct(); 
$categoryCollection = $_product->getCategoryCollection(); 
+0

可以也添加類別屬性到集合 - '$ _product-> getCategoryCollection() - > addAttributeToSelect(array('name'))' – minlare

+0

這項工作通常,但它不起作用,當你直接進入產品來自主頁。不通過類別頁面。 – surfer190

0

從您發佈的代碼中,使用$ categoryId,但它是未定義的。

嘗試:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); 
$category = $objectManager->get('Magento\Framework\Registry')->registry('current_category'); 
$categoryProducts = $category ->getCategoryProducts($category->getId()); 
+0

感謝friend.i將檢查 – vijay

相關問題