3

I need to show the rating for the products in my custom slider in my home page.

Here im getting just the rating number say: 60 .

How to get the rating stars here in template.

Featuredproduct\view\frontend\templates\featured_grid.phtml

<?php
                    $_ratingSummary = $block->getRatingSummary($_product->getId());
                    $_reviewCount = $_ratingSummary->getRatingCount();
                    $_ratingSummary =$_ratingSummary->getRatingSummary(); 

                    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.')'); ?>
                        </div>
                    </div>
                    <?php } ?>

Featuredproduct\Block\Featuredproduct.php

protected $_reviewFactory;
public function __construct(
    \Magento\Catalog\Block\Product\Context $context,
    \Magento\Framework\App\ResourceConnection $resource,
    \Magento\Framework\Url\Helper\Data $urlHelper,
    \*Vendor*\Featuredproduct\Model\Resource\Customcollection\Collection $fpmanualCollection,
    \Magento\Framework\Stdlib\DateTime\DateTime $datetime,
    \Magento\Sales\Model\Order\Status\History $orderstatus,
    \Magento\Catalog\Model\ResourceModel\Product\Collection $productCollection,
    \Magento\CatalogInventory\Helper\Stock $stockHelper,
    \Magento\Review\Model\ReviewFactory $reviewFactory,
    //\Magento\Review\Model\ResourceModel\Rating\Collection $ratingCollection,
    \Magento\Review\Model\ResourceModel\Review\Summary\Collection $ratingCollection,
    \Magento\Catalog\Model\ProductFactory $_productloader,
        array $data = []
)
{
     $this->_coreResource = $resource;
     $this->urlHelper = $urlHelper;
     $this->_fpmanualCollection = $fpmanualCollection;
     $this->_datetime=$datetime;
     $this->_orderstatus=$orderstatus;
     $this->productCollection=$productCollection;
     $this->ratingCollection=$ratingCollection;
     $this->stockHelper=$stockHelper;

     $this->_reviewFactory = $reviewFactory;
     $this->_productloader = $_productloader;

    parent::__construct($context,$data);


}



public function getRatingSummary($id)
{ 
    $product = $this->_productloader->create()->load($id); // follow the link for this
    $this->_reviewFactory->create()->getEntitySummary($product, $this->_storeManager->getStore()->getId());
    $ratingSummary = $product->getRatingSummary();

    return $ratingSummary;
}
  • Do you want to get average rating of product – Arunendra Mar 2 '17 at 9:35
  • yes ,exactly, i need to get the average rating star in my home page product slider, i followed this : community.magento.com/t5/Programming-Questions/… But need to know where i have to put this – Sushivam Mar 2 '17 at 9:37
  • You can add it your slider's module or extend that module – Arunendra Mar 2 '17 at 9:43
  • Sorry, You have to do it your self . – Arunendra Mar 2 '17 at 10:26
  • i have updated the question where i need to get the average rating stars, could u suggest on this – Sushivam Mar 2 '17 at 11:23
9

Getting Review star in phtml file,

   <?php 
        //pass $product object as argument...
        $_ratingSummary = $block->getRatingSummary($product);
        $_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.')'); ?>
        </div>
    </div>
    <?php } ?>

In Block file,

public function getRatingSummary($product)
{
    $this->_reviewFactory->create()->getEntitySummary($product, $this->_storeManager->getStore()->getId());
    $ratingSummary = $product->getRatingSummary()->getRatingSummary();
    return $ratingSummary;
}
  • can you please help me to get the reviews of the product by customers. – srg Sep 6 '17 at 11:25
  • can you please help me to get the reviews star of the product by customers. @Rakesh – Dhaduk Mitesh May 24 '18 at 5:06
2

You have to create and call this method with product ID. It will return rating count of each star. You can calculate the average rating with this result

public function getAllStart($pid) {
    $review = $this->_objectReview->getCollection()     //\Magento\Review\Model\Review $reviewFactory (_objectReview)
            ->addFieldToFilter('main_table.status_id', 1)
            ->addEntityFilter('product', $pid)          //$pid = > your current product ID
            ->addStoreFilter($this->_storeManager->getStore()->getId())
            ->addFieldToSelect('review_id')
    ;
    $review->getSelect()->columns('detail.detail_id')->joinInner(
            ['vote' => $review->getTable('rating_option_vote')], 'main_table.review_id = vote.review_id', array('review_value' => 'vote.value')
    );
    $review->getSelect()->order('review_value DESC');
    $review->getSelect()->columns('count(vote.vote_id) as total_vote')->group('review_value');
    for ($i = 5; $i >= 1; $i--) {
        $arrRatings[$i]['value'] = 0;
    }
    foreach ($review as $_result) {
        $arrRatings[$_result['review_value']]['value'] = $_result['total_vote'];
    }
    return $arrRatings;
    }
2

In your slider phtml file code just add this function

$product = $this->getRatingSummary($passProductIdHere);
var_dump($product);

Now in Featuredproduct.php just load that product (Follow this link for load product) to get rating for exp :

public function getRatingSummary($id)
{ 
    $product = $this->_productloader->create()->load($id); // follow the link for this
    $this->_reviewFactory->create()->getEntitySummary($product, $this->_storeManager->getStore()->getId());
    $ratingSummary = $product->getRatingSummary()->getRatingSummary();

    return $ratingSummary;
}
  • where is the link? – Sushivam Mar 2 '17 at 12:47
  • @SachinS updated the ans – Arunendra Mar 2 '17 at 12:59
  • Have updated my question @Arunendra, i m getting the rating number now in my block, how to get the average rating star – Sushivam Mar 3 '17 at 8:54

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.