在网上商店中,每种产品都会有不同的销售数量。有必要了解商店中所有产品的销售情况,看看哪种产品卖得最好,哪种产品卖得不好。

当谈到畅销产品时,它通常被用作对客户,尤其是初次购买的客户的良好推荐。在商店中展示畅销产品以在购物旺季获得更多销售额也是一个明智的策略。

要显示畅销产品,您将需要有关畅销产品集合的数据。因此,在今天的帖子中,我将为您提供三个阶段来帮助您获得 Magento 2 的畅销书收藏

如何通过三步获得畅销书系列

  • 第 1 步:创建 BestSellerProducts 块
  • 第 2 步:插入phtml文件
  • 第 3 步:刷新缓存和测试结果

第 1 步:创建 BestSellerProducts 块

要获得畅销书系列,您要做的第一件事就是创建 BestSellerProducts 块。沿着这条路走Example/Productslider/Block/BestSellerProducts.php

<?php

namespace Example\Productslider\Block;
use Magento\Catalog\Block\Product\Context;
use Magento\Catalog\Model\Product\Visibility;
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
use Magento\Framework\App\Http\Context as HttpContext;
use Magento\Framework\Stdlib\DateTime\DateTime;
use Magento\Sales\Model\ResourceModel\Report\Bestsellers\CollectionFactory as BestSellersCollectionFactory;
use Example\Productslider\Helper\Data;
/**
 * Class BestSellerProducts
 * @package Example\Productslider\Block
 */
class BestSellerProducts extends AbstractSlider
{
    /**
     * @var BestSellersCollectionFactory
     */
    protected $_bestSellersCollectionFactory;
    /**
     * BestSellerProducts constructor.
     * @param Context $context
     * @param CollectionFactory $productCollectionFactory
     * @param Visibility $catalogProductVisibility
     * @param DateTime $dateTime
     * @param Data $helperData
     * @param HttpContext $httpContext
     * @param BestSellersCollectionFactory $bestSellersCollectionFactory
     * @param array $data
     */
    public function __construct(
        Context $context,
        CollectionFactory $productCollectionFactory,
        Visibility $catalogProductVisibility,
        DateTime $dateTime,
        Data $helperData,
        HttpContext $httpContext,
        BestSellersCollectionFactory $bestSellersCollectionFactory,
        array $data = []
    ) {
        $this->_bestSellersCollectionFactory = $bestSellersCollectionFactory;
        parent::__construct($context, $productCollectionFactory, $catalogProductVisibility, $dateTime, $helperData, $httpContext, $data);
    }
    /**
     * get collection of best-seller products
     * @return mixed
     */
    public function getProductCollection()
    {
        $productIds = [];
        $bestSellers = $this->_bestSellersCollectionFactory->create()
            ->setPeriod('month');
        foreach ($bestSellers as $product) {
            $productIds[] = $product->getProductId();
        }
        $collection = $this->_productCollectionFactory->create()->addIdFilter($productIds);
        $collection->addMinimalPrice()
            ->addFinalPrice()
            ->addTaxPercents()
            ->addAttributeToSelect('*')
            ->addStoreFilter($this->getStoreId())->setPageSize($this->getProductsCount());
        return $collection;
    }
}

步骤2:插入phtml文件

在块中拥有集合后,现在您可以按照上面的代码片段从块中获取产品集合Example/HelloWorld/view/frontend/templates/list.phtml

然后,在 phtml 文件中插入以下代码。

<?php
$collection = $block->getProductCollection();
foreach ($collection as $_product) {
    echo $product->getName() . ' - ' . $product->getProductUrl() . '<br />';
}

第 3 步:刷新缓存和测试结果

最后,让我们刷新缓存并测试结果。