Counting amount of products in category not workingWrong number of products in categoryMagento Category...

Current across a wire with zero potential difference

How do you get out of your own psychology to write characters?

Why do neural networks need so many training examples to perform?

Plausible reason for gold-digging ant

Cat is tipping over bed-side lamps during the night

Separate environment for personal and development use under macOS

Existence of Riemann surface, holomorphic maps

What is a good reason for every spaceship to carry a weapon on board?

Nuance between philia and mania?

How do you funnel food off a cutting board?

In Linux what happens if 1000 files in a directory are moved to another location while another 300 files were added to the source directory?

Prioritising polygons in QGIS

Why does magnet wire need to be insulated?

Coworker asking me to not bring cakes due to self control issue. What should I do?

How can 若い時 and ずっと work in this sentence?

Hosting images in SFMC - is it a good idea?

Subsurf on a crown. How can I smooth some edges and keep others sharp?

What is the industry term for house wiring diagrams?

Does a paladin have to announce that they're using Divine Smite before attacking?

What is the wife of a henpecked husband called?

What is a DAG (Graph Theory)?

Square Root Distance from Integers

What language shall they sing in?

Microtypography protrusion with Polish quotation marks



Counting amount of products in category not working


Wrong number of products in categoryMagento Category Products reindex error - Integrity constraint violationnew magento install - how to get any product or category page to show up - anything other than the home pageBackend Category count is right but displaying productsLoading Products Filtered by CategoryMagento 1.9.2.4 sub-category listing not workingProducts not getting displayed on category page in magento 2Dynamically display the products which price is greater than some amount in specific categoryCategory product count not updating after deleting all productsMagento 2: Product not showing in category page













0















Currently I am making a custom RWD theme, with the Madison Island sample data. The category 'Women' contains 51 products in total. These products are divided in several different subcategories, for example 'Tops & Blouses'.



I looked around the internet for a piece of code that will help me display the amount of product within a category, and all answers came down to this code:



$categoryId = '4';
$products_count = Mage::getModel('catalog/category')->load($categoryId)->getProductCount();
echo $products_count;


Where 4 is the category ID of the category 'Women'. The problem is, is that this snippet echoes '3'. But I don't have 3 product, I have 51. Why is this?










