How to get Review for products collection magento 2?
-
07-02-2021 - |
Domanda
Actually i want to get Review Rating and summary count.Please check below code
<?php
error_reporting(0);
use \Magento\Framework\App\Bootstrap;
include('../app/bootstrap.php');
$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
$listBlock = $objectManager->get('\Magento\Catalog\Block\Product\ListProduct');
$addToCartUrl = $listBlock->getAddToCartUrl($product);
$reviewFactory = $objectManager->create('Magento\Review\Model\Review');
$storeManager = $objectManager->create('\Magento\Store\Model\StoreManagerInterface');
$storeId = $storeManager->getStore()->getStoreId();
$reviewFactory->getEntitySummary($product, $storeId);
$ratingSummary = $product->getRatingSummary()->getRatingSummary();
$reviewCount = $product->getRatingSummary()->getReviewsCount();
?>
Soluzione
You can try the code given below.
Please change the $sku for the product for which you want to get the review and reting summery.
<?php
ini_set('display_startup_errors', 1);
ini_set('display_errors', 1);
error_reporting(-1);
use Magento\Framework\App\Bootstrap;
require __DIR__ . '/app/bootstrap.php';
$params = $_SERVER;
$bootstrap = Bootstrap::create(BP, $params);
$objectManager = $bootstrap->getObjectManager();
$state = $objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');
$listBlock = $objectManager->get('\Magento\Catalog\Block\Product\ListProduct');
$sku = '24-MB04';
$product = $objectManager->create("Magento\Catalog\Model\Product")->loadByAttribute('sku', $sku); //you can also use load($id) if you know product id
$addToCartUrl = $listBlock->getAddToCartUrl($product);
$reviewFactory = $objectManager->create('Magento\Review\Model\Review');
$storeManager = $objectManager->create('\Magento\Store\Model\StoreManagerInterface');
$storeId = $storeManager->getStore()->getStoreId();
$reviewFactory->getEntitySummary($product, $storeId);
$ratingSummary = $product->getRatingSummary()->getRatingSummary();
$reviewCount = $product->getRatingSummary()->getReviewsCount();
echo "<br />Rating Summary=". $ratingSummary;
echo "<br />Review Count=".$reviewCount;
?>
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a magento.stackexchange