3

I need to get product rating collection in block, and need to display it in template.Please provide me a solution

2
1

You can get Rating collection in magento as 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();
   echo "<pre>";print_r($ratingCollection->getData());
    return $ratingCollection->getData();
}

Above $ratingCollection->getData() function output will be like below,

Array
(
    [0] => Array
        (
            [rating_id] => 4
            [entity_id] => 1
            [rating_code] => Rating
            [position] => 0
            [is_active] => 1
            [entity_code] => product
        )

)
4
  • How do I call only rating code and its value from this collection to my phtml code
    – Jaisa
    Dec 20 '17 at 11:13
  • you can call this function in your phtml i have added debug code you can check it. Dec 20 '17 at 11:17
  • If I have 3 ratings means, whether it can fetch that 3 rating codes
    – Jaisa
    Dec 20 '17 at 12:05
  • Yes 3 star means 3 ratings. Dec 20 '17 at 12:06

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.