Question

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'); 
Was it helpful?

Solution

<?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
                        }
                    }
                    ?>
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top