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();