Magento 2 - How to get category name and category tree for product or for category id?How to get category tree in Magento 2?how to get multiple categories and subcategories for a product in a custom script in magentoHow to get Product Category name and Category ID on checkout success page?How to get first product from category in 1column.phtml?How to add Category tree structure like product edit/add in custom module Magento 2 [Solved]Magento 2: How to Override Abstract Class for Product Category Indexing Issue?Magento 2: How to get secure category URL programmatically?Magento 2 - Get a level descending category collection for path to product for breadcrumbsMagento 2 some Product URLs show Category pathHow to restore 'sort by position' option for category sortingMagento 2 Get Full Category Path for product

Are polynomials with the same roots identical?

Is there a DSLR/mirorless camera with minimal options like a classic, simple SLR?

New bike, tubeless tire will not inflate

Increase speed altering column on large table to NON NULL

Electricity free spaceship

How to learn Linux system internals

Teaching a class likely meant to inflate the GPA of student athletes

Which is the better way to call a method that is only available to one class that implements an interface but not the other one?

Advantages of the Exponential Family: why should we study it and use it?

Did Apple bundle a specific monitor with the Apple II+ for schools?

Smart-expansion of a range to a list of numbers

What differences exist between adamantine and adamantite in all editions of D&D?

What standard algorithm can determine if exactly one of a container satisfies a predicate?

Why can I traceroute to this IP address, but not ping?

Understanding "Current Draw" in terms of "Ohm's Law"

How can I make 12 tone and atonal melodies sound interesting?

What is the logic behind taxing money for property?

60s or 70s novel about Empire of Man making 1st contact with 1st discovered alien race

Separate SPI data

What aircraft was used as Air Force One for the flight between Southampton and Shannon?

How can I remove material from this wood beam?

Why is long-term living in Almost-Earth causing severe health problems?

Return a String containing only alphabets without spaces

Is it expected that a reader will skip parts of what you write?



Magento 2 - How to get category name and category tree for product or for category id?


How to get category tree in Magento 2?how to get multiple categories and subcategories for a product in a custom script in magentoHow to get Product Category name and Category ID on checkout success page?How to get first product from category in 1column.phtml?How to add Category tree structure like product edit/add in custom module Magento 2 [Solved]Magento 2: How to Override Abstract Class for Product Category Indexing Issue?Magento 2: How to get secure category URL programmatically?Magento 2 - Get a level descending category collection for path to product for breadcrumbsMagento 2 some Product URLs show Category pathHow to restore 'sort by position' option for category sortingMagento 2 Get Full Category Path for product






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








3















Magento 2.2.7



I have to output category name and "tree" or path for product.
For instance if my product is in category Lg775 my output should be:
Hardware/Motherboards/Intel/Lg775



so far I have this code that gives me category ID:



 //Load the product categories
$categories = $product_data->getCategoryIds();
//Select the last category in the list
$categoryId = end($categories);


Thank you all Magento experts for all the help so far! I really appreciate it :)




UPDATE - Solved



So I used the answer I checked however I had to modify the code so here is my code how to get full category path for "categoryID"



<?php

namespace VendorModuleHelper;


class CategoryTree

/**
* @var MagentoStoreModelStoreManagerInterface
*/
private $storeManager;

/**
* @var MagentoCatalogApiCategoryRepositoryInterface
*/
private $categoryRepository;

/**
* @var MagentoCatalogModelResourceModelCategoryTree
*/
private $tree;

public function __construct(
MagentoCatalogApiCategoryRepositoryInterface $categoryRepository,
MagentoCatalogModelResourceModelCategoryTree $tree,
MagentoStoreModelStoreManagerInterface $storeManager)
$this->tree = $tree;
$this->categoryRepository = $categoryRepository;
$this->storeManager = $storeManager;
// end of function __construct


public function getTreeByCategoryId($categoryId)

