1

I need to get bestsellers products for the current month. I use Magento\Sales\Model\ResourceModel\Report\Bestsellers\Collection, but I get bestsellers for all months, I used the following code to get it for current month

$collection = $this->productCollection->create('Magento\Sales\Model\ResourceModel\Report\Bestsellers\Collection'); 
$data=array();
    $collection->setPeriod('monthly');
    //$collection->setPeriod('year');


    $currentMonth = date('Y-m');
    foreach ($collection as $item) {
        if($item['period'] == $currentMonth){
            array_push($data,$item->getData());
        }

    }

but that code will take more time for every coming month.

Is there a way to get products of current month directly from database?

Your Answer

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

Browse other questions tagged or ask your own question.