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'); 
  • Did you inject Magento\Framework\Registry in block constructor? – Jjo Jun 28 '17 at 13:05
  • _coreRegistry by default its available – venkata prasad Jun 28 '17 at 13:11
  • use $count_products = count($this->_getProductCollection()->load()); – Jjo Jun 28 '17 at 13:14
  • $count_products = count($this->_getProductCollection()->load()); already used this . Not working. – venkata prasad Jun 28 '17 at 13:15
  • working or not? – Jjo Jun 28 '17 at 13:18
1
<?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, privacy policy and cookie policy

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