Magento 2 Get Review, Rating Collection programatically

How to get review, rating collection

Directory

VendorName/ModuleName/Model/ProductReviews.php

Content for this file

<?php

namespace VendorName\ModuleName\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();
}
}

If you are looking to hire a Magento programmer, visit Magento Development Agency.