1

I am getting orders for last week 7 days order and I am getting all product which was placed last week. Below is my code:

foreach ($orders as $order) {
    foreach ($order->getAllVisibleItems() as $item) {
        $data['sku'] = $item->getSku();
        $data['name'] = $item->getName();
        $data['price'] = $item->getPrice();
        $data['qty'] = $item->getQtyOrdered();
        $data['created_at'] = $order->getCreatedAt();
        $stream->writeCsv($data);

    }
}

and its generate csv perfectly I need some more complex things in my code.

Currently, if the same sku product is placed by 2 different customers its shows separate rows but I want if the same sku is placed more than 1 time it shows in the same row and qty multiplied and price added. And one more thing, if product A price today is 36 and its changed to 45 the next day then it should be separate how I can do that?

For e.g Expected result:

If product A place by 2 customer and one customer place 1 qty and 2nd customer place 3qty.

So when its generate csv it will show A product in single row, qty 4 and price x 4.

And if product A price today is 36 and its changed to 45 the next day then it should be separate.

Actual result:

its showing 2 records with product A.

3
  • You could use a MySQL query group by product SKU.
    – HoangHieu
    Commented Oct 12, 2022 at 7:02
  • can you see my updated code
    – imt
    Commented Oct 12, 2022 at 7:06
  • I means you could use the MySQL to export this instead of load the order collection. It more faster than load the order collection
    – HoangHieu
    Commented Oct 12, 2022 at 8:34

1 Answer 1

1

Replace your code with the following:

$ordersMerged = [];
$ordersMergedProductsDifferentPrice = [];
foreach ($orders as $order) {
    foreach ($order->getAllVisibleItems() as $item) {
        $totalPrice = $item->getQtyOrdered() * $item->getPrice();
        if (!array_key_exists($item->getSku(), $ordersMerged)) {
            $item->setOrderCreatedAt($order->getCreatedAt());
            $item->setTotalPrice($totalPrice);
            $ordersMerged[$item->getSku()] = $item;
        } else {
            // Check if the same product has different price
            if ($ordersMerged[$item->getSku()]->getPrice() != $item->getPrice()) {
                $item->setOrderCreatedAt($order->getCreatedAt());
                $item->setTotalPrice($totalPrice);
                $ordersMergedProductsDifferentPrice[$item->getSku()] = $item;
            } else {
                // Update qty and price for existing item
                $totalQty = $ordersMerged[$item->getSku()]->getQtyOrdered() + $item->getQtyOrdered();
                $totalPrice = $totalQty * $item->getPrice();
                $ordersMerged[$item->getSku()]->setQtyOrdered($totalQty);
                $ordersMerged[$item->getSku()]->setTotalPrice($totalPrice);
            }
        }
    }
}

foreach ($ordersMerged as $item) {
    $data['sku'] = $item->getSku();
    $data['name'] = $item->getName();
    $data['price'] = $item->getTotalPrice();
    $data['qty'] = $item->getQtyOrdered();
    $data['created_at'] = $item->getOrderCreatedAt();
    $stream->writeCsv($data);
}

foreach ($ordersMergedProductsDifferentPrice as $item) {
    $data['sku'] = $item->getSku();
    $data['name'] = $item->getName();
    $data['price'] = $item->getTotalPrice();
    $data['qty'] = $item->getQtyOrdered();
    $data['created_at'] = $item->getOrderCreatedAt();
    $stream->writeCsv($data);
}

Due to merging all orders, the order created at value will be merged. In this code, I've used the order created at from the first order that belongs to the ordered product.

10
  • 1 more thing if A product price today is 36 and its change to 45 next day then it should be seprate how i can do that?
    – imt
    Commented Oct 12, 2022 at 8:12
  • @imt I've updated the answer to support your new requirements.
    – Tu Van
    Commented Oct 12, 2022 at 9:14
  • great you are magician coder
    – imt
    Commented Oct 12, 2022 at 9:51
  • due this code qty disturbe?
    – imt
    Commented Oct 12, 2022 at 10:19
  • Thanks for your kind words. What do you mean by the above question? By the way, if you feel any answer or comment is helpful, that would be great if you click the Upvote button.
    – Tu Van
    Commented Oct 12, 2022 at 10:31

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.