如何在 Magento 2 的产品详细信息页面中使用产品 ID 获取类别集合

How to get category collection using product id in product details page in Magento 2

商品详情页如何获取分类合集?我试过使用下面的代码,但没有得到集合。

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

从您发布的代码来看,您使用的是 $categoryId 但未定义。

尝试:

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

在产品详细信息页面上,您只需调用产品对象上的 getCategoryCollection 函数即可获取类别集合,如下所示:

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