<?php
namespace Custom\Cmenu\Block;
use Magento\Catalog\Api\CategoryRepositoryInterface;
class Category extends \Magento\Framework\View\Element\Template
{
protected $_productRepository;
protected $_reviewFactory;
protected $_ratingCollection;
public function __construct(
\Magento\Catalog\Model\ProductRepository $productRepository,
\Magento\Review\Model\ReviewFactory $reviewFactory,
\Magento\Review\Model\ResourceModel\Review\Summary\Collection $ratingCollection,
\Magento\Framework\View\Element\Template\Context $context,
array $data = []
) {
$this->_productRepository = $productRepository;
$this->_reviewFactory = $reviewFactory;
$this->_ratingCollection = $ratingCollection;
parent::__construct($context);
}
public function getRatingSummary($product)
{
//$product = $this->_productloader->create()->load($id); // follow the link for this
$this->_reviewFactory->create()->getEntitySummary($product, 1);
$ratingSummary = $product->getRatingSummary()->getRatingSummary();
return $ratingSummary;
}
}
----------------- PHTML page --------------------------------------
<?php
$category = $block->getLayout()->createBlock('Custom\Cmenu\Block\Category'); ?>
<?php
//pass $product object as argument...
$_ratingSummary = $block->getRatingSummary($product);
?>
<?php if($_ratingSummary){ ?>
<div class="product-reviews-summary short">
<div class="rating-summary">
<div title="<?php echo (int)$_ratingSummary; ?>%" class="rating-result">
<span style="width:<?php echo (int)$_ratingSummary; ?>%"><span><?php echo (int)$_ratingSummary; ?>%</span></span>
</div>
</div>
</div>
<?php } ?>
namespace Custom\Cmenu\Block;
use Magento\Catalog\Api\CategoryRepositoryInterface;
class Category extends \Magento\Framework\View\Element\Template
{
protected $_productRepository;
protected $_reviewFactory;
protected $_ratingCollection;
public function __construct(
\Magento\Catalog\Model\ProductRepository $productRepository,
\Magento\Review\Model\ReviewFactory $reviewFactory,
\Magento\Review\Model\ResourceModel\Review\Summary\Collection $ratingCollection,
\Magento\Framework\View\Element\Template\Context $context,
array $data = []
) {
$this->_productRepository = $productRepository;
$this->_reviewFactory = $reviewFactory;
$this->_ratingCollection = $ratingCollection;
parent::__construct($context);
}
public function getRatingSummary($product)
{
//$product = $this->_productloader->create()->load($id); // follow the link for this
$this->_reviewFactory->create()->getEntitySummary($product, 1);
$ratingSummary = $product->getRatingSummary()->getRatingSummary();
return $ratingSummary;
}
}
----------------- PHTML page --------------------------------------
<?php
$category = $block->getLayout()->createBlock('Custom\Cmenu\Block\Category'); ?>
<?php
//pass $product object as argument...
$_ratingSummary = $block->getRatingSummary($product);
?>
<?php if($_ratingSummary){ ?>
<div class="product-reviews-summary short">
<div class="rating-summary">
<div title="<?php echo (int)$_ratingSummary; ?>%" class="rating-result">
<span style="width:<?php echo (int)$_ratingSummary; ?>%"><span><?php echo (int)$_ratingSummary; ?>%</span></span>
</div>
</div>
</div>
<?php } ?>
No comments:
Post a Comment