【问题标题】:Top rated products magento 2最受好评的产品 magento 2
【发布时间】:2016-08-16 06:11:35
【问题描述】:

我想在 magento 2 中显示评分最高的产品

我的区块Toprated.php

   public function getTesting()
{

    $collection = $this->_productCollectionFactory->create();

    foreach($collection as $eachColl)
    {
        $storeId = $eachColl->getStore()->getId();
        $reviewSum = $this->reviewSummaryFactory->create()->setStoreId($storeId)->load($eachColl->getId());

        $rated[] = array(
            'rating' => $reviewSum['rating_summary'],
            'name' => $eachColl->getName(),
            'url' => $eachColl->getUrlPath(),
            'product_sku' => $eachColl->getSku()
        );
        $rateds[$eachColl->getSku()] = $reviewSum['rating_summary'];

    }
  arsort($rateds);
    $rateds = array_slice($rateds, 0, 3);
    $collection = $this->_productCollectionFactory->create();
    $collection->addAttributeToFilter('status', '1');
    $collection->addAttributeToFilter('rating', array('in' => implode(",", $rateds)));

    return $collection;

}

我的模板 toprated.phtml 文件

<?php
$_productCollection = $this->getTesting();

$_helper = $this->helper('Magento\Catalog\Helper\Output');
$_objectManager = \Magento\Framework\App\ObjectManager::getInstance();

?>

但是 phtml 文件没有调用任何数据。我在这里做错了什么

【问题讨论】:

    标签: magento2


    【解决方案1】:

    您想在哪里显示新块?

    可以通过在视图/前端/布局/中编辑/添加布局文件来实现

    <?xml version="1.0" encoding="UTF-8"?>
    <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">      
    <referenceContainer name="content">
        <block
            template="[your template]"
            class="[your class]"
            name="[your name]"/>
    </referenceContainer>
    

    【讨论】:

      【解决方案2】:

      在视图中创建布局文件 -> 前端 -> 模板 -> 布局, 文件名应该是你的 routname_controllernamespace_controllername (controller->Index->​​index.php) 前任。 blog_index_index 文件应该是这样的

      <?xml version="1.0"?> <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd"> <body> <referenceContainer name="content"> <block class="Demo\Blog\Block\PostList" name="demo.list" template="Demo_Blog::list.phtml" /> </referenceContainer> </body> </page>

      【讨论】:

        猜你喜欢
        • 2016-12-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-04-29
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多