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 ?

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

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.

  • I tried that and that works fine. But I need records month wise – Narendra Dec 28 '18 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!

  • 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 – Narendra Dec 28 '18 at 9:02
  • I think I need to change this ORDER BY period ASC to DSC. You have any idea how to do that ? – Narendra Dec 28 '18 at 9:03
  • In the place of sort_order you need to pass product_id – Chirag Patel Dec 28 '18 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') – Chirag Patel Dec 28 '18 at 9:05
  • G etting same error for this as well. column not found – Narendra Dec 28 '18 at 9:10

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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