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;
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(" > ",$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
add a comment |
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(" > ",$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
have you checked this magento.stackexchange.com/questions/204269/…
– fmsthird
Jun 3 at 4:18
add a comment |
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(" > ",$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
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(" > ",$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
magento2 category category-products category-tree
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
add a comment |
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
add a comment |
2 Answers
2
active
oldest
votes
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'];
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
add a comment |
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!
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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'];
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
add a comment |
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'];
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
add a comment |
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'];
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'];
answered Jun 3 at 7:39
Amit Bera♦Amit 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
add a comment |
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
add a comment |
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!
add a comment |
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!
add a comment |
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!
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!
answered Jun 3 at 8:29
Dhara BhattiDhara Bhatti
453112
453112
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
have you checked this magento.stackexchange.com/questions/204269/…
– fmsthird
Jun 3 at 4:18