如何在主页 magento 2 中获取 bestseller和大多数观看的产品

我们必须在Magento 2的主页滑块中显示 Bestseller和大多数已浏览产品列表。

有帮助吗?

解决方案

for bestseller在__construct中创建一个块GET

的实例
\Magento\Sales\Model\ResourceModel\Report\Bestsellers\CollectionFactory $collectionFactory,
.

ex

<?php
namespace Sugarcode\Test\Block;

class Test extends \Magento\Framework\View\Element\Template
{
    protected $_coreRegistry = null;
    protected $_collectionFactory;

    public function __construct(
        \Magento\Backend\Block\Template\Context $context,
        \Magento\Framework\Registry $registry,
       \Magento\Sales\Model\ResourceModel\Report\Bestsellers\CollectionFactory $collectionFactory,
        array $data = []
    ) {
        $this->_collectionFactory = $collectionFactory;
        $this->_coreRegistry = $registry;
        parent::__construct($context, $data);
    }



    public function _prepareLayout()
    {
        return parent::_prepareLayout();
    }
    public function getBestSellerData()
    {
        $collection = $this->_collectionFactory->create()->setModel(
            'Magento\Catalog\Model\Product'
        );

        return $collection;
    }       

}
.

对于最近浏览,您可以使用来自admin侧的小部件,否则您可以编写自定义块 具有\Magento\Reports\Model\ResourceModel\Product\CollectionFactory $productsFactory

查看:

vendor\magento\module-backend\Block\Dashboard\Tab\Products\Viewed.php

and

vendor\magento\module-backend\Block\Dashboard\Tab\Products\Ordered.php 
.

其他提示

使用以下代码查看Magento 2滑块中的最佳卖家以及大多数已知产品。

<?php $objectManager = \Magento\Framework\App\ObjectManager::getInstance();   
$productCollection = $objectManager->create('Magento\Reports\Model\ResourceModel\Report\Collection\Factory'); 
$collection = $productCollection->create('Magento\Sales\Model\ResourceModel\Report\Bestsellers\Collection'); ?>
.

许可以下: CC-BY-SA归因
scroll top