0

i want to get products rating using object manager i tried below mention code but its not working for me please guide me

$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();
  • fatal error:uncaught Error:using $this when not in object context error – Shan Haider Jul 9 '19 at 9:17
  • Please check my updated answer. – Rohan Hapani Jul 9 '19 at 9:20
  • @RohanHapani i want to product rating without percentage simple key name rating encode=>4 etc – Shan Haider Jul 9 '19 at 9:20
  • warning:Division by zero out put @RohanHapani – Shan Haider Jul 9 '19 at 9:23
  • Check it now... – Rohan Hapani Jul 9 '19 at 9:29
0

Try to use this below code :

Method 1 :

$product_id = '1';
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$rating = $objectManager->create('\Magento\Review\Model\Rating');
$review = $objectManager->create('\Magento\Review\Model\Review');
$storeManager = $objectManager->create("\Magento\Store\Model\StoreManagerInterface");
$_ratingSummary = $rating->getEntitySummary($product_id);
$ratingCollection = $review->getResourceCollection()
                    ->addStoreFilter($storeManager->getStore()->getId())
                    ->addStatusFilter(\Magento\Review\Model\Review::STATUS_APPROVED)
                    ->addEntityFilter('product', $product_id);
$review_count = count($ratingCollection); // How many review in that specific product
$product_rating = '';
if ($_ratingSummary->getSum() > 0 && $_ratingSummary->getCount() > 0) {
     $product_rating = $_ratingSummary->getSum() / $_ratingSummary->getCount(); // Product rating in percentage
}    

Method 2 :

    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $rating = $objectManager->create('\Magento\Review\Model\Rating');
    $storeManager = $objectManager->create("\Magento\Store\Model\StoreManagerInterface");
    $ratingCollection = $rating->getResourceCollection()->addEntityFilter(
        'product',$product_id
    )->setPositionOrder()->setStoreFilter(
        $storeManager->getStore()->getId()
    )->addRatingPerStoreName(
        $storeManager->getStore()->getId()
    )->load();
    echo "<pre>";print_r($ratingCollection->getData());

Output Like :

Array
(
    [0] => Array
        (
            [rating_id] => 3
            [entity_id] => 1
            [rating_code] => Rating
            [position] => 0
            [is_active] => 1
            [entity_code] => product
        )

)

enter image description here

Note : Avoid to use Object Manager Method. Use Factory Method Structure.

Reference

| 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.