5
<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->get('Magento\Framework\Registry')->registry('current_product');
$storeManager = $objectManager->get('Magento\Store\Model\StoreManagerInterface');
$currentStoreId = $storeManager->getStore()->getId();
$rating = $objectManager->get("Magento\Review\Model\ResourceModel\Review\CollectionFactory");
$reviewFactory = $objectManager->create('Magento\Review\Model\Review');

$collection = $rating->create()->addStoreFilter(
    $currentStoreId
    )->addStatusFilter(
    \Magento\Review\Model\Review::STATUS_APPROVED
    )->addEntityFilter(
    'product',
    $product->getId()
    )->setDateOrder();
    $reviewlist = $collection->getData();
    foreach ($reviewlist as $review) 
    {
        echo "<pre/>";
        print_r($review);
    }

#######################################################

Output
    [review_id] => 
    [created_at] => 
    [entity_id] => 
    [entity_pk_value] => 
    [status_id] => 
    [detail_id] => 
    [title] => 
    [detail] => 
    [nickname] => 
    [customer_id] => 
    [entity_code] => 

In that I can not get the review star per review with the help of object manager. enter image description here I want highlighted box value

3
  • first of all, objectmanager is a bad idea to implement. Commented Jan 31, 2018 at 10:20
  • I know that, but can you help with my issue?@ShyamKrishnaSreekumar Commented Jan 31, 2018 at 10:24
  • you can try with magento 2 product reviews extension it will easy to install and resolve your query
    – nikunj
    Commented Jun 2, 2021 at 11:00

3 Answers 3

4

Try to use bellow code in your phtml file You should not use the ObjectManager directly!

<?php
$productId = 'your_product_id';
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$reviewFactory = $objectManager->create('Magento\Review\Model\Review');
$product = $objectManager->create('Magento\Catalog\Model\Product')->load($productId);
$storeManager  = $objectManager->create('\Magento\Store\Model\StoreManagerInterface');
$storeId = $storeManager->getStore()->getStoreId();
$reviewFactory->getEntitySummary($product, $storeId);

$ratingSummary = $product->getRatingSummary()->getRatingSummary();
$reviewCount = $product->getRatingSummary()->getReviewsCount();
?>
<?php if($ratingSummary){ ?>
<div class="product-reviews-summary short">
    <div class="rating-summary">
        <div title="<?php echo (int)$ratingSummary; ?>%" class="rating-result">
            <span style="width:<?php echo (int)$ratingSummary; ?>%"><span><?php echo (int)$ratingSummary; ?>%</span></span>
        </div>
    </div>
    <div class="reviews-actions">
        <?php echo __('('.$reviewCount.') Reviews'); ?>
    </div>
</div>
<?php } ?>
1
  • I used this code and get an overall rating amount, but i want individual rating value and also updated question.@Chander Shekhar Commented Jan 31, 2018 at 11:57
2
I found one link that helps to get a collection of each review
    <?php 
    use Magento\Framework\App\Bootstrap;  
    require __DIR__ . '/app/bootstrap.php';  
    $bootstrap = Bootstrap::create(BP, $_SERVER);  
    $obj = $bootstrap->getObjectManager();  
    $state = $obj->get('Magento\Framework\App\State');  
    $state->setAreaCode('frontend');  

    $reviewId = 5; //custom review id.  
    $ratingCollection = $obj->get('Magento\Review\Model\ResourceModel\Rating\Option\Vote\Collection')
                       ->addRatingInfo()
                       ->addOptionInfo()
                       ->addRatingOptions()
                       ->addFieldToFilter('review_id',$reviewId);  
    print_r($ratingCollection->getData());
1

you need to call $collection->load()->addRateVotes();, than get ratings for each review by calling $review->getRatingVotes();

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.