I have removed reviews tab and placed the form in a popup modal.
Now I have displayed all the reviews posted by the customer about the particular product and I have displayed the Rating summary for the Product.
Now I need to get Rating for each customer and display in the following format for each reviews in frontend. Please Provide me a solution to add star rating for each reviews.
This is my collection class I have displayed in frontend like below
For reference
Block file
<?php
namespace XXX\YYY\Block;
use Magento\Framework\View\Element\Template;
class Collection extends Template
{
protected $_coreRegistry;
protected $_reviewsColFactory;
protected $_reviewFactory;
protected $_voteFactory;
public function __construct(\Magento\Framework\View\Element\Template\Context $context,\Magento\Review\Model\Rating\Option\VoteFactory $voteFactory, \Magento\Review\Model\ReviewFactory $reviewFactory, \Magento\Framework\Registry $registry, \Magento\Review\Model\ResourceModel\Review\CollectionFactory $collectionFactory, array $data = [])
{
$this->_coreRegistry = $registry;
$this->_reviewsColFactory = $collectionFactory;
$this->_reviewFactory = $reviewFactory;
$this->_voteFactory = $voteFactory;
parent::__construct($context, $data);
}
public function getProductId()
{
$product = $this->_coreRegistry->registry('product');
return $product ? $product->getId() : null;
}
public function getCollectionSize()
{
$collection = $this->_reviewsColFactory->create()->addStoreFilter(
$this->_storeManager->getStore()->getId()
)->addStatusFilter(
\Magento\Review\Model\Review::STATUS_APPROVED
)->addEntityFilter(
'product', $this->getProductId()
);
return $collection->getSize();
}
public function getAllreviews()
{
$collection = $this->_reviewsColFactory->create()->addStoreFilter(
$this->_storeManager->getStore()->getId()
)->addStatusFilter(
\Magento\Review\Model\Review::STATUS_APPROVED
)->addEntityFilter(
'product', $this->getProductId()
);
return $collection;
}
public function getRatingSummary()
{
$product = $this->_coreRegistry->registry('product');
$this->_reviewFactory->create()->getEntitySummary($product, $this->_storeManager->getStore()->getId());
$ratingSummary = $product->getRatingSummary()->getRatingSummary();
return $ratingSummary;
}
}
abc.phtml
<div id="customer-reviews" class="customer-reviews">
<?php $reviews = $block->getAllreviews(); ?>
<span><b><?= /* @escapeNotVerified */ __('Rating summary : ') ?></b></span><?php echo $rating = $block->getRatingSummary(); ?><br>
<div id ="reviews" class="reviews"> <span><b><?= /* @escapeNotVerified */ __('Customer Reviews') ?></b></span><br>
<?php foreach ($reviews as $review): ?>
<span><?= /* @escapeNotVerified */ __('Customer Name : ') ?></span><?php echo $review->getNickname(); ?><br>
<span><?= /* @escapeNotVerified */ __('Review : ') ?></span><?php echo $review->getDetail(); ?><br>
<span><?= /* @escapeNotVerified */ __('Date : ') ?></span><?php echo $review->getCreatedAt(); ?><br>
<?php endforeach; ?>
</div>
</div>