0

I am fetching Bestsellers like this:

protected $_collectionFactory;

\Magento\Sales\Model\ResourceModel\Report\Bestsellers\CollectionFactory $collectionFactory,

$this->_collectionFactory = $collectionFactory;

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

Issue is this that this is not giving me latest month entry first. I mean if in collection I have records from 2012 to 2018, I am getting first records of 2012. And as I need only 8 records so every time getting the same records.

Not getting how I can sort and get 2018 records first ?

3
  • Can you check the answer? Its working for me. Commented Dec 28, 2018 at 9:05
  • Hi Narendra, I am updating chirag's answer you should try ->setOrder('period','DESC') like this. Commented Dec 28, 2018 at 9:12
  • @RamkishanSuthar: using so gives no error but records are not sorted Commented Dec 28, 2018 at 9:15

2 Answers 2

1

Try the following code:

protected $_collectionFactory;

\Magento\Sales\Model\ResourceModel\Report\Bestsellers\CollectionFactory $collectionFactory,

$this->_collectionFactory = $collectionFactory;

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

Flush the cache and test.

1
  • I tried that and that works fine. But I need records month wise Commented Dec 28, 2018 at 9:07
-1

For that you need to get collection by descending order so you need to use ->setOrder('sort_order','DESC') function.

Try with below code.

protected $_collectionFactory;

\Magento\Sales\Model\ResourceModel\Report\Bestsellers\CollectionFactory 
$collectionFactory,

$this->_collectionFactory = $collectionFactory;

$bestSellerProdcutCollection = $this->_collectionFactory->create()
    ->setModel('Magento\Catalog\Model\Product')
    ->setPageSize(8)
    ->setPeriod('month')
    ->setOrder('sort_order','DESC');

I hope it helps!

13
  • I am getting this error: Exception #0 (Zend_Db_Statement_Exception): SQLSTATE[42S22]: Column not found: 1054 Unknown column 'sort_order' in 'order clause', query was: SELECT DATE_FORMAT(period, '%Y-%m') AS period, SUM(qty_ordered) AS qty_ordered, sales_bestsellers_aggregated_monthly.product_id, MAX(product_name) AS product_name, MAX(product_price) AS product_price FROM sales_bestsellers_aggregated_monthly WHERE (rating_pos <= 5) AND (store_id IN(0)) AND (store_id IN(0)) GROUP BY period, product_id ORDER BY period ASC, qty_ordered DESC, sort_order DESC LIMIT 8 Commented Dec 28, 2018 at 9:02
  • I think I need to change this ORDER BY period ASC to DSC. You have any idea how to do that ? Commented Dec 28, 2018 at 9:03
  • In the place of sort_order you need to pass product_id Commented Dec 28, 2018 at 9:03
  • 1
    For Ex:- if you wan to get last updates product so you need to pass like this ->setOrder('updated_at','DESC') Commented Dec 28, 2018 at 9:05
  • G etting same error for this as well. column not found Commented Dec 28, 2018 at 9:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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