Magento 2 - Get tree of specific category, child categories and product collectionget product collection and subcategory id using category id magento 2Get product collection by root category and all its subcategories in Magento 2?Get child categories tree when Flat Catalog Category is enabled in admin in Magento 2Magento 2: Display specific categories to specific usergroupsMagento 2: Display list of specific categories on Home PageMagento-2 How to change product collection based on categoriesHow to get category ids from product collectionMagento 2 : get second level categories of parent category except default category by using block?Magento 2 : I Want to Display All Catagory and Sub Category in Layerd Navigationmagento 2 get all category id of a specific store including all level nested child category
What's the difference between 'rename' and 'mv'?
Today is the Center
Do I have a twin with permutated remainders?
What is a clear way to write a bar that has an extra beat?
What do you call someone who asks many questions?
What mechanic is there to disable a threat instead of killing it?
Watching something be written to a file live with tail
Twin primes whose sum is a cube
I Accidentally Deleted a Stock Terminal Theme
Why "Having chlorophyll without photosynthesis is actually very dangerous" and "like living with a bomb"?
Assassin's bullet with mercury
Can a rocket refuel on Mars from water?
In Romance of the Three Kingdoms why do people still use bamboo sticks when paper had already been invented?
What is going on with Captain Marvel's blood colour?
Doing something right before you need it - expression for this?
Where does SFDX store details about scratch orgs?
Why does Arabsat 6A need a Falcon Heavy to launch
Brothers & sisters
How can I prevent hyper evolved versions of regular creatures from wiping out their cousins?
How can I make my BBEG immortal short of making them a Lich or Vampire?
Arrow those variables!
How can I tell someone that I want to be his or her friend?
Why was the shrinking from 8″ made only to 5.25″ and not smaller (4″ or less)?
Does a druid starting with a bow start with no arrows?
Magento 2 - Get tree of specific category, child categories and product collection
get product collection and subcategory id using category id magento 2Get product collection by root category and all its subcategories in Magento 2?Get child categories tree when Flat Catalog Category is enabled in admin in Magento 2Magento 2: Display specific categories to specific usergroupsMagento 2: Display list of specific categories on Home PageMagento-2 How to change product collection based on categoriesHow to get category ids from product collectionMagento 2 : get second level categories of parent category except default category by using block?Magento 2 : I Want to Display All Catagory and Sub Category in Layerd Navigationmagento 2 get all category id of a specific store including all level nested child category
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I have specific categorize and I want to create a menu from this specific categories with categories children and display product name.. example
Specific root categories
subcategory
product
product
subcategory
product
product
product
product
subcategorie
product
product
subcategory
product
product
product
product
product
subcategorie
product
product
product
subcategorie
subcategorie
subcategorie
subcategorie
subcategorie
subcategorie
subcategorie
magento2
add a comment |
I have specific categorize and I want to create a menu from this specific categories with categories children and display product name.. example
Specific root categories
subcategory
product
product
subcategory
product
product
product
product
subcategorie
product
product
subcategory
product
product
product
product
product
subcategorie
product
product
product
subcategorie
subcategorie
subcategorie
subcategorie
subcategorie
subcategorie
subcategorie
magento2
add a comment |
I have specific categorize and I want to create a menu from this specific categories with categories children and display product name.. example
Specific root categories
subcategory
product
product
subcategory
product
product
product
product
subcategorie
product
product
subcategory
product
product
product
product
product
subcategorie
product
product
product
subcategorie
subcategorie
subcategorie
subcategorie
subcategorie
subcategorie
subcategorie
magento2
I have specific categorize and I want to create a menu from this specific categories with categories children and display product name.. example
Specific root categories
subcategory
product
product
subcategory
product
product
product
product
subcategorie
product
product
subcategory
product
product
product
product
product
subcategorie
product
product
product
subcategorie
subcategorie
subcategorie
subcategorie
subcategorie
subcategorie
subcategorie
magento2
magento2
edited Apr 17 '18 at 9:07
Emipro Technologies Pvt. Ltd.
2,6611825
2,6611825
asked Apr 16 '18 at 20:04
MIGUEL ANGEL RAMIREZ MEDELMIGUEL ANGEL RAMIREZ MEDEL
62
62
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
en block class add
use MagentoFrameworkViewElementTemplateContext;
use MagentoCatalogModelCategoryRepository;
use MagentoCatalogModelCategoryFactory;
class Menu extends MagentoFrameworkViewElementTemplate
protected $categoryRepository;
protected $_categoryFactory;
public function __construct(
Context $context,
CategoryRepository $categoryRepository,
CategoryFactory $categoryFactory
)
$this->categoryRepository = $categoryRepository;
$this->_categoryFactory = $categoryFactory;
parent::__construct($context);
public function getParentCategorie($categoryId)
return $this->categoryRepository->get($categoryId);
public function getSubcategories($categoryId)
return $this->getParentCategorie($categoryId)->getChildrenCategories();
public function getCategoryName($categoryId)
$category = $this->_categoryFactory->create()->load($categoryId);
$categoryName = $category->getName();
return $categoryName;
public function getCategoryProduts($categoryId)
$collection = $this->_categoryFactory->create()->load($categoryId)->getProductCollection()->addAttributeToSelect('*')->addAttributeToSort('position', 'asc');
return $collection;
/*public function getsubCategories($catId)
$cat = $this->_categoryFactory->load($catId);
$subcats = $cat->getChildren();
$subcategories = array();
foreach(explode(',',$subcats) as $subCatid)
$_subCategory = $this->_categoryFactory->load($subCatid);
if($_subCategory->getIsActive())
$subcategories[] = array('id'=>$_subCategory->getId(),'name'=>$_subCategory->getName());
return $subcategories;
*/
public function getCategory($categoryId)
return $this->_categoryRepository->get($categoryId);
en phptml use
<?php $parent_category_id = 20 ?>
<?php $subCategories = $block->getSubcategories($parent_category_id) ?>
<b><?php echo $block->getCategoryName($parent_category_id); ?></b>
<ul>
<?php foreach ($subCategories as $subCategorie): ?>
<li><a href="<?php echo $subCategorie->getUrl() ?>"><?php echo $subCategorie->getName() ?></a></li>
<?php if($subCategorie->getProductCount() > 0): ?>
<?php $productCollection = $block->getCategoryProduts($subCategorie->getId()); ?>
<ul>
<?php foreach($productCollection as $_product): ?>
<li><a href="<?php echo $_product->getProductUrl() ?>"><?php echo $_product->getName()?></a></li>
<?php endforeach ?>
</ul>
<?php endif ?>
<?php if ($subCategorie->hasChildren()): ?>
<?php $childSubCategories = $block->getSubcategories($subCategorie->getId()); ?>
<ul>
<?php foreach ($childSubCategories as $childSubCategorie): ?>
<li><a href="<?php echo $childSubCategorie->getUrl() ?>"><?php echo $childSubCategorie->getName() ?></a></li>
<?php if($childSubCategorie->getProductCount() > 0): ?>
<?php $childproductCollection = $block->getCategoryProduts($childSubCategorie->getId()); ?>
<ul>
<?php foreach($childproductCollection as $_item): ?>
<li><a href="<?php echo $_item->getProductUrl() ?>"><?php echo $_item->getName()?></a></li>
<?php endforeach ?>
</ul>
<?php endif ?>
<?php endforeach ?>
</ul>
<?php endif ?>
<?php endforeach ?>
</ul>
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%2f222535%2fmagento-2-get-tree-of-specific-category-child-categories-and-product-collecti%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
en block class add
use MagentoFrameworkViewElementTemplateContext;
use MagentoCatalogModelCategoryRepository;
use MagentoCatalogModelCategoryFactory;
class Menu extends MagentoFrameworkViewElementTemplate
protected $categoryRepository;
protected $_categoryFactory;
public function __construct(
Context $context,
CategoryRepository $categoryRepository,
CategoryFactory $categoryFactory
)
$this->categoryRepository = $categoryRepository;
$this->_categoryFactory = $categoryFactory;
parent::__construct($context);
public function getParentCategorie($categoryId)
return $this->categoryRepository->get($categoryId);
public function getSubcategories($categoryId)
return $this->getParentCategorie($categoryId)->getChildrenCategories();
public function getCategoryName($categoryId)
$category = $this->_categoryFactory->create()->load($categoryId);
$categoryName = $category->getName();
return $categoryName;
public function getCategoryProduts($categoryId)
$collection = $this->_categoryFactory->create()->load($categoryId)->getProductCollection()->addAttributeToSelect('*')->addAttributeToSort('position', 'asc');
return $collection;
/*public function getsubCategories($catId)
$cat = $this->_categoryFactory->load($catId);
$subcats = $cat->getChildren();
$subcategories = array();
foreach(explode(',',$subcats) as $subCatid)
$_subCategory = $this->_categoryFactory->load($subCatid);
if($_subCategory->getIsActive())
$subcategories[] = array('id'=>$_subCategory->getId(),'name'=>$_subCategory->getName());
return $subcategories;
*/
public function getCategory($categoryId)
return $this->_categoryRepository->get($categoryId);
en phptml use
<?php $parent_category_id = 20 ?>
<?php $subCategories = $block->getSubcategories($parent_category_id) ?>
<b><?php echo $block->getCategoryName($parent_category_id); ?></b>
<ul>
<?php foreach ($subCategories as $subCategorie): ?>
<li><a href="<?php echo $subCategorie->getUrl() ?>"><?php echo $subCategorie->getName() ?></a></li>
<?php if($subCategorie->getProductCount() > 0): ?>
<?php $productCollection = $block->getCategoryProduts($subCategorie->getId()); ?>
<ul>
<?php foreach($productCollection as $_product): ?>
<li><a href="<?php echo $_product->getProductUrl() ?>"><?php echo $_product->getName()?></a></li>
<?php endforeach ?>
</ul>
<?php endif ?>
<?php if ($subCategorie->hasChildren()): ?>
<?php $childSubCategories = $block->getSubcategories($subCategorie->getId()); ?>
<ul>
<?php foreach ($childSubCategories as $childSubCategorie): ?>
<li><a href="<?php echo $childSubCategorie->getUrl() ?>"><?php echo $childSubCategorie->getName() ?></a></li>
<?php if($childSubCategorie->getProductCount() > 0): ?>
<?php $childproductCollection = $block->getCategoryProduts($childSubCategorie->getId()); ?>
<ul>
<?php foreach($childproductCollection as $_item): ?>
<li><a href="<?php echo $_item->getProductUrl() ?>"><?php echo $_item->getName()?></a></li>
<?php endforeach ?>
</ul>
<?php endif ?>
<?php endforeach ?>
</ul>
<?php endif ?>
<?php endforeach ?>
</ul>
add a comment |
en block class add
use MagentoFrameworkViewElementTemplateContext;
use MagentoCatalogModelCategoryRepository;
use MagentoCatalogModelCategoryFactory;
class Menu extends MagentoFrameworkViewElementTemplate
protected $categoryRepository;
protected $_categoryFactory;
public function __construct(
Context $context,
CategoryRepository $categoryRepository,
CategoryFactory $categoryFactory
)
$this->categoryRepository = $categoryRepository;
$this->_categoryFactory = $categoryFactory;
parent::__construct($context);
public function getParentCategorie($categoryId)
return $this->categoryRepository->get($categoryId);
public function getSubcategories($categoryId)
return $this->getParentCategorie($categoryId)->getChildrenCategories();
public function getCategoryName($categoryId)
$category = $this->_categoryFactory->create()->load($categoryId);
$categoryName = $category->getName();
return $categoryName;
public function getCategoryProduts($categoryId)
$collection = $this->_categoryFactory->create()->load($categoryId)->getProductCollection()->addAttributeToSelect('*')->addAttributeToSort('position', 'asc');
return $collection;
/*public function getsubCategories($catId)
$cat = $this->_categoryFactory->load($catId);
$subcats = $cat->getChildren();
$subcategories = array();
foreach(explode(',',$subcats) as $subCatid)
$_subCategory = $this->_categoryFactory->load($subCatid);
if($_subCategory->getIsActive())
$subcategories[] = array('id'=>$_subCategory->getId(),'name'=>$_subCategory->getName());
return $subcategories;
*/
public function getCategory($categoryId)
return $this->_categoryRepository->get($categoryId);
en phptml use
<?php $parent_category_id = 20 ?>
<?php $subCategories = $block->getSubcategories($parent_category_id) ?>
<b><?php echo $block->getCategoryName($parent_category_id); ?></b>
<ul>
<?php foreach ($subCategories as $subCategorie): ?>
<li><a href="<?php echo $subCategorie->getUrl() ?>"><?php echo $subCategorie->getName() ?></a></li>
<?php if($subCategorie->getProductCount() > 0): ?>
<?php $productCollection = $block->getCategoryProduts($subCategorie->getId()); ?>
<ul>
<?php foreach($productCollection as $_product): ?>
<li><a href="<?php echo $_product->getProductUrl() ?>"><?php echo $_product->getName()?></a></li>
<?php endforeach ?>
</ul>
<?php endif ?>
<?php if ($subCategorie->hasChildren()): ?>
<?php $childSubCategories = $block->getSubcategories($subCategorie->getId()); ?>
<ul>
<?php foreach ($childSubCategories as $childSubCategorie): ?>
<li><a href="<?php echo $childSubCategorie->getUrl() ?>"><?php echo $childSubCategorie->getName() ?></a></li>
<?php if($childSubCategorie->getProductCount() > 0): ?>
<?php $childproductCollection = $block->getCategoryProduts($childSubCategorie->getId()); ?>
<ul>
<?php foreach($childproductCollection as $_item): ?>
<li><a href="<?php echo $_item->getProductUrl() ?>"><?php echo $_item->getName()?></a></li>
<?php endforeach ?>
</ul>
<?php endif ?>
<?php endforeach ?>
</ul>
<?php endif ?>
<?php endforeach ?>
</ul>
add a comment |
en block class add
use MagentoFrameworkViewElementTemplateContext;
use MagentoCatalogModelCategoryRepository;
use MagentoCatalogModelCategoryFactory;
class Menu extends MagentoFrameworkViewElementTemplate
protected $categoryRepository;
protected $_categoryFactory;
public function __construct(
Context $context,
CategoryRepository $categoryRepository,
CategoryFactory $categoryFactory
)
$this->categoryRepository = $categoryRepository;
$this->_categoryFactory = $categoryFactory;
parent::__construct($context);
public function getParentCategorie($categoryId)
return $this->categoryRepository->get($categoryId);
public function getSubcategories($categoryId)
return $this->getParentCategorie($categoryId)->getChildrenCategories();
public function getCategoryName($categoryId)
$category = $this->_categoryFactory->create()->load($categoryId);
$categoryName = $category->getName();
return $categoryName;
public function getCategoryProduts($categoryId)
$collection = $this->_categoryFactory->create()->load($categoryId)->getProductCollection()->addAttributeToSelect('*')->addAttributeToSort('position', 'asc');
return $collection;
/*public function getsubCategories($catId)
$cat = $this->_categoryFactory->load($catId);
$subcats = $cat->getChildren();
$subcategories = array();
foreach(explode(',',$subcats) as $subCatid)
$_subCategory = $this->_categoryFactory->load($subCatid);
if($_subCategory->getIsActive())
$subcategories[] = array('id'=>$_subCategory->getId(),'name'=>$_subCategory->getName());
return $subcategories;
*/
public function getCategory($categoryId)
return $this->_categoryRepository->get($categoryId);
en phptml use
<?php $parent_category_id = 20 ?>
<?php $subCategories = $block->getSubcategories($parent_category_id) ?>
<b><?php echo $block->getCategoryName($parent_category_id); ?></b>
<ul>
<?php foreach ($subCategories as $subCategorie): ?>
<li><a href="<?php echo $subCategorie->getUrl() ?>"><?php echo $subCategorie->getName() ?></a></li>
<?php if($subCategorie->getProductCount() > 0): ?>
<?php $productCollection = $block->getCategoryProduts($subCategorie->getId()); ?>
<ul>
<?php foreach($productCollection as $_product): ?>
<li><a href="<?php echo $_product->getProductUrl() ?>"><?php echo $_product->getName()?></a></li>
<?php endforeach ?>
</ul>
<?php endif ?>
<?php if ($subCategorie->hasChildren()): ?>
<?php $childSubCategories = $block->getSubcategories($subCategorie->getId()); ?>
<ul>
<?php foreach ($childSubCategories as $childSubCategorie): ?>
<li><a href="<?php echo $childSubCategorie->getUrl() ?>"><?php echo $childSubCategorie->getName() ?></a></li>
<?php if($childSubCategorie->getProductCount() > 0): ?>
<?php $childproductCollection = $block->getCategoryProduts($childSubCategorie->getId()); ?>
<ul>
<?php foreach($childproductCollection as $_item): ?>
<li><a href="<?php echo $_item->getProductUrl() ?>"><?php echo $_item->getName()?></a></li>
<?php endforeach ?>
</ul>
<?php endif ?>
<?php endforeach ?>
</ul>
<?php endif ?>
<?php endforeach ?>
</ul>
en block class add
use MagentoFrameworkViewElementTemplateContext;
use MagentoCatalogModelCategoryRepository;
use MagentoCatalogModelCategoryFactory;
class Menu extends MagentoFrameworkViewElementTemplate
protected $categoryRepository;
protected $_categoryFactory;
public function __construct(
Context $context,
CategoryRepository $categoryRepository,
CategoryFactory $categoryFactory
)
$this->categoryRepository = $categoryRepository;
$this->_categoryFactory = $categoryFactory;
parent::__construct($context);
public function getParentCategorie($categoryId)
return $this->categoryRepository->get($categoryId);
public function getSubcategories($categoryId)
return $this->getParentCategorie($categoryId)->getChildrenCategories();
public function getCategoryName($categoryId)
$category = $this->_categoryFactory->create()->load($categoryId);
$categoryName = $category->getName();
return $categoryName;
public function getCategoryProduts($categoryId)
$collection = $this->_categoryFactory->create()->load($categoryId)->getProductCollection()->addAttributeToSelect('*')->addAttributeToSort('position', 'asc');
return $collection;
/*public function getsubCategories($catId)
$cat = $this->_categoryFactory->load($catId);
$subcats = $cat->getChildren();
$subcategories = array();
foreach(explode(',',$subcats) as $subCatid)
$_subCategory = $this->_categoryFactory->load($subCatid);
if($_subCategory->getIsActive())
$subcategories[] = array('id'=>$_subCategory->getId(),'name'=>$_subCategory->getName());
return $subcategories;
*/
public function getCategory($categoryId)
return $this->_categoryRepository->get($categoryId);
en phptml use
<?php $parent_category_id = 20 ?>
<?php $subCategories = $block->getSubcategories($parent_category_id) ?>
<b><?php echo $block->getCategoryName($parent_category_id); ?></b>
<ul>
<?php foreach ($subCategories as $subCategorie): ?>
<li><a href="<?php echo $subCategorie->getUrl() ?>"><?php echo $subCategorie->getName() ?></a></li>
<?php if($subCategorie->getProductCount() > 0): ?>
<?php $productCollection = $block->getCategoryProduts($subCategorie->getId()); ?>
<ul>
<?php foreach($productCollection as $_product): ?>
<li><a href="<?php echo $_product->getProductUrl() ?>"><?php echo $_product->getName()?></a></li>
<?php endforeach ?>
</ul>
<?php endif ?>
<?php if ($subCategorie->hasChildren()): ?>
<?php $childSubCategories = $block->getSubcategories($subCategorie->getId()); ?>
<ul>
<?php foreach ($childSubCategories as $childSubCategorie): ?>
<li><a href="<?php echo $childSubCategorie->getUrl() ?>"><?php echo $childSubCategorie->getName() ?></a></li>
<?php if($childSubCategorie->getProductCount() > 0): ?>
<?php $childproductCollection = $block->getCategoryProduts($childSubCategorie->getId()); ?>
<ul>
<?php foreach($childproductCollection as $_item): ?>
<li><a href="<?php echo $_item->getProductUrl() ?>"><?php echo $_item->getName()?></a></li>
<?php endforeach ?>
</ul>
<?php endif ?>
<?php endforeach ?>
</ul>
<?php endif ?>
<?php endforeach ?>
</ul>
answered yesterday
MIGUEL ANGEL RAMIREZ MEDELMIGUEL ANGEL RAMIREZ MEDEL
62
62
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%2f222535%2fmagento-2-get-tree-of-specific-category-child-categories-and-product-collecti%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