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
Font hinting is lost in Chrome-like browsers (for some languages )
What does it mean to describe someone as a butt steak?
Arthur Somervell: 1000 Exercises - Meaning of this notation
How is it possible to have an ability score that is less than 3?
TGV timetables / schedules?
Prove that NP is closed under karp reduction?
Can an x86 CPU running in real mode be considered to be basically an 8086 CPU?
Is this a crack on the carbon frame?
Did Shadowfax go to Valinor?
LaTeX closing $ signs makes cursor jump
What do you call a Matrix-like slowdown and camera movement effect?
What defenses are there against being summoned by the Gate spell?
An academic/student plagiarism
Why, historically, did Gödel think CH was false?
Is it unprofessional to ask if a job posting on GlassDoor is real?
can i play a electric guitar through a bass amp?
How to find program name(s) of an installed package?
How does strength of boric acid solution increase in presence of salicylic acid?
Is it important to consider tone, melody, and musical form while writing a song?
Show that if two triangles built on parallel lines, with equal bases have the same perimeter only if they are congruent.
Why Is Death Allowed In the Matrix?
Maximum likelihood parameters deviate from posterior distributions
To string or not to string
The use of multiple foreign keys on same column in SQL Server
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,6431825
2,6431825
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 Apr 3 at 19:45
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