1

I have this piece of code found on StackExchange, to show all the reviews from a store store view. And it works great! But how can I extend it to also show the rating and stars for each review?

Thank you,

<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$_reviewsColFactory = $objectManager->get("\Magento\Review\Model\ResourceModel\Review\CollectionFactory");
$_storeManager = $objectManager->get('\Magento\Store\Model\StoreManagerInterface');
$reviewsCollection = $_reviewsColFactory->create()
->addStoreFilter($_storeManager->getStore()->getStoreId())
->addStatusFilter(\Magento\Review\Model\Review::STATUS_APPROVED)
->setDateOrder();

foreach($reviewsCollection as $review):?> <div class='review-single col-md-12'> <div class="box-review-single"> <h4 id="title-review"><?php echo $review['title']; ?></h4> <span>Rating and Stars</span> <p id="detail-review"><em><?php echo $review['detail']; ?></em></p> </div> </div> <?php endforeach; ?>

UPDATE

@Msquare

This is the code I'm trying:

<?php
protected $_storeManager;
protected $_reviewsCollection;
protected $ratingAttributeFactory;
protected $ratingFactory;

public function __construct( \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Review\Model\ResourceModel\Rating\Option\Vote\CollectionFactory $ratingFactory, \Magento\Review\Model\ResourceModel\Review\CollectionFactory $collectionFactory, \Magento\Review\Model\ResourceModel\Rating\CollectionFactory $ratingAttributeFactory

) { $this->_reviewsColFactory = $collectionFactory; $this->ratingFactory = $ratingFactory; $this->_storeManager = $storeManager; $this->ratingAttributeFactory = $ratingAttributeFactory; }

public function execute() { echo "<pre>"; $review_collection = $this->_reviewsCollection = $this->_reviewsColFactory->create()->addStoreFilter( $this->_storeManager->getStore()->getId() )->setDateOrder(); $final_rate_value = []; $default_rate_value = []; $finalRatingData = []; // get All Reviews foreach ($review_collection as $reviewKey => $reviewValue) { // get review enable label name $ratingAttribute_collection = $this->ratingAttributeFactory->create()->addFieldToFilter('is_active', 1); $rating_collection = $this->ratingFactory->create()->addFieldToFilter('review_id', $reviewValue->getId()); $x = 0; // get rating values $finalRatingData[$reviewValue->getId()]['id'] = $reviewValue->getId(); $finalRatingData[$reviewValue->getId()]['title'] = $reviewValue->getTitle(); $finalRatingData[$reviewValue->getId()]['detail'] = $reviewValue->getDetail(); $finalRatingData[$reviewValue->getId()]['nickname'] = $reviewValue->getNickname(); $finalRatingData[$reviewValue->getId()]['entity_id'] = $reviewValue->getEntityId(); $finalRatingData[$reviewValue->getId()]['status_id'] = $reviewValue->getStatusId(); foreach ($rating_collection as $ratingKey => $ratingValue) { // get rating label name foreach ($ratingAttribute_collection as $rat_attribute_key => $rat_attribute_value) { if ($ratingValue->getRatingId() == $rat_attribute_value->getRatingId()) { $final_rate_value[$reviewValue->getId()][$rat_attribute_value->getRatingCode()] = $ratingValue->getPercent(); $default_rate_value[$reviewValue->getId()][$ratingValue->getRatingId()] = $rat_attribute_value->getRatingCode(); } } } } foreach ($final_rate_value as $final_rateKey => $final_rateValue) { if (!empty($final_rateValue)) { foreach ($final_rateValue as $FinalKey => $finalValue) { if ($finalValue == 20) { $finalRatingData[$final_rateKey][$FinalKey] = '&#9733;&nbsp;&#9734;&nbsp;&#9734;&nbsp;&#9734;&nbsp;&#9734;&nbsp;'; } elseif ($finalValue == 40) { $finalRatingData[$final_rateKey][$FinalKey] = '&#9733;&nbsp;&#9733;&nbsp;&#9734;&nbsp;&#9734;&nbsp;&#9734;&nbsp;'; } elseif ($finalValue == 60) { $finalRatingData[$final_rateKey][$FinalKey] = '&#9733;&nbsp;&#9733;&nbsp;&#9733;&nbsp;&#9734;&nbsp;&#9734;&nbsp;'; } elseif ($finalValue == 80) { $finalRatingData[$final_rateKey][$FinalKey] = '&#9733;&nbsp;&#9733;&nbsp;&#9733;&nbsp;&#9733;&nbsp;&#9734;&nbsp;'; } elseif ($finalValue == 100) { $finalRatingData[$final_rateKey][$FinalKey] = '&#9733;&nbsp;&#9733;&nbsp;&#9733;&nbsp;&#9733;&nbsp;&#9733;&nbsp;'; } else { $finalRatingData[$final_rateKey][$FinalKey] = '&#9734;&nbsp;&#9734;&nbsp;&#9734;&nbsp;&#9734;&nbsp;&#9734;&nbsp;'; }

    }
}

} echo "Your Rating Star Data <br/>"; print_r($finalRatingData); exit(); } ?>

