0

How can I get Detailed Rating results programatically using product id in magento 2.3.5?

I want to display it on product page.

Like this: Product: 5 stars shipping: 4 stars

Thanks!

2 Answers 2

1

You can get Product rating details by Product id with the use of RatingFactory. Check the below code for this:

<?php

namespace Vendor\Module\Model;
use Magento\Framework\Model\AbstractModel;
class ProductReviews extends AbstractModel{

    protected $_ratingFactory;
    protected $_productFactory;
    protected $_ratingFactory;
    protected $_reviewFactory;

    public function __construct(
            \Magento\Store\Model\StoreManagerInterface $storeManager,
            \Magento\Catalog\Model\ProductFactory $productFactory,
            \Magento\Review\Model\RatingFactory $ratingFactory,
            \Magento\Review\Model\ResourceModel\Review\CollectionFactory $reviewFactory,
        ) {
            $this->_storeManager = $storeManager;
            $this->_productFactory = $productFactory;
            $this->_ratingFactory = $ratingFactory;
            $this->_reviewFactory = $reviewFactory;
        }

    public function getReviewCollection($productId){
        $collection = $this->_reviewFactory->create()
        ->addStatusFilter(
            \Magento\Review\Model\Review::STATUS_APPROVED
        )->addEntityFilter(
            'product',
            $productId
        )->setDateOrder();

    }

    public function getRatingCollection(){
        $ratingCollection = $this->_ratingFactory->create()
        ->getResourceCollection()
        ->addEntityFilter(
            'product' 
        )->setPositionOrder()->setStoreFilter(
            $this->_storeManager->getStore()->getId()
        )->addRatingPerStoreName(
            $this->_storeManager->getStore()->getId()
        )->load();

        return $ratingCollection->getData();
    }

}
1
  • Thank you! But I think I expressed myself poorly. I want to get one value per time to use it to define some business rules, for sample: 0 to 20 per cent of shipping review = show text "bad product shipment" or something else. And to do this, I need to get this informations individually. Mar 16, 2021 at 13:12
0

You can try this code:

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

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.