I'm trying to show the Top 4 Selling Product(s) with order by qty_ordered DESC in frontend like Admin Dashboard best seller's short view. But it will not displayed data in the exact way like Admin Dashboard. My code is given below:
<?php
namespace Vendorname\Modulename\Block\Product;
use Magento\Catalog\Api\CategoryRepositoryInterface;
use Magento\Catalog\Block\Product\AbstractProduct;
use Magento\Catalog\Block\Product\Widget\Html\Pager;
use Magento\Catalog\Model\Product;
use Magento\Catalog\Model\Product\Visibility;
use Magento\Catalog\Model\ResourceModel\Product\Collection;
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
use Magento\Sales\Model\ResourceModel\Report\Bestsellers\CollectionFactory as BestSellersCollectionFactory;
use Magento\Catalog\Pricing\Price\FinalPrice;
use Magento\CatalogWidget\Model\Rule;
use Magento\Framework\App\ActionInterface;
use Magento\Framework\App\Http\Context;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\DataObject\IdentityInterface;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Pricing\PriceCurrencyInterface;
use Magento\Framework\Serialize\Serializer\Json;
use Magento\Framework\Url\EncoderInterface;
use Magento\Framework\View\LayoutFactory;
use Magento\Framework\View\LayoutInterface;
use Magento\Rule\Model\Condition\Combine;
use Magento\Rule\Model\Condition\Sql\Builder;
use Magento\Widget\Block\BlockInterface;
use Magento\Widget\Helper\Conditions;
/**
* Catalog Products List widget block
*
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
class ProductsList extends \Magento\CatalogWidget\Block\Product\ProductsList //implements \Magento\Widget\Block\BlockInterface
{
/**
* Instance of pager block
*
* @var Pager
*/
protected $pager;
/**
* @var Context
*/
protected $httpContext;
/**
* Catalog product visibility
*
* @var Visibility
*/
protected $catalogProductVisibility;
/**
* Product collection factory
*
* @var CollectionFactory
*/
protected $productCollectionFactory;
protected $_bestSellersCollectionFactory;
/**
* @var Builder
*/
protected $sqlBuilder;
/**
* @var Rule
*/
protected $rule;
/**
* @var Conditions
*/
protected $conditionsHelper;
/**
* @var PriceCurrencyInterface
*/
private $priceCurrency;
/**
* Json Serializer Instance
*
* @var Json
*/
private $json;
/**
* @var LayoutFactory
*/
private $layoutFactory;
/**
* @var EncoderInterface|null
*/
private $urlEncoder;
/**
* @var \Magento\Framework\View\Element\RendererList
*/
private $rendererListBlock;
/**
* @var CategoryRepositoryInterface
*/
private $categoryRepository;
/**
* @param \Magento\Catalog\Block\Product\Context $context
* @param CollectionFactory $productCollectionFactory
* @param Visibility $catalogProductVisibility
* @param Context $httpContext
* @param Builder $sqlBuilder
* @param Rule $rule
* @param Conditions $conditionsHelper
* @param CategoryRepositoryInterface $categoryRepository
* @param array $data
* @param Json|null $json
* @param LayoutFactory|null $layoutFactory
* @param EncoderInterface|null $urlEncoder
*
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function __construct(
\Magento\Catalog\Block\Product\Context $context,
CollectionFactory $productCollectionFactory,
Visibility $catalogProductVisibility,
Context $httpContext,
Builder $sqlBuilder,
Rule $rule,
Conditions $conditionsHelper,
CategoryRepositoryInterface $categoryRepository,
BestSellersCollectionFactory $bestSellersCollectionFactory,
array $data = [],
Json $json = null,
LayoutFactory $layoutFactory = null,
EncoderInterface $urlEncoder = null
) {
$this->productCollectionFactory = $productCollectionFactory;
$this->catalogProductVisibility = $catalogProductVisibility;
$this->httpContext = $httpContext;
$this->sqlBuilder = $sqlBuilder;
$this->rule = $rule;
$this->conditionsHelper = $conditionsHelper;
$this->json = $json ?: ObjectManager::getInstance()->get(Json::class);
$this->layoutFactory = $layoutFactory ?: ObjectManager::getInstance()->get(LayoutFactory::class);
$this->urlEncoder = $urlEncoder ?: ObjectManager::getInstance()->get(EncoderInterface::class);
$this->categoryRepository = $categoryRepository;
$this->_bestSellersCollectionFactory = $bestSellersCollectionFactory;
parent::__construct(
$context,
$productCollectionFactory,
$catalogProductVisibility,
$httpContext,
$sqlBuilder,
$rule,
$conditionsHelper,
$categoryRepository,
$data
);
}
public function getBestsellerProductCollection()
{
//$productIds = [];
$bestSellers = $this->_bestSellersCollectionFactory->create()
->setModel('Magento\Catalog\Model\Product')
//->addStoreFilter($this->getStoreId())
//->setPeriod('month');
->setPeriod('year');
$bestSellers->setPageSize(4);
$bestSellers->addStoreFilter(1)->getSelect()->order('qty_ordered DESC');
//echo $bestSellers->getSelect()->__toString();
foreach ($bestSellers as $product) {
$prodIds[] = $product->getProductId();
}
echo "<pre>"; print_r($prodIds); exit();
//$productIds = array_slice(array_unique($prodIds), 0, 4);
// //echo "<pre>"; print_r(array_unique($productIds)); exit();
// echo "<pre>"; print_r($productIds); exit();
$collection = $this->productCollectionFactory->create()->addIdFilter($productIds);
$collection->addMinimalPrice()
->addFinalPrice()
->addTaxPercents()
->addAttributeToSelect($this->_catalogConfig->getProductAttributes())
->addStoreFilter($this->getStoreId())
->addUrlRewrite()
->setVisibility($this->catalogProductVisibility->getVisibleInSiteIds());
//->setPageSize($this->getProductsCount());
return $collection;
}
}
I cann't understand where I'm wrong to show the Top 4 Seller Products in frontend. If any one knows where there is issue, please give a proper solution(s) or clue(s).