magento2 - Magento 2 : Top rated products on homepage

标签 magento2

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

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

最佳答案

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;
}

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

关于magento2 - Magento 2 : Top rated products on homepage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38869033/

相关文章:

javascript - 将数组传递给 requirejs shim

Magento 2 : Custom Module not working

php - Magento 产品导入

php - Magento 2 如何覆盖抽象类中的 protected 函数?

Magento2-具有分层导航的自定义产品集合

Magento 2 自定义模块(管理员)微调器永远不会消失

php - Magento 2 : Add custom script just after head tag

php - Magento 2 - 如何在 header.phtml 中获取购物车项目总数

Magento 2 错误消息 'This is a required field'

magento - 将客户重定向到客户登录网站 Magento 2 的产品页面