Reputation:
I want to display top rated products in magento 2
My block 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;
}
my template toprated.phtml file
<?php
$_productCollection = $this->getTesting();
$_helper = $this->helper('Magento\Catalog\Helper\Output');
$_objectManager = \Magento\Framework\App\ObjectManager::getInstance();
?>
but phtml file not calling any data.what i did mistake here
Upvotes: 0
Views: 586
Reputation: 1
create layout file in view -> frontend -> templates -> layout , file name should be your routname_controllernamespace_controllername (controller->Index->index.php) ex. blog_index_index file should be look like this
<?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>
Upvotes: 0
Reputation: 354
Where do you want to show the new block ?
It is possible by edit/add a layout file in view/frontend/layout/
<?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>
Upvotes: 1