Displaying subcategories instead of products 2.1












2














I am fairly new to Magento, and am having trouble figuring out what I need to add to do this. I have looked all over for this, but can not find a solution that shows how to do this for magento 2. I am looking for how to create a module that displays all of the subcategories of the category clicked. If there are no more subcategories, then I want to display the products for that category.



I have tried making blocks in the admin panel, and attaching these blocks to the product category. I would really like to just create a module that does this for me though.










share|improve this question





























    2














    I am fairly new to Magento, and am having trouble figuring out what I need to add to do this. I have looked all over for this, but can not find a solution that shows how to do this for magento 2. I am looking for how to create a module that displays all of the subcategories of the category clicked. If there are no more subcategories, then I want to display the products for that category.



    I have tried making blocks in the admin panel, and attaching these blocks to the product category. I would really like to just create a module that does this for me though.










    share|improve this question



























      2












      2








      2







      I am fairly new to Magento, and am having trouble figuring out what I need to add to do this. I have looked all over for this, but can not find a solution that shows how to do this for magento 2. I am looking for how to create a module that displays all of the subcategories of the category clicked. If there are no more subcategories, then I want to display the products for that category.



      I have tried making blocks in the admin panel, and attaching these blocks to the product category. I would really like to just create a module that does this for me though.










      share|improve this question















      I am fairly new to Magento, and am having trouble figuring out what I need to add to do this. I have looked all over for this, but can not find a solution that shows how to do this for magento 2. I am looking for how to create a module that displays all of the subcategories of the category clicked. If there are no more subcategories, then I want to display the products for that category.



      I have tried making blocks in the admin panel, and attaching these blocks to the product category. I would really like to just create a module that does this for me though.







      magento2 navigation






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited yesterday









      Teja Bhagavan Kollepara

      2,93841847




      2,93841847










      asked Oct 17 '16 at 15:18









      tadmintadmin

      1112




      1112






















          2 Answers
          2






          active

          oldest

          votes


















          2














          You need to customized the category in the theme folder use the
          following file structure and update the following code



          /app/design/frontend/<vendor>/theme/Magento_Catalog/templates/category

          products.phtml
          <?php

          // call current category and sub category

          $categoryHelper_sub = $this->helper('MagentoCatalogHelperCategory');
          $_category = $block->getCurrentCategory();

          $objectManager_sub = $objectManager =
          MagentoFrameworkAppObjectManager::getInstance();
          $imagehelper = $objectManager->create('MagentoCatalogHelperImage');
          $category = $objectManager_sub->get('MagentoCatalogModelCategory');
          $categories_sub =
          $category->getCollection()->addAttributeToSelect('*')->addAttributeToFilter('is_active',
          1)->addIdFilter($_category->getChildren())->setOrder('position', 'ASC');?>

          <?php if(!empty($categories_sub->getData())){ ?>
          <div class="page-products">
          <div class="products wrapper grid products-grid">
          <div class="col-xs-12 abt_head caps">
          <h2><?php echo $_category->getName(); ?></h2>
          <p><?php echo $_category->getDescription(); ?></p>
          <div class="current-image">
          <img src="<?php echo $_category->getImageUrl()?>" >
          </div>
          </div>
          <?php foreach($categories_sub as $category_sub): ?>
          <?php $category_sub->getParentId();
          $categoryobj =
          $objectManager->create('MagentoCatalogModelCategoryFactory');
          $category =
          $objectManager->get('MagentoFrameworkRegistry')->registry('current_category');//get
          current category
          $category_sub->getChildren();?>
          <div class='submenu'>
          <a href="<?php echo
          $categoryHelper_sub->getCategoryUrl($category_sub);?>">
          <div class='submenu-img'> <img src="<?php echo
          $category_sub->getImageUrl()?>" alt="<?php echo
          $category_sub->getName()?>"></img>
          </div>
          <?php //echo $category_sub->getName()?></a>
          </div>
          <?php endforeach; ?>
          </div>
          <?php } ?>





          share|improve this answer





























            1














            I have create small module for display sub category please check this link.



            https://github.com/pratikmage/magento2-subcategory






            share|improve this answer





















              Your Answer








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

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

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


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f141236%2fdisplaying-subcategories-instead-of-products-2-1%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              2














              You need to customized the category in the theme folder use the
              following file structure and update the following code



              /app/design/frontend/<vendor>/theme/Magento_Catalog/templates/category

              products.phtml
              <?php

              // call current category and sub category

              $categoryHelper_sub = $this->helper('MagentoCatalogHelperCategory');
              $_category = $block->getCurrentCategory();

              $objectManager_sub = $objectManager =
              MagentoFrameworkAppObjectManager::getInstance();
              $imagehelper = $objectManager->create('MagentoCatalogHelperImage');
              $category = $objectManager_sub->get('MagentoCatalogModelCategory');
              $categories_sub =
              $category->getCollection()->addAttributeToSelect('*')->addAttributeToFilter('is_active',
              1)->addIdFilter($_category->getChildren())->setOrder('position', 'ASC');?>

              <?php if(!empty($categories_sub->getData())){ ?>
              <div class="page-products">
              <div class="products wrapper grid products-grid">
              <div class="col-xs-12 abt_head caps">
              <h2><?php echo $_category->getName(); ?></h2>
              <p><?php echo $_category->getDescription(); ?></p>
              <div class="current-image">
              <img src="<?php echo $_category->getImageUrl()?>" >
              </div>
              </div>
              <?php foreach($categories_sub as $category_sub): ?>
              <?php $category_sub->getParentId();
              $categoryobj =
              $objectManager->create('MagentoCatalogModelCategoryFactory');
              $category =
              $objectManager->get('MagentoFrameworkRegistry')->registry('current_category');//get
              current category
              $category_sub->getChildren();?>
              <div class='submenu'>
              <a href="<?php echo
              $categoryHelper_sub->getCategoryUrl($category_sub);?>">
              <div class='submenu-img'> <img src="<?php echo
              $category_sub->getImageUrl()?>" alt="<?php echo
              $category_sub->getName()?>"></img>
              </div>
              <?php //echo $category_sub->getName()?></a>
              </div>
              <?php endforeach; ?>
              </div>
              <?php } ?>





              share|improve this answer


























                2














                You need to customized the category in the theme folder use the
                following file structure and update the following code



                /app/design/frontend/<vendor>/theme/Magento_Catalog/templates/category

                products.phtml
                <?php

                // call current category and sub category

                $categoryHelper_sub = $this->helper('MagentoCatalogHelperCategory');
                $_category = $block->getCurrentCategory();

                $objectManager_sub = $objectManager =
                MagentoFrameworkAppObjectManager::getInstance();
                $imagehelper = $objectManager->create('MagentoCatalogHelperImage');
                $category = $objectManager_sub->get('MagentoCatalogModelCategory');
                $categories_sub =
                $category->getCollection()->addAttributeToSelect('*')->addAttributeToFilter('is_active',
                1)->addIdFilter($_category->getChildren())->setOrder('position', 'ASC');?>

                <?php if(!empty($categories_sub->getData())){ ?>
                <div class="page-products">
                <div class="products wrapper grid products-grid">
                <div class="col-xs-12 abt_head caps">
                <h2><?php echo $_category->getName(); ?></h2>
                <p><?php echo $_category->getDescription(); ?></p>
                <div class="current-image">
                <img src="<?php echo $_category->getImageUrl()?>" >
                </div>
                </div>
                <?php foreach($categories_sub as $category_sub): ?>
                <?php $category_sub->getParentId();
                $categoryobj =
                $objectManager->create('MagentoCatalogModelCategoryFactory');
                $category =
                $objectManager->get('MagentoFrameworkRegistry')->registry('current_category');//get
                current category
                $category_sub->getChildren();?>
                <div class='submenu'>
                <a href="<?php echo
                $categoryHelper_sub->getCategoryUrl($category_sub);?>">
                <div class='submenu-img'> <img src="<?php echo
                $category_sub->getImageUrl()?>" alt="<?php echo
                $category_sub->getName()?>"></img>
                </div>
                <?php //echo $category_sub->getName()?></a>
                </div>
                <?php endforeach; ?>
                </div>
                <?php } ?>





                share|improve this answer
























                  2












                  2








                  2






                  You need to customized the category in the theme folder use the
                  following file structure and update the following code



                  /app/design/frontend/<vendor>/theme/Magento_Catalog/templates/category

                  products.phtml
                  <?php

                  // call current category and sub category

                  $categoryHelper_sub = $this->helper('MagentoCatalogHelperCategory');
                  $_category = $block->getCurrentCategory();

                  $objectManager_sub = $objectManager =
                  MagentoFrameworkAppObjectManager::getInstance();
                  $imagehelper = $objectManager->create('MagentoCatalogHelperImage');
                  $category = $objectManager_sub->get('MagentoCatalogModelCategory');
                  $categories_sub =
                  $category->getCollection()->addAttributeToSelect('*')->addAttributeToFilter('is_active',
                  1)->addIdFilter($_category->getChildren())->setOrder('position', 'ASC');?>

                  <?php if(!empty($categories_sub->getData())){ ?>
                  <div class="page-products">
                  <div class="products wrapper grid products-grid">
                  <div class="col-xs-12 abt_head caps">
                  <h2><?php echo $_category->getName(); ?></h2>
                  <p><?php echo $_category->getDescription(); ?></p>
                  <div class="current-image">
                  <img src="<?php echo $_category->getImageUrl()?>" >
                  </div>
                  </div>
                  <?php foreach($categories_sub as $category_sub): ?>
                  <?php $category_sub->getParentId();
                  $categoryobj =
                  $objectManager->create('MagentoCatalogModelCategoryFactory');
                  $category =
                  $objectManager->get('MagentoFrameworkRegistry')->registry('current_category');//get
                  current category
                  $category_sub->getChildren();?>
                  <div class='submenu'>
                  <a href="<?php echo
                  $categoryHelper_sub->getCategoryUrl($category_sub);?>">
                  <div class='submenu-img'> <img src="<?php echo
                  $category_sub->getImageUrl()?>" alt="<?php echo
                  $category_sub->getName()?>"></img>
                  </div>
                  <?php //echo $category_sub->getName()?></a>
                  </div>
                  <?php endforeach; ?>
                  </div>
                  <?php } ?>





                  share|improve this answer












                  You need to customized the category in the theme folder use the
                  following file structure and update the following code



                  /app/design/frontend/<vendor>/theme/Magento_Catalog/templates/category

                  products.phtml
                  <?php

                  // call current category and sub category

                  $categoryHelper_sub = $this->helper('MagentoCatalogHelperCategory');
                  $_category = $block->getCurrentCategory();

                  $objectManager_sub = $objectManager =
                  MagentoFrameworkAppObjectManager::getInstance();
                  $imagehelper = $objectManager->create('MagentoCatalogHelperImage');
                  $category = $objectManager_sub->get('MagentoCatalogModelCategory');
                  $categories_sub =
                  $category->getCollection()->addAttributeToSelect('*')->addAttributeToFilter('is_active',
                  1)->addIdFilter($_category->getChildren())->setOrder('position', 'ASC');?>

                  <?php if(!empty($categories_sub->getData())){ ?>
                  <div class="page-products">
                  <div class="products wrapper grid products-grid">
                  <div class="col-xs-12 abt_head caps">
                  <h2><?php echo $_category->getName(); ?></h2>
                  <p><?php echo $_category->getDescription(); ?></p>
                  <div class="current-image">
                  <img src="<?php echo $_category->getImageUrl()?>" >
                  </div>
                  </div>
                  <?php foreach($categories_sub as $category_sub): ?>
                  <?php $category_sub->getParentId();
                  $categoryobj =
                  $objectManager->create('MagentoCatalogModelCategoryFactory');
                  $category =
                  $objectManager->get('MagentoFrameworkRegistry')->registry('current_category');//get
                  current category
                  $category_sub->getChildren();?>
                  <div class='submenu'>
                  <a href="<?php echo
                  $categoryHelper_sub->getCategoryUrl($category_sub);?>">
                  <div class='submenu-img'> <img src="<?php echo
                  $category_sub->getImageUrl()?>" alt="<?php echo
                  $category_sub->getName()?>"></img>
                  </div>
                  <?php //echo $category_sub->getName()?></a>
                  </div>
                  <?php endforeach; ?>
                  </div>
                  <?php } ?>






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Oct 19 '16 at 6:03









                  Abhinav SinghAbhinav Singh

                  2,017610




                  2,017610

























                      1














                      I have create small module for display sub category please check this link.



                      https://github.com/pratikmage/magento2-subcategory






                      share|improve this answer


























                        1














                        I have create small module for display sub category please check this link.



                        https://github.com/pratikmage/magento2-subcategory






                        share|improve this answer
























                          1












                          1








                          1






                          I have create small module for display sub category please check this link.



                          https://github.com/pratikmage/magento2-subcategory






                          share|improve this answer












                          I have create small module for display sub category please check this link.



                          https://github.com/pratikmage/magento2-subcategory







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Jan 11 '17 at 11:26









                          PratikPratik

                          2,06851342




                          2,06851342






























                              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.





                              Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                              Please pay close attention to the following guidance:


                              • 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%2f141236%2fdisplaying-subcategories-instead-of-products-2-1%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

                              An IMO inspired problem

                              Management

                              Investment