- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-29-2021
03:26 AM
12-29-2021
03:26 AM
How to count of 5 star, 4 star, 3 star, 2 star and 1 star by parameter id?
I want to count all review products in Magento 2 like :
There are 5 reviews 1 star
There are 3 reviews 2 star ....
Pls Help me, Thanks
Labels:
1 REPLY 1
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-30-2021
03:32 AM
12-30-2021
03:32 AM
Re: How to count of 5 star, 4 star, 3 star, 2 star and 1 star by parameter id?
Try the below solution:
Create and call the below code with product ID.
Don't forget to enable 1 rating option in admin.
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; }
Problem solved? Click Kudos and "Accept as Solution".
200+ Magento 2 Extensions for Enhanced Shopping Experience.
200+ Magento 2 Extensions for Enhanced Shopping Experience.