1

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();
        $products->setVisibility($this->_productVisibility->getVisibleInSiteIds());
    $this->reviewModel->appendSummary($products);
    $products->addAttributeToSelect('*');
    return $products;
}

3 Answers 3

3

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

<?php 
    $_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>
        </div>
        <?php echo '('.$RatingOb->getCount().')'; ?>
    </div> 
</div>

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

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

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;
}  
0

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
            //Description
$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(
            $currentStoreId
        )->addStatusFilter(
            \Magento\Review\Model\Review::STATUS_APPROVED
        )->addEntityFilter(
            'product',
            $_product->getId()
        )->setDateOrder();

$reviewDetails = $collection->getData();
foreach ($reviewDetails as $key => $review) {
                    print_r($review);
                    ?>
                        <div class="review-block">
                            <dt><span>By  <?php echo $review['nickname']; ?></span></dt>
                            <dd>
                                <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>
                            </dd>
                        </div>
                        <hr>
                    <?php
                }

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.