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,