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.

  • Have you got solution? – Chirag Patel Apr 2 at 11:32
  • @ChiragPatel, Not yet I have got error in addAttributeToFilter() – Sanjay Gohil Apr 2 at 11:36
  • Try addFilter('type_id', 'simple') insted of addAttributeToFilter() – Chirag Patel Apr 2 at 11:42
1

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();
    }
}
0

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!

  • I will try and let you know within 10 minutes – Sanjay Gohil Apr 2 at 9:24
  • Okay, let me know if you have any query. – Chirag Patel Apr 2 at 9:25
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

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();  
         }    

         ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.