Home » Magento, Magento 219 September 2016

Magento 2: Get all Categories of any/current Product

This article shows how we can get list of all categories from current product or any particular product as well.

Below is a block class of my custom module (Chapagain_HelloWorld). I have injected object of \Magento\Catalog\Model\ResourceModel\Category\CollectionFactory, \Magento\Catalog\Model\ProductRepository and \Magento\Framework\Registry classes in the constructor of my module’s block class.


To get the current product, we use getCurrentProduct() function. Otherwise, to get any particular product, we use getProductById($id) function.

We then fetch the category ids associated with that product. After that, we fetch the category collection data for those category ids. Here’s the code to be written in template (.phtml) file.

Hope this helps. Thanks.

Magento 2

Get New Post by Email

Find me on

FacebookTwitterGoogle+LinkedInRSS Feed

Comments are closed.