0

I'm trying to use Magento 2 review and rating. I used the object manager. My code is :

        <?php    
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); 
    $product = $objectManager->get('Magento\Framework\Registry')->registry('current_product');//get current product
    $id = $product->getId();
    $ratingCollection = $objectManager->get('Magento\Review\Model\ResourceModel\Rating\Option\Vote\Collection')->addRatingInfo()->addOptionInfo()->addRatingOptions()->addFieldToFilter('entity_pk_value',$id);
    $ratting = 0;
    foreach($ratingCollection as $ratingCollections){

        $ratting += $ratingCollections['percent'];
        $review_rate = $ratting / 3;
        echo $review_rate ;
    }
    ?>

I have totally four products and rating approved for only one product.When I var_dump($ratingCollection) I get array(0){} for product which don't have ratting. and dumping value for products having a rating. But the issue is when I echo $review_rate; I get the ratting approved product's in all product detail page but I have to rating only for one product.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.