0

I need to display the products count at list page before category description.but its not working. Can you please anyone help me.

/vendor/magento/module-catalog/Block/Product/ListProduct.php

protected function _beforeToHtml()
    {
   $count_products = count($this->_productCollection); 
            $this->_coreRegistry->register('productsCount',$count_products);
}

/app/design/frontend/Dcw/wps/Magento_Catalog/templates/category

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $registryObj = $objectManager->get('Magento\Framework\Registry');
 echo $productCount = $registryObj->registry('productsCount'); 
7
  • Did you inject Magento\Framework\Registry in block constructor?
    – Jjo
    Commented Jun 28, 2017 at 13:05
  • _coreRegistry by default its available Commented Jun 28, 2017 at 13:11
  • use $count_products = count($this->_getProductCollection()->load());
    – Jjo
    Commented Jun 28, 2017 at 13:14
  • $count_products = count($this->_getProductCollection()->load()); already used this . Not working. Commented Jun 28, 2017 at 13:15
  • working or not?
    – Jjo
    Commented Jun 28, 2017 at 13:18

1 Answer 1

2
<?php  foreach ($subcats as $subcat) { 
                        if ($subcat->getIsActive()) {
                        $_category = $objectManager->create('Magento\Catalog\Model\Category')->load($subcat->getId());
                            $_imgUrl = $_category->getImageUrl(); 
                            $subcat_url = $subcat->getUrl();
                            $subcat_img = $store->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA) . 'catalog/category/' . $subcat->getImage(); 
                            if($_imgUrl ==''){
                                $_imgUrl = $store->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_MEDIA)."catalog/category/placeholder.png";
                            }
                            ?>
                            <div class="item">
                                <a href="<?php echo $subcat_url; ?>">
                                <span class="image_view_sec">
                                    <img class="img-responsive" src="<?php echo $_imgUrl; ?>"/>
                                </span>
                                    <p><?php echo $subcat->getName(); ?>
                                    <span class="pro_quantity"><?php echo '('.$subcat->getProductCollection()->count().')';?></span></p>
                                </a>
                            </div>

                            <?php
                        }
                    }
                    ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.