cancel
Showing results for 
Search instead for 
Did you mean: 

Magento2: How to get rating stars from bestselling products collection

   Did you know you can see the translated content as per your choice?

Translation is in progress. Please check again after few minutes.

Magento2: How to get rating stars from bestselling products collection

Hi All

 

I am not able to get rating star in bestselling product.

 

Here is my code:

 <div class="row no-gutters">
    <?php $collection->setPeriod('year');
    	$i = 1;
    	foreach ($collection as $item) {
    							
    	$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    	$product = $objectManager->create('Magento\Catalog\Model\Product')->load($item->getProductId());
    	//echo '<pre>'; print_r($product->getData()); echo '</pre>';
    	if($i <= 4) {
    	?>
          <div class="contentBsSeller text-center">
            <div class="product-list-rating-stars">
    		<?php  echo $block->getReviewsSummaryHtml($product, "short", true); ?>
    		  </div>
                   </div>
    	<?php  } $i++; } ?>  
                    </div>

 

 

Please look into my code and provide the solution

4 REPLIES 4

Re: Magento2: How to get rating stars from bestselling products collection

Hello @Shiwani Miglani 

 

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

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()); //
 \Magento\Review\Model\ReviewFactory $reviewFactory
 $ratingSummary = $product->getRatingSummary()->getRatingSummary(); return $ratingSummary; }




Problem solved? Click Kudos & Accept as Solution!
Sunil Patel
Magento 2 Certified Professional Developer & Frontend Developer

Re: Magento2: How to get rating stars from bestselling products collection

Thanks @Sunil Patel 

 

I got the solution for this.

 

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$productCollection = $objectManager->create('Magento\Reports\Model\ResourceModel\Report\Collection\Factory');
$collection = $productCollection->create('Magento\Sales\Model\ResourceModel\Report\Bestsellers\Collection');
$collection->setPeriod('year');
$i = 1;
foreach ($collection as $item) {
$product = $objectManager->create('Magento\Catalog\Model\Product')->load($item->getProductId());
$summary = $product->getRatingSummary()->getRatingSummary();
$reviewcount = $product->getRatingSummary()->getReviewsCount();
<div class="product-list-rating-stars">
<div class="product-reviews-summary<?php echo !$summary ? ' no-rating' : ''?>">
<?php if ($summary){ ?>
<div class="rating-summary">
<div class="rating-result">
<i class="star">&#9733;</i>
<i class="star">&#9733;</i>
<i class="star">&#9733;</i>
<i class="star">&#9733;</i>
<i class="star">&#9733;</i>
<span style="width:<?php echo $summary; ?>%">
<i class="star">&#9733;</i>
<i class="star">&#9733;</i>
<i class="star">&#9733;</i>
<i class="star">&#9733;</i>
<i class="star">&#9733;</i>
</span>
<span class="review-count">(<?php echo $reviewcount; ?>)</span>
</div>
</div>
<?php } ?>
</div>
</div>
}

 

Re: Magento2: How to get rating stars from bestselling products collection

Hello @Shiwani Miglani 

 

don't use object manger into phtml file.

 

if works then mark as the solution.

 

 


Problem solved? Click Kudos & Accept as Solution!
Sunil Patel
Magento 2 Certified Professional Developer & Frontend Developer

Re: Magento2: How to get rating stars from bestselling products collection

Can I get product review sort using the below code?  

 

$categoryProducts = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\Collection');
$categoryProducts->addAttributeToSelect('*');

I want to use this in a filter.