4

In Magento 2 how to get all the product reviews by product SKU.

Please find the screenshot below.

enter image description here please provide/suggest me how to get.

I have written some test code:

<?php
use Magento\Framework\App\Bootstrap;
require __DIR__ . '/app/bootstrap.php';
$bootstrap = Bootstrap::create(BP, $_SERVER);
$obj = $bootstrap->getObjectManager();
$state = $obj->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');

ini_set('display_errors', 1);
echo "All reviews of the product </br>";


$sku ='24-MB04';
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
/** @var \Magento\Catalog\Model\Product $product */
$productObject = $objectManager->get('Magento\Catalog\Model\Product');
$storeManager = $objectManager->get('Magento\Store\Model\StoreManagerInterface');
$currentStoreId = $storeManager->getStore()->getId();
$product = $productObject->loadByAttribute('sku', $sku);
4

This is example to get product review by product SKU or Product ID

$sku = '24-MB02';
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->create("Magento\Catalog\Model\Product")->loadByAttribute('sku', $sku); //use load($producID) if you have product id
$storeManager = $objectManager->get('Magento\Store\Model\StoreManagerInterface');
$currentStoreId = $storeManager->getStore()->getId();
$rating = $objectManager->get("Magento\Review\Model\ResourceModel\Review\CollectionFactory");

$collection = $rating->create()->addStoreFilter(
            $currentStoreId
        )->addStatusFilter(
            \Magento\Review\Model\Review::STATUS_APPROVED
        )->addEntityFilter(
            'product',
            $product->getId()
        )->setDateOrder();

print_r($collection->getData()); //Get all review data of product
| improve this answer | |
  • Let me know if you have any issue. – Prince Patel Jul 31 '17 at 6:46
  • thanks for your reply @prince i am getting an array prntscr.com/g2ec3y but how to get these details prntscr.com/g2ebw8 please suggest me if i have used getData() method getting fatal error – Nagaraju K Jul 31 '17 at 7:02
  • @NAGARAJUKASA plz check my updated answer you can get all review infor like title, description from $collection – Prince Patel Aug 1 '17 at 9:35
  • thanks prince i have small doubt how can get rating percentage by review id? here – Nagaraju K Aug 1 '17 at 10:40
  • @PrincePatel it is giving 0 review count and 0 review summary – Hitesh Balpande Dec 26 '18 at 7:07
0

Load RatingSummary by $product , Try below code :

    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $reviewFactory = $objectManager->create('Magento\Review\Model\Review');

    $storeId = $this->_storeManager->getStore()->getId();
    $reviewFactory->getEntitySummary($product, $storeId);

    $ratingSummary = $product->getRatingSummary()->getRatingSummary();
| improve this answer | |
  • thanks @manthan dave for your reply but how to get remaining all the details as shown in the screenshot – Nagaraju K Jul 31 '17 at 6:02
  • @NAGARAJUKASA check out $reviewFactory ->getData(); you will find all the details then get the details which you want by calling methods for the same – Manthan Dave Jul 31 '17 at 6:05
  • okay thanks let me check and get back to u dave.. – Nagaraju K Jul 31 '17 at 6:15
  • @Manthan it is giving 0 review count and 0 review summary – Hitesh Balpande Dec 26 '18 at 7:06
0

ini_set('display_errors', 1);

error_reporting(E_ALL);

ini_set('max_execution_time', 0);

ini_set('memory_limit', '-1');

set_time_limit(0);

use Magento\Framework\App\Bootstrap;

include("app/bootstrap.php");

$bootstrap = Bootstrap::create(BP, $_SERVER);

$objectManager = $bootstrap->getObjectManager();

$state = $objectManager->get('Magento\Framework\App\State');

$state->setAreaCode('frontend');

$_categoryFactory = $objectManager->get('Magento\Catalog\Model\CategoryFactory');

$storeManager = $objectManager->get('\Magento\Store\Model\StoreManagerInterface');

$scopeConfig = $objectManager->get('\Magento\Framework\App\Config\ScopeConfigInterface');

$sku = 'WT09'; // YOU PRODUCT SKU

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

$product = $objectManager->create("Magento\Catalog\Model\Product")->loadByAttribute('sku', $sku);

$rating = $objectManager->get("Magento\Review\Model\ResourceModel\Review\CollectionFactory");
$collection = $rating->create()
        ->addStatusFilter(
            \Magento\Review\Model\Review::STATUS_APPROVED
        )->addEntityFilter(
            'product',
            $product->getId()
        )->setDateOrder();
echo "<pre>";
print_r($collection->getData());
echo "<pre>";
| improve this answer | |

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.