{"id":7190,"date":"2019-09-18T16:45:50","date_gmt":"2019-09-18T11:15:50","guid":{"rendered":"https:\/\/www.hiddentechies.com\/blog\/?p=7190"},"modified":"2019-09-27T09:59:41","modified_gmt":"2019-09-27T04:29:41","slug":"magento-2-get-all-categories-specific-product","status":"publish","type":"post","link":"https:\/\/www.hiddentechies.com\/blog\/magento-2\/magento-2-get-all-categories-specific-product\/","title":{"rendered":"Magento 2 &#8211; How to Get All Categories of Specific Product"},"content":{"rendered":"<p>In this post I am going to explain how to get all categories of specific product in magento 2.<\/p>\n<p><strong>1. Using Dependency Injection<\/strong><\/p>\n<p>Add below code snippet in Block class.<\/p>\n<pre class=\"lang:default decode:true \">protected $_categoryCollectionFactory;\r\nprotected $_productRepository;\r\n\r\npublic function __construct(\r\n\t\\Magento\\Catalog\\Block\\Product\\Context $context, \r\n\t\\Magento\\Catalog\\Model\\ResourceModel\\Category\\CollectionFactory $categoryCollectionFactory,\r\n\t\\Magento\\Catalog\\Model\\ProductRepository $productRepository,\r\n\tarray $data = []\r\n) {\r\n\t$this-&gt;_categoryCollectionFactory = $categoryCollectionFactory;\r\n\t$this-&gt;_productRepository = $productRepository;\r\n\tparent::__construct($context, $data);\r\n}\r\n\r\npublic function getProductCategoryIds($productId) {\r\n\t$product = $this-&gt;_productRepository-&gt;getById($productId);\r\n\t$productCategoryIds = $product-&gt;getCategoryIds();\r\n\treturn $productCategoryIds;\r\n}\r\n\r\npublic function getProductCategoriesCollection($categoryIds) {\r\n\t$categoryCollection = $this-&gt;_categoryCollectionFactory-&gt;create();\r\n\t$categoryCollection-&gt;addAttributeToSelect('*');\r\n\t$categoryCollection-&gt;addIsActiveFilter();\r\n\t$categoryCollection-&gt;addAttributeToFilter('entity_id', $categoryIds);\r\n\treturn $categoryCollection;\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p>Add below code snippet in template file.<\/p>\n<pre class=\"lang:default decode:true \">$productId = 1;\r\n\r\n\/\/ get product categorory ids\r\n$productCategoryIds = $block-&gt;getProductCategoryIds($productId);\r\n\r\necho \"&lt;pre&gt;\";\r\nprint_r($productCategoryIds);\r\necho \"&lt;\/pre&gt;\";\r\n\r\n\r\n\/\/ get product category collection\r\n$categoryCollection = $block-&gt;getProductCategoriesCollection($productCategoryIds);\r\n\r\nif ($categoryCollection &amp;&amp; count($categoryCollection) &gt; 0) {\r\n    foreach ($categoryCollection as $category) {\r\n        echo $category-&gt;getId() . '&lt;br&gt;';\r\n        echo $category-&gt;getName() . '&lt;br&gt;';\r\n        echo $category-&gt;getUrl() . '&lt;br&gt;';\r\n    }\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>2. Using Object Manager<\/strong><\/p>\n<pre class=\"lang:default decode:true \">$objectManager = \\Magento\\Framework\\App\\ObjectManager::getInstance();\r\n\r\n$productRepository = $objectManager-&gt;get('\\Magento\\Catalog\\Model\\ProductRepository');\r\n$categoryCollection = $objectManager-&gt;get('\\Magento\\Catalog\\Model\\ResourceModel\\Category\\CollectionFactory');\r\n\r\n$productId = 1;\r\n\r\n$product = $productRepository-&gt;getById($productId);\r\n\r\n\/\/ get product categorory ids\r\n$productCategoryIds = $product-&gt;getCategoryIds();\r\n\r\necho \"&lt;pre&gt;\";\r\nprint_r($productCategoryIds);\r\necho \"&lt;\/pre&gt;\";\r\n\r\n\r\n\/\/ get product category collection\r\n$categoryCollection = $categoryCollection-&gt;create();\r\n$categoryCollection-&gt;addAttributeToSelect('*');\r\n$categoryCollection-&gt;addAttributeToFilter('entity_id', $productCategoryIds);\r\n\r\nif ($categoryCollection &amp;&amp; count($categoryCollection) &gt; 0) {\r\n    foreach ($categoryCollection as $category) {\r\n        echo $category-&gt;getId() . '&lt;br&gt;';\r\n        echo $category-&gt;getName() . '&lt;br&gt;';\r\n        echo $category-&gt;getUrl() . '&lt;br&gt;';\r\n    }\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p>Thats it. Enjoy Magento 2!!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this post I am going to explain how to get all categories of specific product in magento 2. 1. Using Dependency Injection Add below code snippet in Block class. protected $_categoryCollectionFactory; protected $_productRepository; public function __construct( \\Magento\\Catalog\\Block\\Product\\Context $context, \\Magento\\Catalog\\Model\\ResourceModel\\Category\\CollectionFactory $categoryCollectionFactory, \\Magento\\Catalog\\Model\\ProductRepository $productRepository, array $data = [] ) { $this-&gt;_categoryCollectionFactory = $categoryCollectionFactory; $this-&gt;_productRepository = $productRepository;&#8230; <\/p>\n<div class=\"actions\"><a href=\"https:\/\/www.hiddentechies.com\/blog\/magento-2\/magento-2-get-all-categories-specific-product\/\">Continue Reading<\/a><\/div>\n","protected":false},"author":1,"featured_media":7318,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[59],"tags":[10,28,27,662,663],"_links":{"self":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts\/7190"}],"collection":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/comments?post=7190"}],"version-history":[{"count":2,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts\/7190\/revisions"}],"predecessor-version":[{"id":7315,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts\/7190\/revisions\/7315"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/media\/7318"}],"wp:attachment":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/media?parent=7190"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/categories?post=7190"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/tags?post=7190"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}