I am tired to get rating by product id in magento2 ? here is my code for get reviews rating .

 protected $reviewModel;  
public function __construct(

\Magento\Review\Model\Review $reviewModel,

    array $data = []

    $this->reviewModel = $reviewModel;


  public function getCategoryProducts($categoryId) 
    $products = $this->getCategory($categoryId)->getProductCollection();
    return $products;

3 Answers 3


Use the following code to avail product rating through product ID in Magento 2

    $_productid = 12;
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $RatingOb = $objectManager->create('Magento\Review\Model\Rating')->getEntitySummary($_productid);   
    $ratings = $RatingOb->getSum()/$RatingOb->getCount(); 

<div class="product-reviews-summary">
    <div class="rating-summary">
        <span class="label"><span>Rating:</span></span>
        <div class="rating-result">
            <span style="width:<?php echo $ratings; ?>%"><span><span itemprop="ratingValue"><?php echo $ratings; ?></span>% of <span itemprop="bestRating">100</span></span></span>
        <?php echo '('.$RatingOb->getCount().')'; ?>

You have to manage using css as per display rating requirement.

  • Do you have any idea how to retrieve review form for a product on custom phtml? Commented Dec 29, 2016 at 10:49

How we get product rating via product id in magento2?

public function __construct(
    \Magento\Review\Model\ReviewFactory $reviewFactory,
    \Magento\Store\Model\StoreManagerInterface $storeManager,
) {
    $this->_reviewFactory = $reviewFactory;
    $this->_storeManager = $storeManager;

public function getRatingSummary()
    $this->_reviewFactory->create()->getEntitySummary($product, $this->_storeManager->getStore()->getId());
    $ratingSummary = $product->getRatingSummary()->getRatingSummary();

    return $ratingSummary;

my reputation is less than 50 so I couldn't reply to your comment. below is the code to get the review from the product

$_product = $this->getProduct(); //Fetch product from model or from same file
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$storeManager = $objectManager->get('Magento\Store\Model\StoreManagerInterface');
$currentStoreId = $storeManager->getStore()->getId();
$rating = $objectManager->get("Magento\Review\Model\ResourceModel\Review\CollectionFactory");

$collection = $rating->create()->addStoreFilter(

$reviewDetails = $collection->getData();
foreach ($reviewDetails as $key => $review) {
                        <div class="review-block">
                            <dt><span>By  <?php echo $review['nickname']; ?></span></dt>
                                <p>Rating<?php echo $review['review_id']; ?></p>
                                <p><?php echo $review['detail']; ?></p>
                                <small class="date">(Posted on <?php echo $review['created_at'] ?>)</small>

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.