--- UPDATE No 2 ---

I have come so far, that I don't have any more error messages, but nothing is shown where I include the phtml-file in a CMS.

In the following file:

\app\design\frontend\Smartwave\alldogroup\Magento_Review\layout\default.xml

I have this:

<block class="AlldoGroup\AllReviews\Block" name="show_all_reviews" template="magento_review::allreviews.phtml" cacheable="false"/>

And on the CMS home page I have:

{{block class="AlldoGroup\AllReviews\Block\AllReviews" template="magento_review::allreviews.phtml"}}

The Block path to AllReviews.php are:

\app\code\AlldoGroup\AllReviews\Block\AllReviews.php

Is this correct?

Aknot
  • 288
  • 10
  • 28

1 Answers1

2

Add Block to your Layout

   <block class="VendoreName\ModuleName\Block\AllReviews"
          name="show_all_reviews"
          template="VendoreName_ModuleName::allreviews.phtml"
          cacheable="false"/>

Add allreviews.phtml file at app/code/VendoreName/ModuleName/view/frontend/templates

<?php
$reviewData = $block->getAllRatingData();
foreach ($reviewData as $reviewKey => $reviewValue) {
    echo "<pre>";
    print_r($reviewValue);
    echo "</pre>";
    ?>
    <div class='review-single col-md-12'>
        <div class="box-review-single">
            <h4 id="title-review"><?php echo $reviewValue['title'];  ?></h4>
            <span>Rating and Stars</span>
            <p id="detail-review"><em><?php echo $reviewValue['detail'];  ?></em></p>
        </div>
    </div>
    <?php
}
?>

Add AllReviews.php file at app/code/VendoreName/ModuleName/Block

<?php

namespace VendoreName\ModuleName\Block;

use Magento\Framework\View\Element\Template;

