2

I have modified review form as popup modal.And I have also created an empty stars rating for the product which does not contain rating yet.But I have created those stars statically. But Now I need to create those stars dynamically in product page. For that I need to get rating code and value in my phtml file. How Do I get rating code and value in my custom block file, Now Need to get only the rating collection

Block file

<?php

namespace XXX\YYY\Block;

class Render extends \Magento\Review\Block\Product\ReviewRenderer
{

    protected $_availableTemplates = [
        self::FULL_VIEW => 'review.phtml',
        self::SHORT_VIEW => 'helper/summary_short.phtml',
    ];


    protected $_reviewFactory;
    protected $_ratingFactory;
    protected $_storeManager;

    /**
     * @param \Magento\Framework\View\Element\Template\Context $context
     * @param \Magento\Review\Model\ReviewFactory $reviewFactory
     * @param array $data
     */
    public function __construct(
    \Magento\Framework\View\Element\Template\Context $context, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Review\Model\RatingFactory $ratingFactory, \Magento\Review\Model\ReviewFactory $reviewFactory, array $data = []
    )
    {
        $this->_storeManager = $storeManager;
        $this->_ratingFactory = $ratingFactory;
        $this->_reviewFactory = $reviewFactory;
        parent::__construct($context, $reviewFactory, $data);
    }

    /**
     * Get review summary html
     *
     * @param Product $product
     * @param string $templateType
     * @param bool $displayIfNoReviews
     *
     * @return string
     */
    public function getReviewsSummaryHtml(
    \Magento\Catalog\Model\Product $product, $templateType = self::DEFAULT_VIEW, $displayIfNoReviews = false
    )
    {

        if (!$product->getRatingSummary() && !$displayIfNoReviews) {
            return '';
        }
        // pick template among available

        if (empty($this->_availableTemplates[$templateType])) {

            $templateType = self::DEFAULT_VIEW;
        }

        $this->setTemplate($this->_availableTemplates[$templateType]);

        $this->setDisplayIfEmpty($displayIfNoReviews);

        if (!$product->getRatingSummary()) {
            $this->_reviewFactory->create()->getEntitySummary($product, $this->_storeManager->getStore()->getId());
        }
        $this->setProduct($product);

        return $this->toHtml();
    }
// I have written below code for rating collection but it retuns empty
    public function getRatings()
    {
echo"dhgdfh";
        return $this->_ratingFactory->create()->getResourceCollection()->addEntityFilter(
                        'product'
                )->setPositionOrder()->addRatingPerStoreName(
                        $this->_storeManager->getStore()->getId()
                )->setStoreFilter(
                        $this->_storeManager->getStore()->getId()
                )->setActiveFilter(
                        true
                )->load()->addOptionToItems();
    }

}
2

You can get rating collection from below way,

public function __construct(
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Review\Model\RatingFactory $ratingFactory,
    ) {
        $this->_ratingFactory = $ratingFactory;
        $this->_storeManager = $storeManager;
    }

public function getRatingCollection(){
    $ratingCollection = $this->_ratingFactory->create()->getResourceCollection()->addEntityFilter(
        'product' # TOFIX
    )->setPositionOrder()->setStoreFilter(
        $this->_storeManager->getStore()->getId()
    )->addRatingPerStoreName(
        $this->_storeManager->getStore()->getId()
    )->load();
    return $ratingCollection->getData();
}

call in phtml file with,

$block->getRatingCollection()
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.