$storeId = $this->storeManager->getStore()->getId();
$category = $this->categoryRepository->get($categoryId, $storeId);
$categoryTree = $this->tree->setStoreId($storeId)->loadBreadcrumbsArray($category->getPath());

$categoryTreepath = array();
foreach($categoryTree as $eachCategory)
echo $eachCategory['name'];
$categoryTreepath[] = $eachCategory['name'];

$categoryTree = implode(" &gt; ",$categoryTreepath);
return $categoryTree;
// end of function getTreeByCategoryId

// end of class


later I called that class like this:



 $categoryObject=MagentoFrameworkAppObjectManager::getInstance();
$category=$categoryObject->create('VendorModuleHelperCategoryTree');
$categoryTreepath=$category->getTreeByCategoryId($categoryId);
var_dump($categoryTreepath);


The result was just what I needed :) Thank you all!










share|improve this question
























  • have you checked this magento.stackexchange.com/questions/204269/…

    – fmsthird
    Jun 3 at 4:18

















3















Magento 2.2.7



I have to output category name and "tree" or path for product.
For instance if my product is in category Lg775 my output should be:
Hardware/Motherboards/Intel/Lg775



so far I have this code that gives me category ID:



 //Load the product categories
$categories = $product_data->getCategoryIds();
//Select the last category in the list
$categoryId = end($categories);


Thank you all Magento experts for all the help so far! I really appreciate it :)




UPDATE - Solved



So I used the answer I checked however I had to modify the code so here is my code how to get full category path for "categoryID"



<?php

namespace VendorModuleHelper;


class CategoryTree

/**
* @var MagentoStoreModelStoreManagerInterface
*/
private $storeManager;

/**
* @var MagentoCatalogApiCategoryRepositoryInterface
*/
private $categoryRepository;

/**
* @var MagentoCatalogModelResourceModelCategoryTree
*/
private $tree;

public function __construct(
MagentoCatalogApiCategoryRepositoryInterface $categoryRepository,
MagentoCatalogModelResourceModelCategoryTree $tree,
MagentoStoreModelStoreManagerInterface $storeManager)
$this->tree = $tree;
$this->categoryRepository = $categoryRepository;
$this->storeManager = $storeManager;
// end of function __construct


public function getTreeByCategoryId($categoryId)

$storeId = $this->storeManager->getStore()->getId();
$category = $this->categoryRepository->get($categoryId, $storeId);
$categoryTree = $this->tree->setStoreId($storeId)->loadBreadcrumbsArray($category->getPath());

$categoryTreepath = array();
foreach($categoryTree as $eachCategory)
echo $eachCategory['name'];
$categoryTreepath[] = $eachCategory['name'];

$categoryTree = implode(" &gt; ",$categoryTreepath);
return $categoryTree;
// end of function getTreeByCategoryId

// end of class


later I called that class like this:



 $categoryObject=MagentoFrameworkAppObjectManager::getInstance();
$category=$categoryObject->create('VendorModuleHelperCategoryTree');
$categoryTreepath=$category->getTreeByCategoryId($categoryId);
var_dump($categoryTreepath);


The result was just what I needed :) Thank you all!










share|improve this question
























  • have you checked this magento.stackexchange.com/questions/204269/…

    – fmsthird
    Jun 3 at 4:18













3












3








3








Magento 2.2.7



I have to output category name and "tree" or path for product.
For instance if my product is in category Lg775 my output should be:
Hardware/Motherboards/Intel/Lg775



so far I have this code that gives me category ID:



 //Load the product categories
$categories = $product_data->getCategoryIds();
//Select the last category in the list
$categoryId = end($categories);


Thank you all Magento experts for all the help so far! I really appreciate it :)




UPDATE - Solved



So I used the answer I checked however I had to modify the code so here is my code how to get full category path for "categoryID"



<?php

namespace VendorModuleHelper;


class CategoryTree

/**
* @var MagentoStoreModelStoreManagerInterface
*/
private $storeManager;

