1

i want to get Total Count of all Product Review of Specific Category.

suppose if Apple category has 15 product and all products have like 15 reviews,

so i want that total 15 counts of only Apple category,

for that, i tried one code, but this code get only one product Review count. here is code...

Path : app/code/Extra/Information/Block/Products.php

<?php
namespace Extra\Information\Block;

class Products extends \Magento\Framework\View\Element\Template
{ 
    protected $_registry;
    protected $_productCollectionFactory;
    protected $categoryFactory;
    protected $_productVisibility;
  
    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context, 
        \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
        \Magento\Catalog\Model\CategoryFactory $categoryFactory,
        //\Magento\Catalog\Model\ResourceModel\CategoryFactory $categoryFactory,
        \Magento\Catalog\Model\Product\Visibility $productVisibility,
        array $data = []
    ) {
        $this->_registry = $registry;       
        $this->_productCollectionFactory = $productCollectionFactory; 
        $this->categoryFactory = $categoryFactory;
        $this->_productVisibility = $productVisibility; 
        parent::__construct($context, $data);
    }
    
    /*public function getCurrentCategory()
    {
        return $this->_registry->registry('current_category');
    }*/
    public function getCategory()
    {
        $category = $this->categoryFactory->create()->load()
                ->getProductCollection()->addAttributeToSelect('*')
                ->addAttributeToFilter('is_feature','1')->setPageSize(20)
                ->setOrder('position','asc');
        $category->setVisibility($this->catalogProductVisibility->getVisibleInCatalogIds());
        return $category;
    }
        
    /*public function getCategory($categoryId) 
    {
        $category = $this->_categoryFactory->create();
        $category->load($categoryId);
        return $category;
    }*/ 
    public function getProductCollection() {
        $collection = $this->_productCollectionFactory->create();
        $collection->addAttributeToSelect('*');
  
        // filter current website products
        $collection->addWebsiteFilter();
 
        // filter current store products
        $collection->addStoreFilter();
  
        // set visibility filter
        $collection->setVisibility($this->productVisibility->getVisibleInSiteIds());
  
        // fetching only 5 products
        $collection->setPageSize(5); 
        
        return $this->getCategory()->getProductCollection()->addAttributeToSelect('*');
        
        //return $collection;
        return $collection->getData();
    }
}

Path : app/code/Extra/Information/view/frontend/templates/myreview.phtml

<?php

    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $category = $objectManager->get('Magento\Framework\Registry')->registry('current_category');
    
    $productCollectionFactory = $objectManager->get('\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory');
    $collection = $productCollectionFactory->create();
    $collection->addAttributeToSelect('*');
    
    $categoryFactory = $objectManager->get('Magento\Catalog\Model\CategoryFactory');
    
    $categoryId = $category->getId();
    $category = $this->categoryFactory->create()->load($categoryId);
    $collection = $this->_productCollectionFactory->create();
    $collection->addAttributeToSelect('*');
    $collection->addCategoryFilter($category);
    $collection->addAttributeToFilter('visibility', \Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH);
    $collection->addAttributeToFilter('status',\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED);
    $ratingSummary = 0;
    $reviewCount = 0;
    $productCount = $collection->count();
    foreach($collection as $item){
       $ratingSummary = $ratingSummary + $item->getRatingSummary()->getRatingSummary();
       $reviewCount = $reviewCount + $item->getRatingSummary()->getReviewsCount();
    }

    $finalRatingSummary = $ratingSummary/$productCount;
    $finalReviewCount = $reviewCount/$productCount;

?>

Finally i call in Phtml file app/design/frontend/Codazon/fastest/bigmarket/Magento_Catalog/templates/category/products.phtml

<?php echo $this->getLayout()->createBlock("Magento\Framework\View\Element\Template")->setTemplate("Extra_Information::myreview.phtml")->toHtml() ?>

I get this kind of error 1 exception(s): Exception #0 (Exception): Notice: Undefined property: Magento\Framework\View\Element\Template::$categoryFactory in /chroot/home/a310743a/test.abc.com/html/vendor/magento/framework/View/TemplateEngine/Php.php on line 110

OutPut:

*****(2) REVIEWS

but i dont understood how to modify this code and get Total COunt of Reviews.

i want like below image shows the count,

enter image description here

3
  • this is because you are passing the product id in the above code. so it will return all the reviews that are present on that particular product
    – Pramod
    Commented Jul 9, 2020 at 9:41
  • yes you are right, but i dont understand that how to modify this code, so i get total number of counts review of specified category product? please help me on this
    – rocky9310
    Commented Jul 9, 2020 at 9:46
  • just try to load this collection with catg id
    – Pramod
    Commented Jul 9, 2020 at 9:47

1 Answer 1

1

I think you have to get the Current category and get the product collection for that category, after that you can cicle the products and get the value that your intrested with, placing them maybe in temporary variables and then get those values and divide them for the numer of product of the collection, in that code it's missing the category where you want to load the collection,

    $categoryId = 'yourcategoryid';
    $category = $this->_categoryFactory->create()->load($categoryId);
    $collection = $this->_productCollectionFactory->create();
    $collection->addAttributeToSelect('*');
    $collection->addCategoryFilter($category);
    $collection->addAttributeToFilter('visibility', \Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH);
    $collection->addAttributeToFilter('status',\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED);
    $ratingSummary = 0;
    $reviewCount = 0;
    $productCount = $collection->count();
    foreach($collection as $item){
       $ratingSummary = $ratingSummary + $item->getRatingSummary()->getRatingSummary();
       $reviewCount = $reviewCount + $item->getRatingSummary()->getReviewsCount();
    }

    $finalRatingSummary = $ratingSummary/$productCount;
    $finalReviewCount = $reviewCount/$productCount;

you can try something like this, starting from a category

7
  • i have tried your code i get this error..
    – rocky9310
    Commented Jul 10, 2020 at 12:11
  • 1 exception(s): Exception #0 (Exception): Notice: Undefined property: Magento\Catalog\Block\Product\ListProduct\Interceptor::$_categoryFactory in /chroot/home/a310743a/test.thejerusalemgiftshop.com/html/vendor/magento/framework/View/TemplateEngine/Php.php on line 110
    – rocky9310
    Commented Jul 10, 2020 at 12:11
  • Have you loaded the category Factory with object manager? It's a thing that i havent done in my code snippet Commented Jul 10, 2020 at 16:40
  • yes, i have load category factory in BLock folders php file, and phtm file also,
    – rocky9310
    Commented Jul 13, 2020 at 10:57
  • protected $_categoryFactory; \Magento\Catalog\Model\CategoryFactory $categoryFactory, $this->_categoryFactory = $categoryFactory; in php file and $_categoryFactory = $objectManager->get('Magento\Catalog\Model\CategoryFactory'); in phtml file
    – rocky9310
    Commented Jul 13, 2020 at 10:58

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.