Magento – Programatically get product rating and review in magento 2 product collection

magento2product-collectionproduct-reviewratingsreview

How can programatically get product rating and review in product collection in Magento 2.

Best Answer

You can just pass the product object and get the rating summary for specific product.

protected $_reviewFactory;

public function __construct(
    ...
    \Magento\Review\Model\ReviewFactory $reviewFactory,
    ...
) {
    ...
    $this->_reviewFactory = $reviewFactory;
    ...
}

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

    return $product->getRatingSummary()->getRatingSummary();
}
Related Topic