/**
* @var MagentoCatalogApiCategoryRepositoryInterface
*/
private $categoryRepository;

/**
* @var MagentoCatalogModelResourceModelCategoryTree
*/
private $tree;

public function __construct(
MagentoCatalogApiCategoryRepositoryInterface $categoryRepository,
MagentoCatalogModelResourceModelCategoryTree $tree,
MagentoStoreModelStoreManagerInterface $storeManager)
$this->tree = $tree;
$this->categoryRepository = $categoryRepository;
$this->storeManager = $storeManager;
// end of function __construct


public function getTreeByCategoryId($categoryId)

$storeId = $this->storeManager->getStore()->getId();
$category = $this->categoryRepository->get($categoryId, $storeId);
$categoryTree = $this->tree->setStoreId($storeId)->loadBreadcrumbsArray($category->getPath());

$categoryTreepath = array();
foreach($categoryTree as $eachCategory)
echo $eachCategory['name'];
$categoryTreepath[] = $eachCategory['name'];

$categoryTree = implode(" &gt; ",$categoryTreepath);
return $categoryTree;
// end of function getTreeByCategoryId

// end of class


later I called that class like this:



 $categoryObject=MagentoFrameworkAppObjectManager::getInstance();
$category=$categoryObject->create('VendorModuleHelperCategoryTree');
$categoryTreepath=$category->getTreeByCategoryId($categoryId);
var_dump($categoryTreepath);


The result was just what I needed :) Thank you all!










share|improve this question
















Magento 2.2.7



I have to output category name and "tree" or path for product.
For instance if my product is in category Lg775 my output should be:
Hardware/Motherboards/Intel/Lg775



so far I have this code that gives me category ID:



 //Load the product categories
$categories = $product_data->getCategoryIds();
//Select the last category in the list
$categoryId = end($categories);


Thank you all Magento experts for all the help so far! I really appreciate it :)




UPDATE - Solved



So I used the answer I checked however I had to modify the code so here is my code how to get full category path for "categoryID"



<?php

namespace VendorModuleHelper;


class CategoryTree

/**
* @var MagentoStoreModelStoreManagerInterface
*/
private $storeManager;

/**
* @var MagentoCatalogApiCategoryRepositoryInterface
*/
private $categoryRepository;

/**
* @var MagentoCatalogModelResourceModelCategoryTree
*/
private $tree;

public function __construct(
MagentoCatalogApiCategoryRepositoryInterface $categoryRepository,
MagentoCatalogModelResourceModelCategoryTree $tree,
MagentoStoreModelStoreManagerInterface $storeManager)
$this->tree = $tree;
$this->categoryRepository = $categoryRepository;
$this->storeManager = $storeManager;
// end of function __construct


public function getTreeByCategoryId($categoryId)

$storeId = $this->storeManager->getStore()->getId();
$category = $this->categoryRepository->get($categoryId, $storeId);
$categoryTree = $this->tree->setStoreId($storeId)->loadBreadcrumbsArray($category->getPath());

$categoryTreepath = array();
foreach($categoryTree as $eachCategory)
echo $eachCategory['name'];
$categoryTreepath[] = $eachCategory['name'];

$categoryTree = implode(" &gt; ",$categoryTreepath);
return $categoryTree;
// end of function getTreeByCategoryId

// end of class


later I called that class like this:



 $categoryObject=MagentoFrameworkAppObjectManager::getInstance();
$category=$categoryObject->create('VendorModuleHelperCategoryTree');
$categoryTreepath=$category->getTreeByCategoryId($categoryId);
var_dump($categoryTreepath);


The result was just what I needed :) Thank you all!







magento2 category category-products category-tree






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jun 3 at 15:53







Oktarin

















asked Jun 2 at 20:19









OktarinOktarin

506




