Get product collection by root category and all its subcategories in Magento 2?How to get list of categories including sub categories in Magento 2?How get product IDs of root category and subcategory under root category programmatically?Getting product filter by root categoryProduct collection for subcategories of subcategoriesHow to do a list with all categories and subcategories, from a given main-categoryhow to get magento categories and set values of categories while adding a product programatically?How to get Subcategory and its products with details in magentoget product collection and subcategory id using category id magento 2Configurable Product Collection and variantsHow to check the category collection is empty or not in magento 2?How to show all categories and their subcategories on Layered Navigation?

Can infringement of a trademark be pursued for using a company's name in a sentence?

Is all copper pipe pretty much the same?

Making a sword in the stone, in a medieval world without magic

When were linguistics departments first established

Is it illegal in Germany to take sick leave if you caused your own illness with food?

Running a subshell from the middle of the current command

Life insurance that covers only simultaneous/dual deaths

Why does Deadpool say "You're welcome, Canada," after shooting Ryan Reynolds in the end credits?

redhat 7 + How to stop systemctl service permanent

Playing ONE triplet (not three)

What happens with multiple copies of Humility and Glorious Anthem on the battlefield?

Latest web browser compatible with Windows 98

Excess Zinc in garden soil

Word for a person who has no opinion about whether god exists

Make a transparent 448*448 image

What to do when during a meeting client people start to fight (even physically) with each others?

Touchscreen-controlled dentist office snowman collector game

Want to switch to tankless, but can I use my existing wiring?

Best mythical creature to use as livestock?

Silly Sally's Movie

Time travel short story where dinosaur doesn't taste like chicken

"One can do his homework in the library"

How to deal with a cynical class?

Why doesn't the EU now just force the UK to choose between referendum and no-deal?



Get product collection by root category and all its subcategories in Magento 2?


How to get list of categories including sub categories in Magento 2?How get product IDs of root category and subcategory under root category programmatically?Getting product filter by root categoryProduct collection for subcategories of subcategoriesHow to do a list with all categories and subcategories, from a given main-categoryhow to get magento categories and set values of categories while adding a product programatically?How to get Subcategory and its products with details in magentoget product collection and subcategory id using category id magento 2Configurable Product Collection and variantsHow to check the category collection is empty or not in magento 2?How to show all categories and their subcategories on Layered Navigation?













6















How can I retrieve a product collection by a root category and from all its subcategories?



Eg:



Root Category (2 products)




  • Subcategory 1 (2 products)


  • Subcategory 2 (3 products)

So I want to retrieve all 7 products in the collection.










