Magento 2:主页上评价最高的产品

5 magento2

如何在主页 magento 2 中获得最受好评的产品?

我们必须在 Magento 2 的主页中显示评分最高的产品列表。

小智 0

public function getRatingSummary()
{

    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $reviewFactory = $objectManager->get(\Magento\Review\Model\ReviewFactory::class);
    $storeManager = $objectManager->get(\Magento\Store\Model\StoreManagerInterface::class);
    $productCollection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\CollectionFactory');
    $collection = $productCollection->create()
        ->addAttributeToSelect('*')
        ->load();
    $rating = array();
    foreach ($collection as $product) {
        $reviewFactory->create()->getEntitySummary($product, $this->_storeManager->getStore()->getId());
        $ratingSummary = $product->getRatingSummary()->getRatingSummary();
        if($ratingSummary!=null){
            $rating[$product->getId()] = $ratingSummary;
        }
    }
    return $rating;
}
Run Code Online (Sandbox Code Playgroud)

在您的自定义块中使用此代码来获取评级产品的 ID,并使用此块调用一个模板文件来显示产品数据