506












  • have you checked this magento.stackexchange.com/questions/204269/…

    – fmsthird
    Jun 3 at 4:18

















  • have you checked this magento.stackexchange.com/questions/204269/…

    – fmsthird
    Jun 3 at 4:18
















have you checked this magento.stackexchange.com/questions/204269/…

– fmsthird
Jun 3 at 4:18





have you checked this magento.stackexchange.com/questions/204269/…

– fmsthird
Jun 3 at 4:18










2 Answers
2






active

oldest

votes


















2














If you want to get a particular category tree you have to use below method




MagentoCatalogModelResourceModelCategoryTree::loadBreadcrumbsArray($path,
$addCollectionData = true, $withRootNode = false);




As per this method definition, you have to provide category path $path, of your category instead category ID.



So, first, you have to get category path by category id then load and after that get category tree by tree.



Code:



<?php

namespace StackExchangeMagentoModel;


class CategoryTree

/**
* @var MagentoStoreModelStoreManagerInterface
*/
private $storeManager;

/**
* @var MagentoCatalogApiCategoryRepositoryInterface
*/
private $categoryRepository;

/**
* @var MagentoCatalogModelResourceModelCategoryTree
*/
private $tree;

public function __construct(
MagentoCatalogApiCategoryRepositoryInterface $categoryRepository,
MagentoCatalogModelResourceModelCategoryTree $tree ,
MagentoStoreModelStoreManagerInterface $storeManager
)

$this->tree = $tree;
$this->categoryRepository = $categoryRepository;
$this->storeManager = $storeManager;

public function getTreeByCategoryId()

$storeId = $this->storeManager->getStore()->getId();
$categoryId = 45;
$category = $this->categoryRepository->get($categoryId, $storeId);
$categoryTree = $this->tree->setStoreId($storeId)->loadBreadcrumbsArray($category->getPath());

$categoryTreepath = '';
foreach($categoryTree as $eachCategory)
echo $category['name'];
echo '<pre>';
print_r($eachCategory);
$categoryTreepath = $categoryTreepath. '/'.$category['name'];








share|improve this answer























  • I have marked yours answer as it helped a lot, however I had to make some minor changes to the code that I will post in my original "question". Thank you!

    – Oktarin
    Jun 3 at 15:48



















2














You can get breaducrum of category by category Id using MagentoCatalogModelCategoryFactory as follows:



...
protected $_categoryFactory;
...
public function __construct(
MagentoCatalogModelCategoryFactory $categoryFactory
)
$this->_categoryFactory = $categoryFactory;


public function execute()
//Load the product categories
$categories = $product_data->getCategoryIds();
//Select the last category in the list
$categoryId = end($categories);
$category = $this->_categoryFactory->create()->load($categoryId);
// Parent Categories
$parentCategories = $category->getParentCategories();

foreach ($parentCategories as $cat)
echo $cat->getId() . ": " . $cat->getName() . " :: " . $cat->getUrl() . "<br />";




I hope this may helpful to anyone!