share|improve this question




























    6















    How can I retrieve a product collection by a root category and from all its subcategories?



    Eg:



    Root Category (2 products)




    • Subcategory 1 (2 products)


    • Subcategory 2 (3 products)

    So I want to retrieve all 7 products in the collection.










    share|improve this question


























      6












      6








      6








      How can I retrieve a product collection by a root category and from all its subcategories?



      Eg:



      Root Category (2 products)




      • Subcategory 1 (2 products)


      • Subcategory 2 (3 products)

      So I want to retrieve all 7 products in the collection.










      share|improve this question
















      How can I retrieve a product collection by a root category and from all its subcategories?



      Eg:



      Root Category (2 products)




      • Subcategory 1 (2 products)


      • Subcategory 2 (3 products)

      So I want to retrieve all 7 products in the collection.







      magento2 product category collection






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 12 mins ago









      thedash

      1037




      1037










      asked Sep 15 '16 at 12:20









      nuwausnuwaus

      91931945




      91931945




















          5 Answers
          5






          active

          oldest

          votes


















          3














          You can use like this:



          /** MagentoCatalogApiCategoryRepositoryInterface */
          protected $categoryRepository;

          /** MagentoCatalogModelResourceModelProductCollectionFactory */
          protected $productCollectionFactory;

          public function getAllProductOfSubcategories($categoryId, $storeId = null)

          /** @var $result MagentoCatalogModelResourceModelProductCollection */
          $result = $this->productCollectionFactory->create();

          //get category at $storeId
          try
          $category = $this->categoryRepository->get($categoryId, $storeId);
          catch (MagentoFrameworkExceptionNoSuchEntityException $noSuchEntityException)
          return null;


          return $result->addCategoryFilter($category);



          *Note: Your category must Enable Anchor



          Enable Anchor for Category



          *Note: run php bin/magento indexer:reindex just for make sure






          share|improve this answer
































            2














            Code for your class file:



            protected $_categoryHelper;
            protected $_categoryRepository;

            public function __construct(
            MagentoCatalogHelperCategory $categoryHelper,
            MagentoCatalogModelCategoryRepository $categoryRepository,
            array $data = []
            )

            $this->_categoryHelper = $categoryHelper;
            $this->_categoryCategoryRepository = $categoryRepository;
            parent::__construct($context, $data);


            public function getStoreCategories()

            return $this->_categoryHelper->getStoreCategories();


            public function getCategory($categoryId)

            return $this->_categoryRepository->get($categoryId);



            Code for your template file:



            $categories = $block->getStoreCategories();
            foreach ($categories as $category)
            echo $category->getName();
            echo ' ( ' . $category->getProductCount() . ' )';

            $subCategories = $block->getCategory($category->getId());
            foreach ($subCategories as $subCategory)
            echo $subCategory->getName();
            echo ' ( ' . $subCategory->getProductCount() . ' )';




            Source: Magento 2: Get parent category, children categories & product count






            share|improve this answer
































              1














              I solved it as below,



              protected $_category;
              protected $_productCollection;

              /** You should provide your root category here, and it will return comma seperated sub category list */
              public function getChildren($categoryId = false)

              if ($this->_category)
              return $this->_category->getChildren();
              else
              return $this->getCategory($categoryId)->getChildren();



              protected function _getProductCollection()

              $childListStr = $this->getChildren( 2 ); // Provide the root category ID
              $childList = explode( ",", $childListStr );
              $catToLoad = array();

              foreach( $childList as $item )
              array_push( $catToLoad, $item );


              if ($this->_productCollection === null)
              $layer = $this->getLayer();
              $this->_productCollection = $layer->getProductCollection();


              $this->_productCollection->addCategoriesFilter(['in' => $catToLoad ]);
              return $this->_productCollection;






              share|improve this answer






























                0














                Hi I have another way to get product collection from root category... check it out.. I hope this help



                public function __construct(
                MagentoCatalogModelLayerCategory $categoryLayer
                )
                $this->_categoryLayer = $categoryLayer;


                public function getProductCollection($category)
                return $this->_categoryLayer->setCurrentCategory($category)->getProductCollection();






                share|improve this answer






























                  -4














                  Try this



                   $category = Mage::getModel('catalog/category')->load(2);
                  $children = Mage::getModel('catalog/category')->getCollection()->setStoreId(Mage::app()->getStore()->getId());
                  $children->addAttributeToSelect('*')
                  ->addAttributeToFilter('parent_id', $category->getId())
                  ->addAttributeToFilter('is_active', 1)
                  ->addAttributeToSort('position');
                  foreach($children as $child)


                  $category=Mage::getModel('catalog/category')->load($child->entity_id);






                  share|improve this answer




















                  • 3





                    This is Magento 1 way.

                    – Khoa TruongDinh
                    Sep 15 '16 at 13:29










                  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%2f136517%2fget-product-collection-by-root-category-and-all-its-subcategories-in-magento-2%23new-answer', 'question_page');

                  );

                  Post as a guest















                  Required, but never shown

























                  5 Answers
                  5






                  active

                  oldest

                  votes








                  5 Answers
                  5






                  active

                  oldest

                  votes









                  active

                  oldest

                  votes






                  active

                  oldest

                  votes









                  3














                  You can use like this:



                  /** MagentoCatalogApiCategoryRepositoryInterface */
                  protected $categoryRepository;

                  /** MagentoCatalogModelResourceModelProductCollectionFactory */
                  protected $productCollectionFactory;

                  public function getAllProductOfSubcategories($categoryId, $storeId = null)

                  /** @var $result MagentoCatalogModelResourceModelProductCollection */
                  $result = $this->productCollectionFactory->create();

                  //get category at $storeId
                  try
                  $category = $this->categoryRepository->get($categoryId, $storeId);
                  catch (MagentoFrameworkExceptionNoSuchEntityException $noSuchEntityException)
                  return null;


                  return $result->addCategoryFilter($category);



                  *Note: Your category must Enable Anchor



                  Enable Anchor for Category



                  *Note: run php bin/magento indexer:reindex just for make sure






                  share|improve this answer





























                    3














                    You can use like this:



                    /** MagentoCatalogApiCategoryRepositoryInterface */
                    protected $categoryRepository;

                    /** MagentoCatalogModelResourceModelProductCollectionFactory */
                    protected $productCollectionFactory;

                    public function getAllProductOfSubcategories($categoryId, $storeId = null)

                    /** @var $result MagentoCatalogModelResourceModelProductCollection */
                    $result = $this->productCollectionFactory->create();

                    //get category at $storeId
                    try
                    $category = $this->categoryRepository->get($categoryId, $storeId);
                    catch (MagentoFrameworkExceptionNoSuchEntityException $noSuchEntityException)
                    return null;


                    return $result->addCategoryFilter($category);



                    *Note: Your category must Enable Anchor



                    Enable Anchor for Category



                    *Note: run php bin/magento indexer:reindex just for make sure






                    share|improve this answer



























                      3












                      3








                      3







                      You can use like this:



                      /** MagentoCatalogApiCategoryRepositoryInterface */
                      protected $categoryRepository;

                      /** MagentoCatalogModelResourceModelProductCollectionFactory */
                      protected $productCollectionFactory;

                      public function getAllProductOfSubcategories($categoryId, $storeId = null)

                      /** @var $result MagentoCatalogModelResourceModelProductCollection */
                      $result = $this->productCollectionFactory->create();

                      //get category at $storeId
                      try
                      $category = $this->categoryRepository->get($categoryId, $storeId);
                      catch (MagentoFrameworkExceptionNoSuchEntityException $noSuchEntityException)
                      return null;


                      return $result->addCategoryFilter($category);



                      *Note: Your category must Enable Anchor



                      Enable Anchor for Category



                      *Note: run php bin/magento indexer:reindex just for make sure






                      share|improve this answer















                      You can use like this:



                      /** MagentoCatalogApiCategoryRepositoryInterface */
                      protected $categoryRepository;

                      /** MagentoCatalogModelResourceModelProductCollectionFactory */
                      protected $productCollectionFactory;

                      public function getAllProductOfSubcategories($categoryId, $storeId = null)

                      /** @var $result MagentoCatalogModelResourceModelProductCollection */
                      $result = $this->productCollectionFactory->create();

                      //get category at $storeId
                      try
                      $category = $this->categoryRepository->get($categoryId, $storeId);
                      catch (MagentoFrameworkExceptionNoSuchEntityException $noSuchEntityException)
                      return null;


                      return $result->addCategoryFilter($category);



                      *Note: Your category must Enable Anchor



                      Enable Anchor for Category



                      *Note: run php bin/magento indexer:reindex just for make sure







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Sep 18 '17 at 2:35

























                      answered Aug 16 '17 at 4:27









                      RubeliaRubelia

                      565




                      565























                          2














                          Code for your class file:



                          protected $_categoryHelper;
                          protected $_categoryRepository;

                          public function __construct(
                          MagentoCatalogHelperCategory $categoryHelper,
                          MagentoCatalogModelCategoryRepository $categoryRepository,
                          array $data = []
                          )

                          $this->_categoryHelper = $categoryHelper;
                          $this->_categoryCategoryRepository = $categoryRepository;
                          parent::__construct($context, $data);


                          public function getStoreCategories()

                          return $this->_categoryHelper->getStoreCategories();


                          public function getCategory($categoryId)

                          return $this->_categoryRepository->get($categoryId);



                          Code for your template file:



                          $categories = $block->getStoreCategories();
                          foreach ($categories as $category)
                          echo $category->getName();
                          echo ' ( ' . $category->getProductCount() . ' )';

                          $subCategories = $block->getCategory($category->getId());
                          foreach ($subCategories as $subCategory)
                          echo $subCategory->getName();
                          echo ' ( ' . $subCategory->getProductCount() . ' )';




                          Source: Magento 2: Get parent category, children categories & product count






                          share|improve this answer





























                            2














                            Code for your class file:



                            protected $_categoryHelper;
                            protected $_categoryRepository;

                            public function __construct(
                            MagentoCatalogHelperCategory $categoryHelper,
                            MagentoCatalogModelCategoryRepository $categoryRepository,
                            array $data = []
                            )

                            $this->_categoryHelper = $categoryHelper;
                            $this->_categoryCategoryRepository = $categoryRepository;
                            parent::__construct($context, $data);


                            public function getStoreCategories()

                            return $this->_categoryHelper->getStoreCategories();


                            public function getCategory($categoryId)

                            return $this->_categoryRepository->get($categoryId);



                            Code for your template file:



                            $categories = $block->getStoreCategories();
                            foreach ($categories as $category)
                            echo $category->getName();
                            echo ' ( ' . $category->getProductCount() . ' )';

                            $subCategories = $block->getCategory($category->getId());
                            foreach ($subCategories as $subCategory)
                            echo $subCategory->getName();
                            echo ' ( ' . $subCategory->getProductCount() . ' )';




                            Source: Magento 2: Get parent category, children categories & product count






                            share|improve this answer



























                              2












                              2








                              2







                              Code for your class file:



                              protected $_categoryHelper;
                              protected $_categoryRepository;

                              public function __construct(
                              MagentoCatalogHelperCategory $categoryHelper,
                              MagentoCatalogModelCategoryRepository $categoryRepository,
                              array $data = []
                              )

                              $this->_categoryHelper = $categoryHelper;
                              $this->_categoryCategoryRepository = $categoryRepository;
                              parent::__construct($context, $data);


                              public function getStoreCategories()

                              return $this->_categoryHelper->getStoreCategories();


                              public function getCategory($categoryId)

                              return $this->_categoryRepository->get($categoryId);



                              Code for your template file:



                              $categories = $block->getStoreCategories();
                              foreach ($categories as $category)
                              echo $category->getName();
                              echo ' ( ' . $category->getProductCount() . ' )';

                              $subCategories = $block->getCategory($category->getId());
                              foreach ($subCategories as $subCategory)
                              echo $subCategory->getName();
                              echo ' ( ' . $subCategory->getProductCount() . ' )';




                              Source: Magento 2: Get parent category, children categories & product count






                              share|improve this answer















                              Code for your class file:



                              protected $_categoryHelper;
                              protected $_categoryRepository;

                              public function __construct(
                              MagentoCatalogHelperCategory $categoryHelper,
                              MagentoCatalogModelCategoryRepository $categoryRepository,
                              array $data = []
                              )

                              $this->_categoryHelper = $categoryHelper;
                              $this->_categoryCategoryRepository = $categoryRepository;
                              parent::__construct($context, $data);


                              public function getStoreCategories()

                              return $this->_categoryHelper->getStoreCategories();


                              public function getCategory($categoryId)

                              return $this->_categoryRepository->get($categoryId);



                              Code for your template file:



                              $categories = $block->getStoreCategories();
                              foreach ($categories as $category)
                              echo $category->getName();
                              echo ' ( ' . $category->getProductCount() . ' )';

                              $subCategories = $block->getCategory($category->getId());
                              foreach ($subCategories as $subCategory)
                              echo $subCategory->getName();
                              echo ' ( ' . $subCategory->getProductCount() . ' )';




                              Source: Magento 2: Get parent category, children categories & product count







                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited Sep 16 '16 at 6:49

























                              answered Sep 16 '16 at 5:43









                              Mukesh ChapagainMukesh Chapagain

                              3,82812243




                              3,82812243





















                                  1














                                  I solved it as below,



                                  protected $_category;
                                  protected $_productCollection;

                                  /** You should provide your root category here, and it will return comma seperated sub category list */
                                  public function getChildren($categoryId = false)

                                  if ($this->_category)
                                  return $this->_category->getChildren();
                                  else
                                  return $this->getCategory($categoryId)->getChildren();



                                  protected function _getProductCollection()

                                  $childListStr = $this->getChildren( 2 ); // Provide the root category ID
                                  $childList = explode( ",", $childListStr );
                                  $catToLoad = array();

                                  foreach( $childList as $item )
                                  array_push( $catToLoad, $item );


                                  if ($this->_productCollection === null)
                                  $layer = $this->getLayer();
                                  $this->_productCollection = $layer->getProductCollection();


                                  $this->_productCollection->addCategoriesFilter(['in' => $catToLoad ]);
                                  return $this->_productCollection;






                                  share|improve this answer



























                                    1














                                    I solved it as below,



                                    protected $_category;
                                    protected $_productCollection;

                                    /** You should provide your root category here, and it will return comma seperated sub category list */
                                    public function getChildren($categoryId = false)

                                    if ($this->_category)
                                    return $this->_category->getChildren();
                                    else
                                    return $this->getCategory($categoryId)->getChildren();



                                    protected function _getProductCollection()

                                    $childListStr = $this->getChildren( 2 ); // Provide the root category ID
                                    $childList = explode( ",", $childListStr );
                                    $catToLoad = array();

                                    foreach( $childList as $item )
                                    array_push( $catToLoad, $item );


                                    if ($this->_productCollection === null)
                                    $layer = $this->getLayer();
                                    $this->_productCollection = $layer->getProductCollection();


                                    $this->_productCollection->addCategoriesFilter(['in' => $catToLoad ]);
                                    return $this->_productCollection;






                                    share|improve this answer

























                                      1












                                      1








                                      1







                                      I solved it as below,



                                      protected $_category;
                                      protected $_productCollection;

                                      /** You should provide your root category here, and it will return comma seperated sub category list */
                                      public function getChildren($categoryId = false)

                                      if ($this->_category)
                                      return $this->_category->getChildren();
                                      else
                                      return $this->getCategory($categoryId)->getChildren();



                                      protected function _getProductCollection()

                                      $childListStr = $this->getChildren( 2 ); // Provide the root category ID
                                      $childList = explode( ",", $childListStr );
                                      $catToLoad = array();

                                      foreach( $childList as $item )
                                      array_push( $catToLoad, $item );


                                      if ($this->_productCollection === null)
                                      $layer = $this->getLayer();
                                      $this->_productCollection = $layer->getProductCollection();


                                      $this->_productCollection->addCategoriesFilter(['in' => $catToLoad ]);
                                      return $this->_productCollection;






                                      share|improve this answer













                                      I solved it as below,



                                      protected $_category;
                                      protected $_productCollection;

                                      /** You should provide your root category here, and it will return comma seperated sub category list */
                                      public function getChildren($categoryId = false)

                                      if ($this->_category)
                                      return $this->_category->getChildren();
                                      else
                                      return $this->getCategory($categoryId)->getChildren();



                                      protected function _getProductCollection()

                                      $childListStr = $this->getChildren( 2 ); // Provide the root category ID
                                      $childList = explode( ",", $childListStr );
                                      $catToLoad = array();

                                      foreach( $childList as $item )
                                      array_push( $catToLoad, $item );


                                      if ($this->_productCollection === null)
                                      $layer = $this->getLayer();
                                      $this->_productCollection = $layer->getProductCollection();


                                      $this->_productCollection->addCategoriesFilter(['in' => $catToLoad ]);
                                      return $this->_productCollection;







                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Sep 16 '16 at 7:39









                                      nuwausnuwaus

                                      91931945




                                      91931945





















                                          0














                                          Hi I have another way to get product collection from root category... check it out.. I hope this help



                                          public function __construct(
                                          MagentoCatalogModelLayerCategory $categoryLayer
                                          )
                                          $this->_categoryLayer = $categoryLayer;


                                          public function getProductCollection($category)
                                          return $this->_categoryLayer->setCurrentCategory($category)->getProductCollection();






                                          share|improve this answer



























                                            0














                                            Hi I have another way to get product collection from root category... check it out.. I hope this help



                                            public function __construct(
                                            MagentoCatalogModelLayerCategory $categoryLayer
                                            )
                                            $this->_categoryLayer = $categoryLayer;


                                            public function getProductCollection($category)
                                            return $this->_categoryLayer->setCurrentCategory($category)->getProductCollection();






                                            share|improve this answer

























                                              0












                                              0








                                              0







                                              Hi I have another way to get product collection from root category... check it out.. I hope this help



                                              public function __construct(
                                              MagentoCatalogModelLayerCategory $categoryLayer
                                              )
                                              $this->_categoryLayer = $categoryLayer;


                                              public function getProductCollection($category)
                                              return $this->_categoryLayer->setCurrentCategory($category)->getProductCollection();






                                              share|improve this answer













                                              Hi I have another way to get product collection from root category... check it out.. I hope this help



                                              public function __construct(
                                              MagentoCatalogModelLayerCategory $categoryLayer
                                              )
                                              $this->_categoryLayer = $categoryLayer;


                                              public function getProductCollection($category)
                                              return $this->_categoryLayer->setCurrentCategory($category)->getProductCollection();







                                              share|improve this answer












                                              share|improve this answer



                                              share|improve this answer










                                              answered Nov 1 '16 at 7:57









                                              HoangHieuHoangHieu

                                              746514




                                              746514





















                                                  -4














                                                  Try this



                                                   $category = Mage::getModel('catalog/category')->load(2);
                                                  $children = Mage::getModel('catalog/category')->getCollection()->setStoreId(Mage::app()->getStore()->getId());
                                                  $children->addAttributeToSelect('*')
                                                  ->addAttributeToFilter('parent_id', $category->getId())
                                                  ->addAttributeToFilter('is_active', 1)
                                                  ->addAttributeToSort('position');
                                                  foreach($children as $child)


                                                  $category=Mage::getModel('catalog/category')->load($child->entity_id);






                                                  share|improve this answer




















                                                  • 3





                                                    This is Magento 1 way.

                                                    – Khoa TruongDinh
                                                    Sep 15 '16 at 13:29















                                                  -4














                                                  Try this



                                                   $category = Mage::getModel('catalog/category')->load(2);
                                                  $children = Mage::getModel('catalog/category')->getCollection()->setStoreId(Mage::app()->getStore()->getId());
                                                  $children->addAttributeToSelect('*')
                                                  ->addAttributeToFilter('parent_id', $category->getId())
                                                  ->addAttributeToFilter('is_active', 1)
                                                  ->addAttributeToSort('position');
                                                  foreach($children as $child)


                                                  $category=Mage::getModel('catalog/category')->load($child->entity_id);






                                                  share|improve this answer




















                                                  • 3





                                                    This is Magento 1 way.

                                                    – Khoa TruongDinh
                                                    Sep 15 '16 at 13:29













                                                  -4












                                                  -4








                                                  -4







                                                  Try this



                                                   $category = Mage::getModel('catalog/category')->load(2);
                                                  $children = Mage::getModel('catalog/category')->getCollection()->setStoreId(Mage::app()->getStore()->getId());
                                                  $children->addAttributeToSelect('*')
                                                  ->addAttributeToFilter('parent_id', $category->getId())
                                                  ->addAttributeToFilter('is_active', 1)
                                                  ->addAttributeToSort('position');
                                                  foreach($children as $child)


                                                  $category=Mage::getModel('catalog/category')->load($child->entity_id);






                                                  share|improve this answer















                                                  Try this



                                                   $category = Mage::getModel('catalog/category')->load(2);
                                                  $children = Mage::getModel('catalog/category')->getCollection()->setStoreId(Mage::app()->getStore()->getId());
                                                  $children->addAttributeToSelect('*')
                                                  ->addAttributeToFilter('parent_id', $category->getId())
                                                  ->addAttributeToFilter('is_active', 1)
                                                  ->addAttributeToSort('position');
                                                  foreach($children as $child)


                                                  $category=Mage::getModel('catalog/category')->load($child->entity_id);







                                                  share|improve this answer














                                                  share|improve this answer



                                                  share|improve this answer








                                                  edited Sep 16 '16 at 7:25









                                                  Murtuza Zabuawala

                                                  12.6k73362




                                                  12.6k73362










                                                  answered Sep 15 '16 at 13:13









                                                  Ravi ThankiRavi Thanki

                                                  34429




                                                  34429







                                                  • 3





                                                    This is Magento 1 way.

                                                    – Khoa TruongDinh
                                                    Sep 15 '16 at 13:29












                                                  • 3





                                                    This is Magento 1 way.

                                                    – Khoa TruongDinh
                                                    Sep 15 '16 at 13:29







                                                  3




                                                  3





                                                  This is Magento 1 way.

                                                  – Khoa TruongDinh
                                                  Sep 15 '16 at 13:29





                                                  This is Magento 1 way.

                                                  – Khoa TruongDinh
                                                  Sep 15 '16 at 13:29

















                                                  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%2f136517%2fget-product-collection-by-root-category-and-all-its-subcategories-in-magento-2%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

                                                  Can not update quote_id field of “quote_item” table magento 2Magento 2.1 - We can't remove the item. (Shopping Cart doesnt allow us to remove items before becomes empty)Add value for custom quote item attribute using REST apiREST API endpoint v1/carts/cartId/items always returns error messageCorrect way to save entries to databaseHow to remove all associated quote objects of a customer completelyMagento 2 - Save value from custom input field to quote_itemGet quote_item data using quote id and product id filter in Magento 2How to set additional data to quote_item table from controller in Magento 2?What is the purpose of additional_data column in quote_item table in magento2Set Custom Price to Quote item magento2 from controller

                                                  How to solve knockout JS error in Magento 2 Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Announcing the arrival of Valued Associate #679: Cesar Manara Unicorn Meta Zoo #1: Why another podcast?(Magento2) knockout.js:3012 Uncaught ReferenceError: Unable to process bindingUnable to process binding Knockout.js magento 2Cannot read property `scopeLabel` of undefined on Product Detail PageCan't get Customer Data on frontend in Magento 2Magento2 Order Summary - unable to process bindingKO templates are not loading in Magento 2.1 applicationgetting knockout js error magento 2Product grid not load -— Unable to process binding Knockout.js magento 2Product form not loaded in magento2Uncaught ReferenceError: Unable to process binding “if: function()return (isShowLegend()) ” magento 2

                                                  Nissan Patrol Зміст Перше покоління — 4W60 (1951-1960) | Друге покоління — 60 series (1960-1980) | Третє покоління (1980–2002) | Четверте покоління — Y60 (1987–1998) | П'яте покоління — Y61 (1997–2013) | Шосте покоління — Y62 (2010- ) | Посилання | Зноски | Навігаційне менюОфіційний український сайтТест-драйв Nissan Patrol 2010 7-го поколінняNissan PatrolКак мы тестировали Nissan Patrol 2016рвиправивши або дописавши її