- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-06-2019
02:57 AM
05-06-2019
02:57 AM
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
Labels:
4 REPLIES 4
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-06-2019
03:04 AM
05-06-2019
03:04 AM
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
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-06-2019
10:10 PM
05-06-2019
10:10 PM
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">★</i>
<i class="star">★</i>
<i class="star">★</i>
<i class="star">★</i>
<i class="star">★</i>
<span style="width:<?php echo $summary; ?>%">
<i class="star">★</i>
<i class="star">★</i>
<i class="star">★</i>
<i class="star">★</i>
<i class="star">★</i>
</span>
<span class="review-count">(<?php echo $reviewcount; ?>)</span>
</div>
</div>
<?php } ?>
</div>
</div>
}
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-06-2019
11:04 PM
05-06-2019
11:04 PM
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
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-26-2019
10:43 PM
12-26-2019
10:43 PM
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.