Magento-2.1 Review – Display All Reviews on a Single CMS Page

magento-2.1review

I've searched for the answer to this, but I'm only seeing answers to Magento 1. I need to display all of the approved Reviews on a single page. Preferably a CMS page. How do I go about this?

The best code I've seen for this is:

<?php 
$review_collection = Mage::getModel('review/review')
->getResourceCollection()
->addStoreFilter(Mage::app()->getStore()->getId())
->addStatusFilter(Mage_Review_Model_Review::STATUS_APPROVED)
->setDateOrder()
->addRateVotes();
 ?>

<?php foreach($review_collection as $review): ?>

<div class='review-single col-md-12'>
<div class="box-review-single">
<p id="title-review">
    <?php echo $review['title'];  ?>
</p>
<p id="detail-review">
    <?php echo $review['detail'];  ?>
</p>
<p id="nick-review">
    <?php echo $review['nickname'];  ?>
</p>
</div>
</div>

From this Magento forum page: Display all reviews on one page

But it is for Magento 1.9 and I'm not sure it will work in Magento 2.1.7 which is what I am using. Further more, I am not sure what pieces of the code go where (ie: PHP into a phtml or PHP file – but what directory do I place it in, Do I place any code in the XML section of the CMS page, etc)

Any help would be greatly appreciated.

Thanks,
A

Best Answer

Try this method for Magento 2

<?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">
                <p id="title-review">
                    <?php echo $review['title'];  ?>
                </p>
                <p id="detail-review">
                    <?php echo $review['detail'];  ?>
                </p>
                <p id="nick-review">
                    <?php echo $review['nickname'];  ?>
                </p>
            </div>
        </div>
    <?php endforeach; ?>
Related Topic