Cookies setting

Cookies help us enhance your experience on our site by storing information about your preferences and interactions. You can customize your cookie settings by choosing which cookies to allow. Please note that disabling certain cookies might impact the functionality and features of our services, such as personalized content and suggestions. Cookie Policy

Cookie Policy
Essential cookies

These cookies are strictly necessary for the site to work and may not be disabled.

Information
Always enabled
Advertising cookies

Advertising cookies deliver ads relevant to your interests, limit ad frequency, and measure ad effectiveness.

Information
Analytics cookies

Analytics cookies collect information and report website usage statistics without personally identifying individual visitors to Google.

Information
mageplaza.com

How to Get Review, Rating Collection in Magento 2?

Vinh Jacker | 12-18-2024

How to Get Review, Rating Collection in Magento 2

As you might have already known, review and rating play an important role in every online business, especially those which run on the Magento 2 platform as it related directly to the reputation of stores as well as the purchase decisions of customers.

Therefore, in today’s post, I will provide you with the simplest way to get a review and rating collection in Magento 2.


Better Product Reviews

Better Product Reviews for Magento 2

Motivate shoppers to make faster decisions by displaying top-rated products from previous customers

Check it out!


How to get review, rating collection

To get product review, ratting collection, you need to create a ProductReviews.php file. Follow this path Mageplaza/HelloWorld/Model/ProductReviews.php and here are how you are going to do it.

<?php

namespace Mageplaza\HelloWorld\Model;
use Magento\Framework\Model\AbstractModel;
class ProductReviews extends AbstractModel{

    protected $_ratingFactory;
    protected $_productFactory;
    protected $_ratingFactory;
    protected $_reviewFactory;

    public function __construct(
            \Magento\Store\Model\StoreManagerInterface $storeManager,
            \Magento\Catalog\Model\ProductFactory $productFactory,
            \Magento\Review\Model\RatingFactory $ratingFactory,
            \Magento\Review\Model\ResourceModel\Review\CollectionFactory $reviewFactory,
        ) {
            $this->_storeManager = $storeManager;
            $this->_productFactory = $productFactory;
            $this->_ratingFactory = $ratingFactory;
            $this->_reviewFactory = $reviewFactory;
        }

    public function getReviewCollection($productId){
        $collection = $this->_reviewFactory->create()
        ->addStatusFilter(
            \Magento\Review\Model\Review::STATUS_APPROVED
        )->addEntityFilter(
            'product',
            $productId
        )->setDateOrder();

    }

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

        return $ratingCollection->getData();
    }

}

Conclusion

Above is the instruction on how to get product review and rating collection in Magento 2. I hope this post is helpful for you when getting and managing the collection.

x
    Jacker

    With over a decade of experience crafting innovative tech solutions for ecommerce businesses built on Magento, Jacker is the mastermind behind our secure and well-functioned extensions. With his expertise in building user-friendly interfaces and robust back-end systems, Mageplaza was able to deliver exceptional Magento solutions and services for over 122K+ customers around the world.



    Related Post

    Website Support
    & Maintenance Services

    Make sure your store is not only in good shape but also thriving with a professional team yet at an affordable price.

    Get Started
    mageplaza services