Skip to content
Advertisement

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

How can I get a category collection in the product details page? I’ve tried using the below code, but it’s not getting the collection.

$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$category = $objectManager->get('MagentoFrameworkRegistry')
    ->registry('current_category');
$categoryProducts = $category->getCategoryProducts($categoryId);

Advertisement

Answer

on the product detail page you can just call getCategoryCollection function on product’s Object to get Category Collection as following :

$_product = $block->getProduct();
$categoryCollection = $_product->getCategoryCollection();
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement