By default, Magento 2 shows the most viewed product in Magento Admin > DASHBOARD > choose tab Most Viewed Products. The _prepareCollection()
method in vendor/magento/module-backend/Block/Dashboard/Tab/Products/Viewed.php
is used for selecting products.
We can copy some code lines from _prepareCollection()
above. For example, in your block, use the following code below:
class Index extends \Magento\Framework\View\Element\Template
{
/**
* @var \Magento\Reports\Model\ResourceModel\Product\CollectionFactory
*/
protected $_productsFactory;
/**
* @param \Magento\Framework\View\Element\Template\Context $context
* @param \Magento\Reports\Model\ResourceModel\Product\CollectionFactory $productsFactory
* @param array $data
*/
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\Magento\Reports\Model\ResourceModel\Product\CollectionFactory $productsFactory,
array $data = []
) {
$this->_productsFactory = $productsFactory;
parent::__construct($context, $data);
}
/**
* Getting most viewed products
*/
public function getCollection()
{
$currentStoreId = $this->_storeManager->getStore()->getId();
$collection = $this->_productsFactory->create()
->addAttributeToSelect(
'*'
)->addViewsCount()->setStoreId(
$currentStoreId
)->addStoreFilter(
$currentStoreId
);
$items = $collection->getItems();
return $items;
}
}
The initial class \Magento\Reports\Model\ResourceModel\Product\CollectionFactory
is the most important class because this class will get our products report. The getCollection
method will return the most viewed items by current store.
In your template, you can get the items. For example:
<?php
$items = $this->getCollection();
foreach($items as $item) {
echo $item->getName() .'<br/>';
}
?>
Use below code to get Most Viewed products collection :
<?php
$storeId = Mage::app()->getStore()->getId();
$_productCollection = Mage::getResourceModel('reports/product_collection')
->addAttributeToSelect('*')
->setStoreId($storeId)
->addStoreFilter($storeId)
->addViewsCount()
//->setPageSize($totalPerPage)
;
?>
For more details, check List Popular Products
UPDATE : In admin, go to CMS > Pages
, edit Homepage.
-- In Content area, add this line : {{block type="catalog/product" template="catalog/product/most-viewed.phtml"}}
and SAVE.
-- Now create a new template file at /app/design/frontend/PACKAGE/THEME/template/catalog/product/
, and name it most-viewed.phtml
-- Add this code in most-viewed.phtml
file, (this will show max 10 products, edit $totalPerPage
variable as you require)
<?php
$totalPerPage = 10;
$storeId = Mage::app()->getStore()->getId();
$_productCollection = Mage::getResourceModel('reports/product_collection')
->addAttributeToSelect('*')
->setStoreId($storeId)
->addStoreFilter($storeId)
->addViewsCount()
->setPageSize($totalPerPage)
;
?>
<?php $_collectionSize = $_productCollection->count(); ?>
<?php if ($_collectionSize) { ?>
<h2 class="subtitle">Most Viewed Products</h2>
<ul class="products-grid">
<?php
foreach ($_productCollection as $_product):
$image_url = $this->helper('catalog/image')->init($_product, 'thumbnail')->setWatermarkImageOpacity(0)->keepFrame(false)->resize(200,200);
$alt = $this->htmlEscape($this->stripTags($this->getImageLabel($_product, 'small_image'), null, true));
?>
<li class="item">
<a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>">
<img src="<?php echo $image_url; ?>" alt="<?php echo trim($alt)? $alt: $this->htmlEscape($this->stripTags($_product->getName())); ?>" />
</a>
<div class="product-info">
<h2 class="product-name">
<a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>">
<?php echo trim($alt)? $alt: $this->htmlEscape($this->stripTags($_product->getName())); ?>
</a>
</h2>
</div>
</li>
<?php endforeach; ?>
</ul>
<?php } ?>
Best Answer
For bestseller create a block in
__construct
get instance ofex
For recently viewed you can use widget from admin side or else you can write custom block with
\Magento\Reports\Model\ResourceModel\Product\CollectionFactory $productsFactory
Look at: