How to Get Categories from specific Product in Magento 2

Today I will recommend the way to get categories specific product in Magento 2. It is allowed to get the list from current or any product as you need.

Now you need to open the block class of my custom module (Mageplaza_HelloWorld), then inject the object of \Magento\Catalog\Model\ResourceModel\Category\CollectionFactory, \Magento\Catalog\Model\ProductRepository and \Magento\Framework\Registry classes in the constructor of that block class.

app/code/Mageplaza/HelloWorld/Block/HelloWorld.php

<?php
namespace Mageplaza\HelloWorld\Block;
class HelloWorld extends \Magento\Framework\View\Element\Template
{    
    protected $_categoryCollectionFactory;
    protected $_productRepository;
    protected $_registry;
        
    public function __construct(
        \Magento\Backend\Block\Template\Context $context,        
        \Magento\Catalog\Model\ResourceModel\Category\CollectionFactory $categoryCollectionFactory,
        \Magento\Catalog\Model\ProductRepository $productRepository,
        \Magento\Framework\Registry $registry,
        array $data = []
        )
        {
            $this->_categoryCollectionFactory = $categoryCollectionFactory;
            $this->_productRepository = $productRepository;
            $this->_registry = $registry;
            parent::__construct($context, $data);
    }
    
    /**
     * Get category collection
     *
     * @param bool $isActive
     * @param bool|int $level
     * @param bool|string $sortBy
     * @param bool|int $pageSize
     * @return \Magento\Catalog\Model\ResourceModel\Category\Collection or array
     */
    public function getCategoryCollection($isActive = true, $level = false, $sortBy = false, $pageSize = false)
    {
        $collection = $this->_categoryCollectionFactory->create();
        $collection->addAttributeToSelect('*');        
        
        // select only active categories
        if ($isActive) {
            $collection->addIsActiveFilter();
        }
                
        // select categories of certain level
        if ($level) {
            $collection->addLevelFilter($level);
        }
        
        // sort categories by some value
        if ($sortBy) {
            $collection->addOrderField($sortBy);
        }
        
        // select certain number of categories
        if ($pageSize) {
            $collection->setPageSize($pageSize); 
        }    
        
        return $collection;
    }
    
    public function getProductById($id)
    {        
        return $this->_productRepository->getById($id);
    }
    
    public function getCurrentProduct()
    {        
        return $this->_registry->registry('current_product');
    }
}
?>

In case you want to work with the current product, the getCurrentProduct() funciton is active. And if from any specific item, please enable getProductById($id) function. After that, you can retrieve the collection of the category from the category IDs which are linked to that product. In the .phtml file, you need to add the following code snippet:

$productId = 1; // YOUR PRODUCT ID
$product = $block->getProductById($productId);
 
// for current product
// $product = $block->getCurrentProduct();
 
$categoryIds = $product->getCategoryIds();
 
$categories = $block->getCategoryCollection()
                    ->addAttributeToFilter('entity_id', $categoryIds);
                    
foreach ($categories as $category) {
    echo $category->getName() . '<br>';
}

That is all things you will use to retrieve categories specific product in Magento 2. Thanks for your reading and please comment below if there is any trouble in that.





Enjoyed the tutorial? Spread it to your friends!

magento-2-tutorial
get
categories
specific
product

Comments for How to Get Categories from specific Product in Magento 2

Please leave comments if you have any questions, feedbacks.

Module Development

You also may like these Magento 2 Extensions

One Step Checkout

$199
10 reviews

Layered Navigation

$99
12 reviews

SEO

FREE
no review

People also searched for:

  • magento 2 get categories specific product