{"id":892,"date":"2020-03-20T06:50:19","date_gmt":"2020-03-20T06:50:19","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/2020\/03\/20\/get-parent-category-id-in-magento-2\/"},"modified":"2025-07-23T10:01:50","modified_gmt":"2025-07-23T04:31:50","slug":"get-parent-category-id-in-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/get-parent-category-id-in-magento-2\/","title":{"rendered":"How To Get Parent Category ID In Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Magento 2 is a flexible CMS that allows store owners to offer feature-rich shopping platforms and expand their E-commerce stores!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">One such customization that the CMS allows is implementing category-specific features, and for that, you need the programmatic methods to&nbsp;<em><strong>get parent category ID in Magento 2<\/strong><\/em>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example, you need to display the name or image of a parent category from its subcategory. Or, impose shopping restrictions based on a particular category.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can use the below code for such category-related customizations in Magento 2, depending on the business or client requirements. Methods to Get Parent Category ID in Magento 2 are as follows:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Using&nbsp; block file<\/li>\n\n\n\n<li>Using PHTML file<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">Methods to Get Parent Category ID in Magento 2:<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">1.<strong> Using Block File<\/strong><\/p>\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\nnamespace Vendor\\Extension\\Block;\n\nuse Magento\\Framework\\View\\Element\\Template;\nuse Magento\\Backend\\Block\\Template\\Context;\nuse Magento\\Catalog\\Model\\CategoryFactory;\nuse Magento\\Framework\\Registry;\n\nclass CategoryBrand extends Template{\n\n    protected $categoryFactory;\n    protected $registry;\n    protected $category;\n\n    public function __construct(\n        Context $context,\n        CategoryFactory $categoryFactory,\n        Registry $registry,\n        array $data = []\n    )\n    {\n        $this->categoryFactory = $categoryFactory;\n        $this->registry = $registry;\n        parent::__construct($context, $data);\n    }\n\n    public function getCurrentProduct()\n    {\n        return $this->registry->registry('current_product');\n    }\n\n    public function getCategory($categoryId)\n    {\n        $this->category = $this->categoryFactory->create();\n        $this->category->load($categoryId);\n        return $this->category;\n    }\n}\n\n?><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">2. <strong>Using PHTML File<\/strong><\/p>\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\n$product = $this->getCurrentProduct();\n\n$categoryIds = $product->getCategoryIds(); \/\/Get Current CategoryIds\n\nforeach ($categoryIds as $categoryId){\n    $category = $this->getCategory($categoryId);   \/\/ Load Category\n    $parentCategories = $category->getparent_id();   \/\/ Get Parent Category Id\n\n    echo $parentCategories. '&lt;br \/>';\n}\n\n?><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">That\u2019s it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Use any of the above methods to get parent category ID in Magento 2.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Please share the solution with the 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 is a flexible CMS that allows store owners to offer feature-rich shopping platforms and expand their E-commerce stores! One such customization that the&#8230;<\/p>\n","protected":false},"author":5,"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-892","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/892","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\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=892"}],"version-history":[{"count":4,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/892\/revisions"}],"predecessor-version":[{"id":19111,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/892\/revisions\/19111"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=892"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=892"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=892"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}