share|improve this answer























    Your Answer








    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "479"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f277013%2fmagento-2-how-to-get-category-name-and-category-tree-for-product-or-for-catego%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    If you want to get a particular category tree you have to use below method




    MagentoCatalogModelResourceModelCategoryTree::loadBreadcrumbsArray($path,
    $addCollectionData = true, $withRootNode = false);




    As per this method definition, you have to provide category path $path, of your category instead category ID.



    So, first, you have to get category path by category id then load and after that get category tree by tree.



    Code:



    <?php

    namespace StackExchangeMagentoModel;


    class CategoryTree

    /**
    * @var MagentoStoreModelStoreManagerInterface
    */
    private $storeManager;

    /**
    * @var MagentoCatalogApiCategoryRepositoryInterface
    */
    private $categoryRepository;

    /**
    * @var MagentoCatalogModelResourceModelCategoryTree
    */
    private $tree;

    public function __construct(
    MagentoCatalogApiCategoryRepositoryInterface $categoryRepository,
    MagentoCatalogModelResourceModelCategoryTree $tree ,
    MagentoStoreModelStoreManagerInterface $storeManager
    )

    $this->tree = $tree;
    $this->categoryRepository = $categoryRepository;
    $this->storeManager = $storeManager;

    public function getTreeByCategoryId()

    $storeId = $this->storeManager->getStore()->getId();
    $categoryId = 45;
    $category = $this->categoryRepository->get($categoryId, $storeId);
    $categoryTree = $this->tree->setStoreId($storeId)->loadBreadcrumbsArray($category->getPath());

    $categoryTreepath = '';
    foreach($categoryTree as $eachCategory)
    echo $category['name'];
    echo '<pre>';
    print_r($eachCategory);
    $categoryTreepath = $categoryTreepath. '/'.$category['name'];








    share|improve this answer























    • I have marked yours answer as it helped a lot, however I had to make some minor changes to the code that I will post in my original "question". Thank you!

      – Oktarin
      Jun 3 at 15:48
















    2














    If you want to get a particular category tree you have to use below method




    MagentoCatalogModelResourceModelCategoryTree::loadBreadcrumbsArray($path,
    $addCollectionData = true, $withRootNode = false);




    As per this method definition, you have to provide category path $path, of your category instead category ID.



    So, first, you have to get category path by category id then load and after that get category tree by tree.



    Code:



    <?php

    namespace StackExchangeMagentoModel;


    class CategoryTree

    /**
    * @var MagentoStoreModelStoreManagerInterface
    */
    private $storeManager;

    /**
    * @var MagentoCatalogApiCategoryRepositoryInterface
    */
    private $categoryRepository;

    /**
    * @var MagentoCatalogModelResourceModelCategoryTree
    */
    private $tree;

    public function __construct(
    MagentoCatalogApiCategoryRepositoryInterface $categoryRepository,
    MagentoCatalogModelResourceModelCategoryTree $tree ,
    MagentoStoreModelStoreManagerInterface $storeManager
    )

    $this->tree = $tree;
    $this->categoryRepository = $categoryRepository;
    $this->storeManager = $storeManager;

    public function getTreeByCategoryId()

    $storeId = $this->storeManager->getStore()->getId();
    $categoryId = 45;
    $category = $this->categoryRepository->get($categoryId, $storeId);
    $categoryTree = $this->tree->setStoreId($storeId)->loadBreadcrumbsArray($category->getPath());

    $categoryTreepath = '';
    foreach($categoryTree as $eachCategory)
    echo $category['name'];
    echo '<pre>';
    print_r($eachCategory);
    $categoryTreepath = $categoryTreepath. '/'.$category['name'];








    share|improve this answer























    • I have marked yours answer as it helped a lot, however I had to make some minor changes to the code that I will post in my original "question". Thank you!

      – Oktarin
      Jun 3 at 15:48














    2












    2








    2







    If you want to get a particular category tree you have to use below method




    MagentoCatalogModelResourceModelCategoryTree::loadBreadcrumbsArray($path,
    $addCollectionData = true, $withRootNode = false);




    As per this method definition, you have to provide category path $path, of your category instead category ID.



    So, first, you have to get category path by category id then load and after that get category tree by tree.



    Code:



    <?php

    namespace StackExchangeMagentoModel;


    class CategoryTree

    /**
    * @var MagentoStoreModelStoreManagerInterface
    */
    private $storeManager;

    /**
    * @var MagentoCatalogApiCategoryRepositoryInterface
    */
    private $categoryRepository;

    /**
    * @var MagentoCatalogModelResourceModelCategoryTree
    */
    private $tree;

    public function __construct(
    MagentoCatalogApiCategoryRepositoryInterface $categoryRepository,
    MagentoCatalogModelResourceModelCategoryTree $tree ,
    MagentoStoreModelStoreManagerInterface $storeManager
    )

    $this->tree = $tree;
    $this->categoryRepository = $categoryRepository;
    $this->storeManager = $storeManager;

    public function getTreeByCategoryId()

    $storeId = $this->storeManager->getStore()->getId();
    $categoryId = 45;
    $category = $this->categoryRepository->get($categoryId, $storeId);
    $categoryTree = $this->tree->setStoreId($storeId)->loadBreadcrumbsArray($category->getPath());

    $categoryTreepath = '';
    foreach($categoryTree as $eachCategory)
    echo $category['name'];
    echo '<pre>';
    print_r($eachCategory);
    $categoryTreepath = $categoryTreepath. '/'.$category['name'];








    share|improve this answer













    If you want to get a particular category tree you have to use below method




    MagentoCatalogModelResourceModelCategoryTree::loadBreadcrumbsArray($path,
    $addCollectionData = true, $withRootNode = false);




    As per this method definition, you have to provide category path $path, of your category instead category ID.



    So, first, you have to get category path by category id then load and after that get category tree by tree.



    Code:



    <?php

    namespace StackExchangeMagentoModel;


    class CategoryTree

    /**
    * @var MagentoStoreModelStoreManagerInterface
    */
    private $storeManager;

    /**
    * @var MagentoCatalogApiCategoryRepositoryInterface
    */
    private $categoryRepository;

    /**
    * @var MagentoCatalogModelResourceModelCategoryTree
    */
    private $tree;

    public function __construct(
    MagentoCatalogApiCategoryRepositoryInterface $categoryRepository,
    MagentoCatalogModelResourceModelCategoryTree $tree ,
    MagentoStoreModelStoreManagerInterface $storeManager
    )

    $this->tree = $tree;
    $this->categoryRepository = $categoryRepository;
    $this->storeManager = $storeManager;

    public function getTreeByCategoryId()

    $storeId = $this->storeManager->getStore()->getId();
    $categoryId = 45;
    $category = $this->categoryRepository->get($categoryId, $storeId);
    $categoryTree = $this->tree->setStoreId($storeId)->loadBreadcrumbsArray($category->getPath());

    $categoryTreepath = '';
    foreach($categoryTree as $eachCategory)
    echo $category['name'];
    echo '<pre>';
    print_r($eachCategory);
    $categoryTreepath = $categoryTreepath. '/'.$category['name'];









    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Jun 3 at 7:39









    Amit BeraAmit Bera

    61k1682181




    61k1682181












    • I have marked yours answer as it helped a lot, however I had to make some minor changes to the code that I will post in my original "question". Thank you!

      – Oktarin
      Jun 3 at 15:48


















    • I have marked yours answer as it helped a lot, however I had to make some minor changes to the code that I will post in my original "question". Thank you!

      – Oktarin
      Jun 3 at 15:48

















    I have marked yours answer as it helped a lot, however I had to make some minor changes to the code that I will post in my original "question". Thank you!

    – Oktarin
    Jun 3 at 15:48






    I have marked yours answer as it helped a lot, however I had to make some minor changes to the code that I will post in my original "question". Thank you!

    – Oktarin
    Jun 3 at 15:48














    2














    You can get breaducrum of category by category Id using MagentoCatalogModelCategoryFactory as follows:



    ...
    protected $_categoryFactory;
    ...
    public function __construct(
    MagentoCatalogModelCategoryFactory $categoryFactory
    )
    $this->_categoryFactory = $categoryFactory;


    public function execute()
    //Load the product categories
    $categories = $product_data->getCategoryIds();
    //Select the last category in the list
    $categoryId = end($categories);
    $category = $this->_categoryFactory->create()->load($categoryId);
    // Parent Categories
    $parentCategories = $category->getParentCategories();

    foreach ($parentCategories as $cat)
    echo $cat->getId() . ": " . $cat->getName() . " :: " . $cat->getUrl() . "<br />";




    I hope this may helpful to anyone!






    share|improve this answer



























      2














      You can get breaducrum of category by category Id using MagentoCatalogModelCategoryFactory as follows:



      ...
      protected $_categoryFactory;
      ...
      public function __construct(
      MagentoCatalogModelCategoryFactory $categoryFactory
      )
      $this->_categoryFactory = $categoryFactory;


      public function execute()
      //Load the product categories
      $categories = $product_data->getCategoryIds();
      //Select the last category in the list
      $categoryId = end($categories);
      $category = $this->_categoryFactory->create()->load($categoryId);
      // Parent Categories
      $parentCategories = $category->getParentCategories();

      foreach ($parentCategories as $cat)
      echo $cat->getId() . ": " . $cat->getName() . " :: " . $cat->getUrl() . "<br />";




      I hope this may helpful to anyone!






      share|improve this answer

























        2












        2








        2







        You can get breaducrum of category by category Id using MagentoCatalogModelCategoryFactory as follows:



        ...
        protected $_categoryFactory;
        ...
        public function __construct(
        MagentoCatalogModelCategoryFactory $categoryFactory
        )
        $this->_categoryFactory = $categoryFactory;


        public function execute()
        //Load the product categories
        $categories = $product_data->getCategoryIds();
        //Select the last category in the list
        $categoryId = end($categories);
        $category = $this->_categoryFactory->create()->load($categoryId);
        // Parent Categories
        $parentCategories = $category->getParentCategories();

        foreach ($parentCategories as $cat)
        echo $cat->getId() . ": " . $cat->getName() . " :: " . $cat->getUrl() . "<br />";




        I hope this may helpful to anyone!






        share|improve this answer













        You can get breaducrum of category by category Id using MagentoCatalogModelCategoryFactory as follows:



        ...
        protected $_categoryFactory;
        ...
        public function __construct(
        MagentoCatalogModelCategoryFactory $categoryFactory
        )
        $this->_categoryFactory = $categoryFactory;


        public function execute()
        //Load the product categories
        $categories = $product_data->getCategoryIds();
        //Select the last category in the list
        $categoryId = end($categories);
        $category = $this->_categoryFactory->create()->load($categoryId);
        // Parent Categories
        $parentCategories = $category->getParentCategories();

        foreach ($parentCategories as $cat)
        echo $cat->getId() . ": " . $cat->getName() . " :: " . $cat->getUrl() . "<br />";




        I hope this may helpful to anyone!







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jun 3 at 8:29









        Dhara BhattiDhara Bhatti

        453112




        453112



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Magento Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f277013%2fmagento-2-how-to-get-category-name-and-category-tree-for-product-or-for-catego%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Category:9 (number) SubcategoriesMedia in category "9 (number)"Navigation menuUpload mediaGND ID: 4485639-8Library of Congress authority ID: sh85091979ReasonatorScholiaStatistics

            Circuit construction for execution of conditional statements using least significant bitHow are two different registers being used as “control”?How exactly is the stated composite state of the two registers being produced using the $R_zz$ controlled rotations?Efficiently performing controlled rotations in HHLWould this quantum algorithm implementation work?How to prepare a superposed states of odd integers from $1$ to $sqrtN$?Why is this implementation of the order finding algorithm not working?Circuit construction for Hamiltonian simulationHow can I invert the least significant bit of a certain term of a superposed state?Implementing an oracleImplementing a controlled sum operation

            Magento 2 “No Payment Methods” in Admin New OrderHow to integrate Paypal Express Checkout with the Magento APIMagento 1.5 - Sales > Order > edit order and shipping methods disappearAuto Invoice Check/Money Order Payment methodAdd more simple payment methods?Shipping methods not showingWhat should I do to change payment methods if changing the configuration has no effects?1.9 - No Payment Methods showing upMy Payment Methods not Showing for downloadable/virtual product when checkout?Magento2 API to access internal payment methodHow to call an existing payment methods in the registration form?