0

My site contain only configurable products and I want to display bestseller products. I know there are many extension available for bestseller products but i want to display only configurable products.

Can anyone help me solve this.

Thanks In Advance.

Hassan Ali Shahzad
  • 2,330
  • 18
  • 35
Sanjay Gohil
  • 2,200
  • 1
  • 14
  • 29

4 Answers4

2

Its better to use productRepository:- inject BestSellerProducts class in your class via DI

  $bestSellerProducts->getBestSellingProducts();

your custom class can be as:-

<?php
namespace Vendor\ModlueName\Model;

use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Catalog\Api\ProductRepositoryInterface;

class BestSellerProducts
{
    /** @var ProductRepositoryInterface */
    protected $productRepository;

    /** @var SearchCriteriaBuilder */
    protected $searchCriteriaBuilder;

    /**
     * Initialize dependencies.
     *
     * @param ProductRepositoryInterface $productRepository
     * @param SearchCriteriaBuilder $searchCriteriaBuilder
     */
    public function __construct(
        ProductRepositoryInterface $productRepository,
        SearchCriteriaBuilder $searchCriteriaBuilder
    ) {
        $this->productRepository = $productRepository;
        $this->searchCriteriaBuilder = $searchCriteriaBuilder;
    }

    /**
     * Get products with filter.
     * 
     * @return \Magento\Catalog\Api\Data\ProductInterface[]
     */
    public function getBestSellingProducts()
    {
        $searchCriteria = $this->searchCriteriaBuilder->addFilter('type_id', 'configurable', 'eq')->create();
        $products = $this->productRepository->getList($searchCriteria);
        return $products->getItems();
    }
}
Hassan Ali Shahzad
  • 2,330
  • 18
  • 35
1

Try with below way.

Step 1 : Create block file.

<?php
namespace Namespace\Modulename\Block;

class BestSeller extends \Magento\Framework\View\Element\Template
{

    protected $_collectionFactory;
    public function __construct(
       \Magento\Backend\Block\Template\Context $context,
      \Magento\Sales\Model\ResourceModel\Report\Bestsellers\CollectionFactory $collectionFactory,
               array $data = []
    ) {

       $this->_collectionFactory = $collectionFactory;
             parent::__construct($context, $data);
    }


public function getBestSellerData(){

    $bestSellerProdcutCollection = $this->_collectionFactory->create()
                        ->setModel('Magento\Catalog\Model\Product')
                        ->setPeriod('month');

    $bsProducts = $bestSellerProdcutCollection->addAttributeToSelect('*')
            ->addAttributeToFilter('type_id', array('eq' => 'configurable'))                
            ->load();

           return $bsProducts;
       }
}

Step 2 : Create phtml file to display collection.

<?php
    $bestSeller =  $block->getBestSellerData(); ?>
    <h1>Best Seller Collection.....</h1>
    <ul>
        <?php foreach ($bestSeller as $product) {
            ?>
            <li><?php  echo $product->getProductName();?>--<?php  echo $product->getQtyOrdered();?></li>
        <?php } ?>
    </ul>

Note : Use Factory methode insted of object manager.

I hope it works!

Chirag Patel
  • 6,126
  • 2
  • 23
  • 65
0

try this way..

using block class file

<?php
namespace Vendor\Module\Block;

class BestSeller extends \Magento\Framework\View\Element\Template
{

    protected $_collectionFactory;
    public function __construct(
       \Magento\Backend\Block\Template\Context $context,
      \Magento\Sales\Model\ResourceModel\Report\Bestsellers\CollectionFactory $collectionFactory,
               array $data = []
    ) {

       $this->_collectionFactory = $collectionFactory;
             parent::__construct($context, $data);
    }


public function getBestSellerData(){

    $bestsellers = $this->_collectionFactory->create()
                        ->setModel('Magento\Catalog\Model\Product')
                        ->setPeriod('month');

    $collection = $bestsellers->addAttributeToSelect('*')
            ->addAttributeToFilter('type_id', array('eq' => 'configurable'))                
            ->load();

           return $collection;
       }
}

or using object manager

<?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'); 

    $collection->setPeriod('month');

    $products = $collection->addAttributeToSelect('*')
                ->addAttributeToFilter('type_id', array('eq' => 'configurable'))
                ->setPageSize(10)
                ->load();


                foreach ($products as $product) {
                    print_r($product->getData());
                }

Don't use Object Manager instance directly check this for more details

Rakesh Donga
  • 5,344
  • 2
  • 24
  • 57
0

Please don't use objectmanager, I have posted this answer for your understanding. When you implement use block file and proper dependency.

<?php 
   $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
   $bestSellerData = $objectManager->create('Magento\Sales\Model\ResourceModel\Report\Bestsellers\Collection'); 
     $parent_arr = Array(); 
     foreach ($bestSellerData as $item){           
       $parentIds = $objectManager->get('Magento\ConfigurableProduct\Model\Product\Type\Configurable')
                        ->getParentIdsByChild($item->getProductId());

         if(isset($parentIds[0])){                         
         $productIds =  $parentIds[0]; //Configurable product ids here 
           if (!in_array($productIds, $parent_arr)) { // Avoid Repetition Of Configurable product id    
               $parent_arr[] = $productIds;                       
               $_product = $objectManager->get('\Magento\Catalog\Model\ProductRepository')->getById($productIds); 

                echo $_product->getId();   
                echo $_product->getName();  
         }    

         ?>
Sanjay Gohil
  • 2,200
  • 1
  • 14
  • 29