1

i want to get order item from order collection on weekly basis and i wrote blow code . i just want to confirm that its right way or we can do better than this way please advice

public function execute()
    {

        $filepath = 'export/customerlist.csv';
        $this->directory->create('export');

       

        $stream = $this->directory->openFile($filepath, 'w+');
        $stream->lock();
        $header = ['Sku', 'Name', 'Price','Qty','Date'];
        $stream->writeCsv($header);



        $orders = $this->getOrderCollectionByDateRange();
            if($orders->getTotalCount() > 0) { 
                foreach($orders as $_order) {
                    $order = $this->orderRepository->get($_order->getEntityId());
                    foreach ($order->getAllVisibleItems() as $_item) {
                        $data = [];
                        $data[] = $_item->getSku();
                        $data[] = $_item->getName();
                        $data[] = $_item->getPrice();
                        $data[] = $_item->getQtyOrdered();
                        $data[] = $order->getCreatedAt();
                        $stream->writeCsv($data);
                       
                    }
                   
                }
            }
    }


    public function getOrderCollectionByDateRange(){

        $currentDate = $this->date->date('Y-m-d');
        $previousDate = $this->date->date('Y-m-d', strtotime($currentDate."-2 days"));
        $startDate = date("Y-m-d",strtotime($previousDate)); // start date
        $endDate = date("Y-m-d ", strtotime($currentDate)); // end date
        $orders = $this->orderCollectionFactory->create()
        ->addAttributeToFilter('created_at', array('from'=>$startDate, 'to'=>$endDate));
        echo $orders->getSelect();

        return $orders;
        }

1 Answer 1

0

I confirm your way is the right way to filter Order collection by date range.

Your code could be optimized by removing redundant code:

  • check Order Total count because if there are no orders, the code inside foreach won't reach.
  • reload order by orderRepository because order items is instance of Magento\Sales\Model\Order and it support call getAllVisibleItems method as you need.

So, replace your code:

$orders = $this->getOrderCollectionByDateRange();
            if($orders->getTotalCount() > 0) { 
                foreach($orders as $_order) {
                    $order = $this->orderRepository->get($_order->getEntityId());
                    foreach ($order->getAllVisibleItems() as $_item) {
                        $data = [];
                        $data[] = $_item->getSku();
                        $data[] = $_item->getName();
                        $data[] = $_item->getPrice();
                        $data[] = $_item->getQtyOrdered();
                        $data[] = $order->getCreatedAt();
                        $stream->writeCsv($data);
                       
                    }
                   
                }
            }

To

$orders = $this->getOrderCollectionByDateRange();
foreach ($orders as $order) {
    foreach ($order->getAllVisibleItems() as $item) {
        $data = [];
        $data[] = $item->getSku();
        $data[] = $item->getName();
        $data[] = $item->getPrice();
        $data[] = $item->getQtyOrdered();
        $data[] = $order->getCreatedAt();
        $stream->writeCsv($data);
    }
}
4
  • if($orders->getTotalCount() > 0) why i remove this?
    – imt
    Oct 10, 2022 at 12:32
  • ok understand great help thanks
    – imt
    Oct 10, 2022 at 12:34
  • you're welcome.
    – Tu Van
    Oct 10, 2022 at 12:41
  • i need some more modification in this if same product order by 2 customers then its show in single row i mean qty and price multiply.
    – imt
    Oct 11, 2022 at 7:41

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.