share|improve this question



























    0















    Currently I am making a custom RWD theme, with the Madison Island sample data. The category 'Women' contains 51 products in total. These products are divided in several different subcategories, for example 'Tops & Blouses'.



    I looked around the internet for a piece of code that will help me display the amount of product within a category, and all answers came down to this code:



    $categoryId = '4';
    $products_count = Mage::getModel('catalog/category')->load($categoryId)->getProductCount();
    echo $products_count;


    Where 4 is the category ID of the category 'Women'. The problem is, is that this snippet echoes '3'. But I don't have 3 product, I have 51. Why is this?










    share|improve this question

























      0












      0








      0








      Currently I am making a custom RWD theme, with the Madison Island sample data. The category 'Women' contains 51 products in total. These products are divided in several different subcategories, for example 'Tops & Blouses'.



      I looked around the internet for a piece of code that will help me display the amount of product within a category, and all answers came down to this code:



      $categoryId = '4';
      $products_count = Mage::getModel('catalog/category')->load($categoryId)->getProductCount();
      echo $products_count;


      Where 4 is the category ID of the category 'Women'. The problem is, is that this snippet echoes '3'. But I don't have 3 product, I have 51. Why is this?










      share|improve this question














      Currently I am making a custom RWD theme, with the Madison Island sample data. The category 'Women' contains 51 products in total. These products are divided in several different subcategories, for example 'Tops & Blouses'.



      I looked around the internet for a piece of code that will help me display the amount of product within a category, and all answers came down to this code:



      $categoryId = '4';
      $products_count = Mage::getModel('catalog/category')->load($categoryId)->getProductCount();
      echo $products_count;


      Where 4 is the category ID of the category 'Women'. The problem is, is that this snippet echoes '3'. But I don't have 3 product, I have 51. Why is this?







      magento-1.9 product category category-products






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Sep 2 '16 at 8:14









      Maarten WolfsenMaarten Wolfsen

      619730




      619730






















          5 Answers
          5






          active

          oldest

          votes


















          1














          Use below code



          /** 
          * If you are in any category page then load its parent category
          * Else load root category; e.g root category has ID = 3
          *
          **/
          if (is_object(Mage::registry('current_category'))) {
          $parentCategory = Mage::getModel('catalog/category')->load(Mage::registry('current_category')->getId())->getParentCategory();
          } else {
          $rootCategoryId = 3;
          $parentCategory = Mage::getModel('catalog/category')->load($rootCategoryId);
          }

          <!--more-->

          //$_categories = $parentCategory->getChildren();

          $_categories = $parentCategory->getChildrenCategories();
          $productCollection = Mage::getResourceModel('catalog/product_collection');

          //$layer = Mage::getSingleton('catalog/layer');
          //$layer->prepareProductCollection($productCollection);

          $productCollection->addCountToCategories($_categories);

          // Displaying active categories with their product count
          foreach ($_categories as $_category):
          if($_category->getIsActive()):
          ?>
          <a href="<?php echo $_category->getUrl() ?>">
          <?php echo Mage::helper('core')->htmlEscape($_category->getName()) ?>
          </a>
          <?php echo '('.$_category->getProductCount().')'; ?>
          <br />
          <?php
          endif;
          endforeach;
          ?>


          Hope it helps. Thanks.






          share|improve this answer
























          • worked like a charm! Only thing is, that I have made a little function of it. I will also put that in as an answer, but without this I could not have done it!

            – Maarten Wolfsen
            Sep 2 '16 at 9:12



















          2














          Go to admin and edit this category (id=4). Now go to Category Products tab and check how many products are there. I think there will be 3 products. The total you are looking for is the sum of products of all it's sub categories.



          Use this code instead:



          $categoryId = '4';
          echo $products_count = Mage::getModel('catalog/category')->load($categoryId)->getProductCollection()->getSize();

          //Using count(). But getSize() is much faster
          //echo $products_count = Mage::getModel('catalog/category')->load($categoryId)->getProductCollection()->count();





          share|improve this answer


























          • This category itself contains 3 products indeed, and 4 sub categories. These subcategories do contain products. I need the sum of all the products that are in the subcategories of the category 'women'.

            – Maarten Wolfsen
            Sep 2 '16 at 8:38













          • check updated answer

            – Anil Suthar
            Sep 2 '16 at 8:39





















          0














          Use Below code for countiong prouduct



          $categoryId = '4';
          $collection = Mage::getModel('catalog/category')->load($categoryId)->getProductCollection();
          echo $colletion->getSize();


          getSize() gives you actually size of collection.






          share|improve this answer
























          • did not work for some reason

            – Maarten Wolfsen
            Sep 2 '16 at 9:11











          • same problem ? have you got 3 instead of 51?

            – Murtuza Zabuawala
            Sep 2 '16 at 9:17











          • No, it resulted in probably an error. The rest of the page wouldn't load

            – Maarten Wolfsen
            Sep 2 '16 at 9:19



















          0














          From the previous accepted answer, I have created this function, which will return the amount of products in the category containing subcategories:



          function get_cat_count($int){
          $rootCategoryId = $int;
          $parentCategory = Mage::getModel('catalog/category')->load($rootCategoryId);
          $_categories = $parentCategory->getChildrenCategories();
          $productCollection = Mage::getResourceModel('catalog/product_collection');
          $productCollection->addCountToCategories($_categories);

          // counting amount of products;
          $cat_count = 0;
          foreach ($_categories as $_category):
          if($_category->getIsActive()):
          $cat_count += $_category->getProductCount();
          endif;
          endforeach;

          echo $cat_count;
          }





          share|improve this answer































            0














            It is very simple and it worked well for me
            its just one line of code



            <?php echo Mage::registry('current_category')->getProductCount();?>


            It will display the count of the products of current category






            share|improve this answer








            New contributor




            Mohammed Muzammil is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.




















              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%2f134413%2fcounting-amount-of-products-in-category-not-working%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









              1














              Use below code



              /** 
              * If you are in any category page then load its parent category
              * Else load root category; e.g root category has ID = 3
              *
              **/
              if (is_object(Mage::registry('current_category'))) {
              $parentCategory = Mage::getModel('catalog/category')->load(Mage::registry('current_category')->getId())->getParentCategory();
              } else {
              $rootCategoryId = 3;
              $parentCategory = Mage::getModel('catalog/category')->load($rootCategoryId);
              }

              <!--more-->

              //$_categories = $parentCategory->getChildren();

              $_categories = $parentCategory->getChildrenCategories();
              $productCollection = Mage::getResourceModel('catalog/product_collection');

              //$layer = Mage::getSingleton('catalog/layer');
              //$layer->prepareProductCollection($productCollection);

              $productCollection->addCountToCategories($_categories);

              // Displaying active categories with their product count
              foreach ($_categories as $_category):
              if($_category->getIsActive()):
              ?>
              <a href="<?php echo $_category->getUrl() ?>">
              <?php echo Mage::helper('core')->htmlEscape($_category->getName()) ?>
              </a>
              <?php echo '('.$_category->getProductCount().')'; ?>
              <br />
              <?php
              endif;
              endforeach;
              ?>


              Hope it helps. Thanks.






              share|improve this answer
























              • worked like a charm! Only thing is, that I have made a little function of it. I will also put that in as an answer, but without this I could not have done it!

                – Maarten Wolfsen
                Sep 2 '16 at 9:12
















              1














              Use below code



              /** 
              * If you are in any category page then load its parent category
              * Else load root category; e.g root category has ID = 3
              *
              **/
              if (is_object(Mage::registry('current_category'))) {
              $parentCategory = Mage::getModel('catalog/category')->load(Mage::registry('current_category')->getId())->getParentCategory();
              } else {
              $rootCategoryId = 3;
              $parentCategory = Mage::getModel('catalog/category')->load($rootCategoryId);
              }

              <!--more-->

              //$_categories = $parentCategory->getChildren();

              $_categories = $parentCategory->getChildrenCategories();
              $productCollection = Mage::getResourceModel('catalog/product_collection');

              //$layer = Mage::getSingleton('catalog/layer');
              //$layer->prepareProductCollection($productCollection);

              $productCollection->addCountToCategories($_categories);

              // Displaying active categories with their product count
              foreach ($_categories as $_category):
              if($_category->getIsActive()):
              ?>
              <a href="<?php echo $_category->getUrl() ?>">
              <?php echo Mage::helper('core')->htmlEscape($_category->getName()) ?>
              </a>
              <?php echo '('.$_category->getProductCount().')'; ?>
              <br />
              <?php
              endif;
              endforeach;
              ?>


              Hope it helps. Thanks.






              share|improve this answer
























              • worked like a charm! Only thing is, that I have made a little function of it. I will also put that in as an answer, but without this I could not have done it!

                – Maarten Wolfsen
                Sep 2 '16 at 9:12














              1












              1








              1







              Use below code



              /** 
              * If you are in any category page then load its parent category
              * Else load root category; e.g root category has ID = 3
              *
              **/
              if (is_object(Mage::registry('current_category'))) {
              $parentCategory = Mage::getModel('catalog/category')->load(Mage::registry('current_category')->getId())->getParentCategory();
              } else {
              $rootCategoryId = 3;
              $parentCategory = Mage::getModel('catalog/category')->load($rootCategoryId);
              }

              <!--more-->

              //$_categories = $parentCategory->getChildren();

              $_categories = $parentCategory->getChildrenCategories();
              $productCollection = Mage::getResourceModel('catalog/product_collection');

              //$layer = Mage::getSingleton('catalog/layer');
              //$layer->prepareProductCollection($productCollection);

              $productCollection->addCountToCategories($_categories);

              // Displaying active categories with their product count
              foreach ($_categories as $_category):
              if($_category->getIsActive()):
              ?>
              <a href="<?php echo $_category->getUrl() ?>">
              <?php echo Mage::helper('core')->htmlEscape($_category->getName()) ?>
              </a>
              <?php echo '('.$_category->getProductCount().')'; ?>
              <br />
              <?php
              endif;
              endforeach;
              ?>


              Hope it helps. Thanks.






              share|improve this answer













              Use below code



              /** 
              * If you are in any category page then load its parent category
              * Else load root category; e.g root category has ID = 3
              *
              **/
              if (is_object(Mage::registry('current_category'))) {
              $parentCategory = Mage::getModel('catalog/category')->load(Mage::registry('current_category')->getId())->getParentCategory();
              } else {
              $rootCategoryId = 3;
              $parentCategory = Mage::getModel('catalog/category')->load($rootCategoryId);
              }

              <!--more-->

              //$_categories = $parentCategory->getChildren();

              $_categories = $parentCategory->getChildrenCategories();
              $productCollection = Mage::getResourceModel('catalog/product_collection');

              //$layer = Mage::getSingleton('catalog/layer');
              //$layer->prepareProductCollection($productCollection);

              $productCollection->addCountToCategories($_categories);

              // Displaying active categories with their product count
              foreach ($_categories as $_category):
              if($_category->getIsActive()):
              ?>
              <a href="<?php echo $_category->getUrl() ?>">
              <?php echo Mage::helper('core')->htmlEscape($_category->getName()) ?>
              </a>
              <?php echo '('.$_category->getProductCount().')'; ?>
              <br />
              <?php
              endif;
              endforeach;
              ?>


              Hope it helps. Thanks.







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Sep 2 '16 at 8:50









              Janak BhimaniJanak Bhimani

              39029




              39029













              • worked like a charm! Only thing is, that I have made a little function of it. I will also put that in as an answer, but without this I could not have done it!

                – Maarten Wolfsen
                Sep 2 '16 at 9:12



















              • worked like a charm! Only thing is, that I have made a little function of it. I will also put that in as an answer, but without this I could not have done it!

                – Maarten Wolfsen
                Sep 2 '16 at 9:12

















              worked like a charm! Only thing is, that I have made a little function of it. I will also put that in as an answer, but without this I could not have done it!

              – Maarten Wolfsen
              Sep 2 '16 at 9:12





              worked like a charm! Only thing is, that I have made a little function of it. I will also put that in as an answer, but without this I could not have done it!

              – Maarten Wolfsen
              Sep 2 '16 at 9:12













              2














              Go to admin and edit this category (id=4). Now go to Category Products tab and check how many products are there. I think there will be 3 products. The total you are looking for is the sum of products of all it's sub categories.



              Use this code instead:



              $categoryId = '4';
              echo $products_count = Mage::getModel('catalog/category')->load($categoryId)->getProductCollection()->getSize();

              //Using count(). But getSize() is much faster
              //echo $products_count = Mage::getModel('catalog/category')->load($categoryId)->getProductCollection()->count();





              share|improve this answer


























              • This category itself contains 3 products indeed, and 4 sub categories. These subcategories do contain products. I need the sum of all the products that are in the subcategories of the category 'women'.

                – Maarten Wolfsen
                Sep 2 '16 at 8:38













              • check updated answer

                – Anil Suthar
                Sep 2 '16 at 8:39


















              2














              Go to admin and edit this category (id=4). Now go to Category Products tab and check how many products are there. I think there will be 3 products. The total you are looking for is the sum of products of all it's sub categories.



              Use this code instead:



              $categoryId = '4';
              echo $products_count = Mage::getModel('catalog/category')->load($categoryId)->getProductCollection()->getSize();

              //Using count(). But getSize() is much faster
              //echo $products_count = Mage::getModel('catalog/category')->load($categoryId)->getProductCollection()->count();





              share|improve this answer


























              • This category itself contains 3 products indeed, and 4 sub categories. These subcategories do contain products. I need the sum of all the products that are in the subcategories of the category 'women'.

                – Maarten Wolfsen
                Sep 2 '16 at 8:38













              • check updated answer

                – Anil Suthar
                Sep 2 '16 at 8:39
















              2












              2








              2







              Go to admin and edit this category (id=4). Now go to Category Products tab and check how many products are there. I think there will be 3 products. The total you are looking for is the sum of products of all it's sub categories.



              Use this code instead:



              $categoryId = '4';
              echo $products_count = Mage::getModel('catalog/category')->load($categoryId)->getProductCollection()->getSize();

              //Using count(). But getSize() is much faster
              //echo $products_count = Mage::getModel('catalog/category')->load($categoryId)->getProductCollection()->count();





              share|improve this answer















              Go to admin and edit this category (id=4). Now go to Category Products tab and check how many products are there. I think there will be 3 products. The total you are looking for is the sum of products of all it's sub categories.



              Use this code instead:



              $categoryId = '4';
              echo $products_count = Mage::getModel('catalog/category')->load($categoryId)->getProductCollection()->getSize();

              //Using count(). But getSize() is much faster
              //echo $products_count = Mage::getModel('catalog/category')->load($categoryId)->getProductCollection()->count();






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Sep 2 '16 at 10:10

























              answered Sep 2 '16 at 8:34









              Anil SutharAnil Suthar

              3,9781617




              3,9781617













              • This category itself contains 3 products indeed, and 4 sub categories. These subcategories do contain products. I need the sum of all the products that are in the subcategories of the category 'women'.

                – Maarten Wolfsen
                Sep 2 '16 at 8:38













              • check updated answer

                – Anil Suthar
                Sep 2 '16 at 8:39





















              • This category itself contains 3 products indeed, and 4 sub categories. These subcategories do contain products. I need the sum of all the products that are in the subcategories of the category 'women'.

                – Maarten Wolfsen
                Sep 2 '16 at 8:38













              • check updated answer

                – Anil Suthar
                Sep 2 '16 at 8:39



















              This category itself contains 3 products indeed, and 4 sub categories. These subcategories do contain products. I need the sum of all the products that are in the subcategories of the category 'women'.

              – Maarten Wolfsen
              Sep 2 '16 at 8:38







              This category itself contains 3 products indeed, and 4 sub categories. These subcategories do contain products. I need the sum of all the products that are in the subcategories of the category 'women'.

              – Maarten Wolfsen
              Sep 2 '16 at 8:38















              check updated answer

              – Anil Suthar
              Sep 2 '16 at 8:39







              check updated answer

              – Anil Suthar
              Sep 2 '16 at 8:39













              0














              Use Below code for countiong prouduct



              $categoryId = '4';
              $collection = Mage::getModel('catalog/category')->load($categoryId)->getProductCollection();
              echo $colletion->getSize();


              getSize() gives you actually size of collection.






              share|improve this answer
























              • did not work for some reason

                – Maarten Wolfsen
                Sep 2 '16 at 9:11











              • same problem ? have you got 3 instead of 51?

                – Murtuza Zabuawala
                Sep 2 '16 at 9:17











              • No, it resulted in probably an error. The rest of the page wouldn't load

                – Maarten Wolfsen
                Sep 2 '16 at 9:19
















              0














              Use Below code for countiong prouduct



              $categoryId = '4';
              $collection = Mage::getModel('catalog/category')->load($categoryId)->getProductCollection();
              echo $colletion->getSize();


              getSize() gives you actually size of collection.






              share|improve this answer
























              • did not work for some reason

                – Maarten Wolfsen
                Sep 2 '16 at 9:11











              • same problem ? have you got 3 instead of 51?

                – Murtuza Zabuawala
                Sep 2 '16 at 9:17











              • No, it resulted in probably an error. The rest of the page wouldn't load

                – Maarten Wolfsen
                Sep 2 '16 at 9:19














              0












              0








              0







              Use Below code for countiong prouduct



              $categoryId = '4';
              $collection = Mage::getModel('catalog/category')->load($categoryId)->getProductCollection();
              echo $colletion->getSize();


              getSize() gives you actually size of collection.






              share|improve this answer













              Use Below code for countiong prouduct



              $categoryId = '4';
              $collection = Mage::getModel('catalog/category')->load($categoryId)->getProductCollection();
              echo $colletion->getSize();


              getSize() gives you actually size of collection.







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Sep 2 '16 at 8:45









              Murtuza ZabuawalaMurtuza Zabuawala

              12.5k73362




              12.5k73362













              • did not work for some reason

                – Maarten Wolfsen
                Sep 2 '16 at 9:11











              • same problem ? have you got 3 instead of 51?

                – Murtuza Zabuawala
                Sep 2 '16 at 9:17











              • No, it resulted in probably an error. The rest of the page wouldn't load

                – Maarten Wolfsen
                Sep 2 '16 at 9:19



















              • did not work for some reason

                – Maarten Wolfsen
                Sep 2 '16 at 9:11











              • same problem ? have you got 3 instead of 51?

                – Murtuza Zabuawala
                Sep 2 '16 at 9:17











              • No, it resulted in probably an error. The rest of the page wouldn't load

                – Maarten Wolfsen
                Sep 2 '16 at 9:19

















              did not work for some reason

              – Maarten Wolfsen
              Sep 2 '16 at 9:11





              did not work for some reason

              – Maarten Wolfsen
              Sep 2 '16 at 9:11













              same problem ? have you got 3 instead of 51?

              – Murtuza Zabuawala
              Sep 2 '16 at 9:17





              same problem ? have you got 3 instead of 51?

              – Murtuza Zabuawala
              Sep 2 '16 at 9:17













              No, it resulted in probably an error. The rest of the page wouldn't load

              – Maarten Wolfsen
              Sep 2 '16 at 9:19





              No, it resulted in probably an error. The rest of the page wouldn't load

              – Maarten Wolfsen
              Sep 2 '16 at 9:19











              0














              From the previous accepted answer, I have created this function, which will return the amount of products in the category containing subcategories:



              function get_cat_count($int){
              $rootCategoryId = $int;
              $parentCategory = Mage::getModel('catalog/category')->load($rootCategoryId);
              $_categories = $parentCategory->getChildrenCategories();
              $productCollection = Mage::getResourceModel('catalog/product_collection');
              $productCollection->addCountToCategories($_categories);

              // counting amount of products;
              $cat_count = 0;
              foreach ($_categories as $_category):
              if($_category->getIsActive()):
              $cat_count += $_category->getProductCount();
              endif;
              endforeach;

              echo $cat_count;
              }





              share|improve this answer




























                0














                From the previous accepted answer, I have created this function, which will return the amount of products in the category containing subcategories:



                function get_cat_count($int){
                $rootCategoryId = $int;
                $parentCategory = Mage::getModel('catalog/category')->load($rootCategoryId);
                $_categories = $parentCategory->getChildrenCategories();
                $productCollection = Mage::getResourceModel('catalog/product_collection');
                $productCollection->addCountToCategories($_categories);

                // counting amount of products;
                $cat_count = 0;
                foreach ($_categories as $_category):
                if($_category->getIsActive()):
                $cat_count += $_category->getProductCount();
                endif;
                endforeach;

                echo $cat_count;
                }





                share|improve this answer


























                  0












                  0








                  0







                  From the previous accepted answer, I have created this function, which will return the amount of products in the category containing subcategories:



                  function get_cat_count($int){
                  $rootCategoryId = $int;
                  $parentCategory = Mage::getModel('catalog/category')->load($rootCategoryId);
                  $_categories = $parentCategory->getChildrenCategories();
                  $productCollection = Mage::getResourceModel('catalog/product_collection');
                  $productCollection->addCountToCategories($_categories);

                  // counting amount of products;
                  $cat_count = 0;
                  foreach ($_categories as $_category):
                  if($_category->getIsActive()):
                  $cat_count += $_category->getProductCount();
                  endif;
                  endforeach;

                  echo $cat_count;
                  }





                  share|improve this answer













                  From the previous accepted answer, I have created this function, which will return the amount of products in the category containing subcategories:



                  function get_cat_count($int){
                  $rootCategoryId = $int;
                  $parentCategory = Mage::getModel('catalog/category')->load($rootCategoryId);
                  $_categories = $parentCategory->getChildrenCategories();
                  $productCollection = Mage::getResourceModel('catalog/product_collection');
                  $productCollection->addCountToCategories($_categories);

                  // counting amount of products;
                  $cat_count = 0;
                  foreach ($_categories as $_category):
                  if($_category->getIsActive()):
                  $cat_count += $_category->getProductCount();
                  endif;
                  endforeach;

                  echo $cat_count;
                  }






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Sep 2 '16 at 9:13









                  Maarten WolfsenMaarten Wolfsen

                  619730




                  619730























                      0














                      It is very simple and it worked well for me
                      its just one line of code



                      <?php echo Mage::registry('current_category')->getProductCount();?>


                      It will display the count of the products of current category






                      share|improve this answer








                      New contributor




                      Mohammed Muzammil is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                      Check out our Code of Conduct.

























                        0














                        It is very simple and it worked well for me
                        its just one line of code



                        <?php echo Mage::registry('current_category')->getProductCount();?>


                        It will display the count of the products of current category






                        share|improve this answer








                        New contributor




                        Mohammed Muzammil is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                        Check out our Code of Conduct.























                          0












                          0








                          0







                          It is very simple and it worked well for me
                          its just one line of code



                          <?php echo Mage::registry('current_category')->getProductCount();?>


                          It will display the count of the products of current category






                          share|improve this answer








                          New contributor




                          Mohammed Muzammil is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                          Check out our Code of Conduct.










                          It is very simple and it worked well for me
                          its just one line of code



                          <?php echo Mage::registry('current_category')->getProductCount();?>


                          It will display the count of the products of current category







                          share|improve this answer








                          New contributor




                          Mohammed Muzammil is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                          Check out our Code of Conduct.









                          share|improve this answer



                          share|improve this answer






                          New contributor




                          Mohammed Muzammil is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                          Check out our Code of Conduct.









                          answered 3 hours ago









                          Mohammed MuzammilMohammed Muzammil

                          16




                          16




                          New contributor




                          Mohammed Muzammil is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                          Check out our Code of Conduct.





                          New contributor





                          Mohammed Muzammil is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                          Check out our Code of Conduct.






                          Mohammed Muzammil is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                          Check out our Code of Conduct.






























                              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%2f134413%2fcounting-amount-of-products-in-category-not-working%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

                              迭戈·戈丁...

                              A phrase ”follow into" in a context The 2019 Stack Overflow Developer Survey Results Are...

                              1960s short story making fun of James Bond-style spy fiction The 2019 Stack Overflow Developer...