如何在magento 2的产品详细信息页面中使用产品ID获取类别集合?
问题描述:
如何在产品详细信息页面中获取类别集合。我试过使用下面的代码,但它没有得到集合。 如何得到它?如何在magento 2的产品详细信息页面中使用产品ID获取类别集合?
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$category = $objectManager->get('Magento\Framework\Registry')
->registry('current_category');
$categoryProducts = $category->getCategoryProducts($categoryId);
答
产品详细页面上,你可以叫getCategoryCollection产品的目标函数得到分类收集如下:
$_product = $block->getProduct();
$categoryCollection = $_product->getCategoryCollection();
答
从您发布的代码中,使用$ 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
可以也添加类别属性到集合 - '$ _product-> getCategoryCollection() - > addAttributeToSelect(array('name'))' – minlare
这项工作通常,但它不起作用,当你直接进入产品来自主页。不通过类别页面。 – surfer190