function oir_generate_report($from, $to) { if (!function_exists('wc_get_orders')) { echo "
WooCommerce not active
"; return; } $orders = wc_get_orders([ 'limit' => -1, 'status' => ['wc-processing', 'wc-on-hold'], 'date_created' => $from . '...' . $to, ]); if (!$orders) { echo "No orders found
"; return; } $items_data = []; foreach ($orders as $order) { $order_id = $order->get_id(); foreach ($order->get_items() as $item) { $product = $item->get_product(); if (!$product) continue; $product_id = $product->get_id(); $product_name = $item->get_name(); $qty = $item->get_quantity(); // Image $image = ''; if ($product->get_image_id()) { $image = wp_get_attachment_image($product->get_image_id(), [40, 40]); } if (!isset($items_data[$product_id])) { $items_data[$product_id] = [ 'name' => $product_name, 'qty' => 0, 'image' => $image, 'orders' => [] ]; } $items_data[$product_id]['qty'] += $qty; // ✅ collect order IDs $items_data[$product_id]['orders'][$order_id] = $order_id; } } if (empty($items_data)) { echo "No items found
"; return; } // Sort uasort($items_data, function ($a, $b) { return $b['qty'] - $a['qty']; }); echo "| Image | Product ID | Product | Order IDs | Total Quantity |
|---|---|---|---|---|
| {$data['image']} | "; echo "{$product_id} | "; echo "" . esc_html($data['name']) . " | "; echo "{$order_ids} | "; echo "{$data['qty']} | "; echo "
Lavanya –
Nice
divya Rishi –
Good quality