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] = '★ ☆ ☆ ☆ ☆ ';
} elseif ($finalValue == 40) {
$finalRatingData[$final_rateKey][$FinalKey] = '★ ★ ☆ ☆ ☆ ';
} elseif ($finalValue == 60) {
$finalRatingData[$final_rateKey][$FinalKey] = '★ ★ ★ ☆ ☆ ';
} elseif ($finalValue == 80) {
$finalRatingData[$final_rateKey][$FinalKey] = '★ ★ ★ ★ ☆ ';
} elseif ($finalValue == 100) {
$finalRatingData[$final_rateKey][$FinalKey] = '★ ★ ★ ★ ★ ';
} else {
$finalRatingData[$final_rateKey][$FinalKey] = '☆ ☆ ☆ ☆ ☆ ';
}
}
}
}
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?