class AllReviews extends Template { protected $customerSession; protected $_storeManager; protected $_localeCurrency; protected $_reviewsCollection; protected $ratingAttributeFactory; protected $ratingFactory;

public function __construct(
    Template\Context $context,
    \Magento\Store\Model\StoreManagerInterface $storeManager,
    \Magento\Review\Model\ResourceModel\Rating\Option\Vote\CollectionFactory $ratingFactory,
    \Magento\Review\Model\ResourceModel\Review\CollectionFactory $collectionFactory,
    \Magento\Review\Model\ResourceModel\Rating\CollectionFactory $ratingAttributeFactory,
    array $data = []
) {
    $this-&gt;_reviewsColFactory = $collectionFactory;
    $this-&gt;ratingFactory = $ratingFactory;
    $this-&gt;_storeManager = $storeManager;
    $this-&gt;ratingAttributeFactory = $ratingAttributeFactory;
    parent::__construct($context, $data);
}

public function getAllRatingData()
{
    $review_collection = $this-&gt;_reviewsCollection = $this-&gt;_reviewsColFactory-&gt;create()-&gt;addStoreFilter(
        $this-&gt;_storeManager-&gt;getStore()-&gt;getId()
    )-&gt;setDateOrder();
    $final_rate_value = [];
    $default_rate_value = [];
    $finalRatingData = [];
    // get All Reviews
    foreach ($review_collection as $reviewKey =&gt; $reviewValue) {
        // get review enable label name
        $ratingAttribute_collection = $this-&gt;ratingAttributeFactory-&gt;create()-&gt;addFieldToFilter('is_active', 1);
        $rating_collection = $this-&gt;ratingFactory-&gt;create()-&gt;addFieldToFilter('review_id', $reviewValue-&gt;getId());
        $x = 0;
        // get rating values
        $finalRatingData[$reviewValue-&gt;getId()]['id'] = $reviewValue-&gt;getId();
        $finalRatingData[$reviewValue-&gt;getId()]['title'] = $reviewValue-&gt;getTitle();
        $finalRatingData[$reviewValue-&gt;getId()]['detail'] = $reviewValue-&gt;getDetail();
        $finalRatingData[$reviewValue-&gt;getId()]['nickname'] = $reviewValue-&gt;getNickname();
        $finalRatingData[$reviewValue-&gt;getId()]['entity_id'] = $reviewValue-&gt;getEntityId();
        $finalRatingData[$reviewValue-&gt;getId()]['status_id'] = $reviewValue-&gt;getStatusId();
        foreach ($rating_collection as $ratingKey =&gt; $ratingValue) {
            // get rating label name
            foreach ($ratingAttribute_collection as $rat_attribute_key =&gt; $rat_attribute_value) {
                if ($ratingValue-&gt;getRatingId() == $rat_attribute_value-&gt;getRatingId()) {
                    $final_rate_value[$reviewValue-&gt;getId()][$rat_attribute_value-&gt;getRatingCode()] = $ratingValue-&gt;getPercent();
                    $default_rate_value[$reviewValue-&gt;getId()][$ratingValue-&gt;getRatingId()] = $rat_attribute_value-&gt;getRatingCode();
                }
            }
        }
    }
    foreach ($final_rate_value as $final_rateKey =&gt; $final_rateValue) {
        if (!empty($final_rateValue)) {
            foreach ($final_rateValue as $FinalKey =&gt; $finalValue) {
                if ($finalValue == 20) {
                    $finalRatingData[$final_rateKey][$FinalKey] = '&amp;#9733;&amp;nbsp;&amp;#9734;&amp;nbsp;&amp;#9734;&amp;nbsp;&amp;#9734;&amp;nbsp;&amp;#9734;&amp;nbsp;';
                } elseif ($finalValue == 40) {
                    $finalRatingData[$final_rateKey][$FinalKey] = '&amp;#9733;&amp;nbsp;&amp;#9733;&amp;nbsp;&amp;#9734;&amp;nbsp;&amp;#9734;&amp;nbsp;&amp;#9734;&amp;nbsp;';
                } elseif ($finalValue == 60) {
                    $finalRatingData[$final_rateKey][$FinalKey] = '&amp;#9733;&amp;nbsp;&amp;#9733;&amp;nbsp;&amp;#9733;&amp;nbsp;&amp;#9734;&amp;nbsp;&amp;#9734;&amp;nbsp;';
                } elseif ($finalValue == 80) {
                    $finalRatingData[$final_rateKey][$FinalKey] = '&amp;#9733;&amp;nbsp;&amp;#9733;&amp;nbsp;&amp;#9733;&amp;nbsp;&amp;#9733;&amp;nbsp;&amp;#9734;&amp;nbsp;';
                } elseif ($finalValue == 100) {
                    $finalRatingData[$final_rateKey][$FinalKey] = '&amp;#9733;&amp;nbsp;&amp;#9733;&amp;nbsp;&amp;#9733;&amp;nbsp;&amp;#9733;&amp;nbsp;&amp;#9733;&amp;nbsp;';
                } else {
                    $finalRatingData[$final_rateKey][$FinalKey] = '&amp;#9734;&amp;nbsp;&amp;#9734;&amp;nbsp;&amp;#9734;&amp;nbsp;&amp;#9734;&amp;nbsp;&amp;#9734;&amp;nbsp;';
                }

            }
        }
    }
    return $finalRatingData;
}

}

Click Here to download

Use into CMS page {{block class="Test\Blog\Block\AllReviews" name="test" template="Test_Blog::allreviews.phtml"}}

I Hope This Helps You.

Msquare
  • 9,063
  • 7
  • 25
  • 63
  • Thanks, trying your code, but we get the following error:

    [2020-12-08 12:50:21] main.CRITICAL: ParseError: syntax error, unexpected 'protected' (T_PROTECTED), expecting end of file in app/design/frontend/Vendor/Theme/Magento_Review/templates/allreviews.phtml:22

    Please advise,

    – Aknot Dec 08 '20 at 13:06
  • Please share your updated file code into your question for more detail. – Msquare Dec 09 '20 at 03:52
  • Did a update on the original post above, where you can se the code. Thanks. – Aknot Dec 09 '20 at 05:47
  • 1
    Add above files and run magento upgrade command. you must add block to your layout. Please check and update me. – Msquare Dec 09 '20 at 06:46
  • Thanks, I'm afraid it doesn't work. I have done exactly as you described. I'm getting this error:

    "We're sorry, an error has occurred while generating this content."

    Maybe I need to create a extension instead? What should the name of the layout file be? show_all_reviews.xml?

    – Aknot Dec 09 '20 at 07:53
  • Before run please run magento commnad php bin/magento setup:upgrade. where you want to show all reviews ? Yes you can create extension. – Msquare Dec 09 '20 at 08:29
  • First I ran php bin/magento setup:upgrade and then deploy. I also ran cache:flush. I want to be able to run it on any CMS page. – Aknot Dec 09 '20 at 10:45
  • Yes you can use into any CMS page. just add {{block class="VendoreName\ModuleName\Block\AllReviews" template="VendoreName_ModuleName::allreviews.phtml"}} https://magento.stackexchange.com/a/200387/82670 – Msquare Dec 09 '20 at 11:10