如何在 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/