{"id":1546,"date":"2021-01-23T12:12:23","date_gmt":"2021-01-23T12:12:23","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/get-categories-from-a-product-in-magento-2\/"},"modified":"2025-06-05T09:44:32","modified_gmt":"2025-06-05T04:14:32","slug":"get-categories-from-a-product-in-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/get-categories-from-a-product-in-magento-2\/","title":{"rendered":"How to Get Categories From a Product in Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Magento 2 store owners have to deal a lot with products and categories.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In constant efforts to improve the store\u2019s shopping experience, the admin may want to improve which categories a product is assigned to in Magento 2 store, update the categories, etc.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In order to do so, initially, one may need to&nbsp;get categories from a product in Magento 2. This report will further assist to update categories and products based on the business requirements.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Also read:&nbsp;<a href=\"https:\/\/meetanshi.com\/blog\/magento-2-api-get-all-categories\/\" target=\"_blank\" rel=\"noreferrer noopener\">How to Get All Categories in Magento 2 Using API?<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Method to Get Categories From a Product in Magento 2:<\/h2>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n\nuse Magento\\Catalog\\Model\\Product;\nuse Magento\\Catalog\\Model\\ResourceModel\\Category\\CollectionFactory;\n\nprotected $product;\nprotected $categoryCollection;\npublic function __construct(Product $product, CollectionFactory $categoryCollection)\n{\n    $this->product = $product;\n    $this->categoryCollection = $categoryCollection;\n}\n\npublic function getCategoriesName($productId)\n{\n    $product = $this->product->load($productId);\n    $categoryIds = $product->getCategoryIds();\n    $categories = $this->categoryCollection->create()->addAttributeToSelect('*')->addAttributeToFilter('entity_id', $categoryIds);\n    $categoryNames = [];\n    foreach ($categories as $category) {\n        $categoryNames[] = $category->getName();\n    }\n    $categoryName = implode(',', $categoryNames);\n    return $categoryName;\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">That\u2019s it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Also, please share the post with Magento community via social media.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thank you.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Magento 2 store owners have to deal a lot with products and categories. In constant efforts to improve the store\u2019s shopping experience, the admin may&#8230;<\/p>\n","protected":false},"author":13,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-1546","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1546","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/13"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=1546"}],"version-history":[{"count":5,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1546\/revisions"}],"predecessor-version":[{"id":16407,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1546\/revisions\/16407"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=1546"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=1546"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=1546"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}