{"id":7286,"date":"2019-09-23T10:50:55","date_gmt":"2019-09-23T05:20:55","guid":{"rendered":"https:\/\/www.hiddentechies.com\/blog\/?p=7286"},"modified":"2019-09-27T13:44:32","modified_gmt":"2019-09-27T08:14:32","slug":"magento-2-get-product-reviews-collection-rating","status":"publish","type":"post","link":"https:\/\/www.hiddentechies.com\/blog\/magento-2\/magento-2-get-product-reviews-collection-rating\/","title":{"rendered":"Magento 2 &#8211; How to Get Product Reviews Collection with Rating"},"content":{"rendered":"<p>In this post I am going to explain how to get product reviews with rating in magento 2.<\/p>\n<p><strong>1. Using Dependency Injection<\/strong><\/p>\n<p>Add below code snippet in Block class.<\/p>\n<pre class=\"lang:default decode:true \">protected $_reviewCollectionFactory;\r\nprotected $_storeManager;\r\n\t\r\npublic function __construct(\r\n\t\\Magento\\Backend\\Block\\Template\\Context $context,        \r\n\t\\Magento\\Review\\Model\\ResourceModel\\Review\\CollectionFactory $reviewCollectionFactory,\r\n\t\\Magento\\Store\\Model\\StoreManagerInterface $storeManager,\r\n\tarray $data = []\r\n)\r\n{\r\n\t$this-&gt;_reviewCollectionFactory = $reviewCollectionFactory;\r\n\t$this-&gt;_storeManager = $storeManager;\r\n\tparent::__construct($context, $data);\r\n}\r\n\r\npublic function getCurrentStoreId() {\r\n\treturn $this-&gt;_storeManager-&gt;getStore()-&gt;getId();\r\n}\r\n\r\npublic function getReviewsCollection() \r\n{\r\n\t$currentStoreId = $this-&gt;getCurrentStoreId();\r\n\r\n\t$reviewsCollection = $this-&gt;_reviewCollectionFactory-&gt;create()\r\n\t\t\t-&gt;addFieldToSelect('*')\r\n\t\t\t-&gt;addStoreFilter($currentStoreId)\r\n\t\t\t-&gt;addStatusFilter(\\Magento\\Review\\Model\\Review::STATUS_APPROVED)\r\n\t\t\t-&gt;setDateOrder()\r\n\t\t\t-&gt;addRateVotes();\r\n\treturn $reviewsCollection;\r\n}<\/pre>\n<p>Add below code snippet in template file.<\/p>\n<pre class=\"lang:default decode:true \">\/\/ Get reviews collection\r\n$reviewsCollection = $block-&gt;getReviewsCollection();\r\n\r\necho \"&lt;pre&gt;\";\r\nprint_r($reviewsCollection-&gt;getData());\r\necho \"&lt;\/pre&gt;\";\r\n\r\n$dateFormat = $block-&gt;getDateFormat() ? : \\IntlDateFormatter::SHORT;\r\n\r\nif ($reviewsCollection &amp;&amp; count($reviewsCollection) &gt; 0) {\r\n    foreach ($reviewsCollection AS $review) {\r\n        echo $review-&gt;getTitle() . \"&lt;br\/&gt;\";\r\n        echo $review-&gt;getDetail() . \"&lt;br\/&gt;\";\r\n        echo $review-&gt;getNickname() . \"&lt;br\/&gt;\";\r\n        echo $block-&gt;formatDate($review-&gt;getCreatedAt(), $dateFormat) . \"&lt;br\/&gt;\";\r\n\r\n        \/\/ Display Average Rating of Review\r\n        $countRatings = count($review-&gt;getRatingVotes());\r\n        if ($countRatings &gt; 0) {\r\n            ?&gt;\r\n            &lt;div class=\"review-ratings\"&gt;\r\n                &lt;?php\r\n                $allRatings = 0;\r\n                foreach ($review-&gt;getRatingVotes() as $vote) {\r\n                    $allRatings = $allRatings + $vote-&gt;getPercent();\r\n                }\r\n                $allRatingsAvg = $allRatings \/ $countRatings;\r\n                ?&gt;\r\n                &lt;div class=\"rating-summary\"&gt;\r\n                    &lt;div class=\"rating-result\" title=\"&lt;?php echo $allRatingsAvg; ?&gt;%\"&gt;\r\n                        &lt;meta itemprop=\"worstRating\" content = \"1\"\/&gt;\r\n                        &lt;meta itemprop=\"bestRating\" content = \"100\"\/&gt;\r\n                        &lt;span style=\"width:&lt;?php echo $allRatingsAvg; ?&gt;%\"&gt;\r\n                            &lt;span itemprop=\"ratingValue\"&gt;&lt;?php echo $allRatingsAvg; ?&gt;%&lt;\/span&gt;\r\n                        &lt;\/span&gt;\r\n                    &lt;\/div&gt;\r\n                &lt;\/div&gt;\r\n            &lt;\/div&gt;\r\n            &lt;?php\r\n        }\r\n    }\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>2. Using Object Manager<\/strong><\/p>\n<pre class=\"lang:default decode:true \">$objectManager = \\Magento\\Framework\\App\\ObjectManager::getInstance();\r\n\r\n\/\/Get current store id\r\n$storeManager = $objectManager-&gt;get('\\Magento\\Store\\Model\\StoreManagerInterface');\r\n$currentStoreId = $storeManager-&gt;getStore()-&gt;getId();\r\n\r\n$reviewCollectionFactory = $objectManager-&gt;create('Magento\\Review\\Model\\ResourceModel\\Review\\CollectionFactory')-&gt;create();\r\n\r\n\/\/ Get reviews collection\r\n$reviewsCollection = $reviewCollectionFactory-&gt;addFieldToSelect('*')\r\n        -&gt;addStoreFilter($currentStoreId)\r\n        -&gt;addStatusFilter(\\Magento\\Review\\Model\\Review::STATUS_APPROVED)\r\n        -&gt;setDateOrder()\r\n        -&gt;addRateVotes();\r\n\r\necho \"&lt;pre&gt;\";\r\nprint_r($reviewsCollection-&gt;getData());\r\necho \"&lt;\/pre&gt;\";\r\n\r\n$dateFormat = $block-&gt;getDateFormat() ? : \\IntlDateFormatter::SHORT;\r\n\r\nif ($reviewsCollection &amp;&amp; count($reviewsCollection) &gt; 0) {\r\n    foreach ($reviewsCollection AS $review) {\r\n        echo $review-&gt;getTitle() . \"&lt;br\/&gt;\";\r\n        echo $review-&gt;getDetail() . \"&lt;br\/&gt;\";\r\n        echo $review-&gt;getNickname() . \"&lt;br\/&gt;\";\r\n        echo $block-&gt;formatDate($review-&gt;getCreatedAt(), $dateFormat) . \"&lt;br\/&gt;\";\r\n\r\n        \/\/ Display Average Rating of Review\r\n        $countRatings = count($review-&gt;getRatingVotes());\r\n        if ($countRatings &gt; 0) {\r\n            ?&gt;\r\n            &lt;div class=\"review-ratings\"&gt;\r\n                &lt;?php\r\n                $allRatings = 0;\r\n                foreach ($review-&gt;getRatingVotes() as $vote) {\r\n                    $allRatings = $allRatings + $vote-&gt;getPercent();\r\n                }\r\n                $allRatingsAvg = $allRatings \/ $countRatings;\r\n                ?&gt;\r\n                &lt;div class=\"rating-summary\"&gt;\r\n                    &lt;div class=\"rating-result\" title=\"&lt;?php echo $allRatingsAvg; ?&gt;%\"&gt;\r\n                        &lt;meta itemprop=\"worstRating\" content = \"1\"\/&gt;\r\n                        &lt;meta itemprop=\"bestRating\" content = \"100\"\/&gt;\r\n                        &lt;span style=\"width:&lt;?php echo $allRatingsAvg; ?&gt;%\"&gt;\r\n                            &lt;span itemprop=\"ratingValue\"&gt;&lt;?php echo $allRatingsAvg; ?&gt;%&lt;\/span&gt;\r\n                        &lt;\/span&gt;\r\n                    &lt;\/div&gt;\r\n                &lt;\/div&gt;\r\n            &lt;\/div&gt;\r\n            &lt;?php\r\n        }\r\n    }\r\n}<\/pre>\n<p>Thats it. Enjoy Magento 2!!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this post I am going to explain how to get product reviews with rating in magento 2. 1. Using Dependency Injection Add below code snippet in Block class. protected $_reviewCollectionFactory; protected $_storeManager; public function __construct( \\Magento\\Backend\\Block\\Template\\Context $context, \\Magento\\Review\\Model\\ResourceModel\\Review\\CollectionFactory $reviewCollectionFactory, \\Magento\\Store\\Model\\StoreManagerInterface $storeManager, array $data = [] ) { $this-&gt;_reviewCollectionFactory = $reviewCollectionFactory; $this-&gt;_storeManager = $storeManager; parent::__construct($context,&#8230; <\/p>\n<div class=\"actions\"><a href=\"https:\/\/www.hiddentechies.com\/blog\/magento-2\/magento-2-get-product-reviews-collection-rating\/\">Continue Reading<\/a><\/div>\n","protected":false},"author":1,"featured_media":7341,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[59],"tags":[10,27,690],"_links":{"self":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts\/7286"}],"collection":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/comments?post=7286"}],"version-history":[{"count":1,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts\/7286\/revisions"}],"predecessor-version":[{"id":7287,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/posts\/7286\/revisions\/7287"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/media\/7341"}],"wp:attachment":[{"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/media?parent=7286"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/categories?post=7286"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hiddentechies.com\/blog\/wp-json\/wp\/v2\/tags?post=7286"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}