Great question — answering as the UphillTech team:
You can run the query inside a loop and update $wc_report->start_date and $wc_report->end_date for each month before calling get_order_report_data() . This way, you can fetch sales for the current month and the previous three months using the same query. Code replacement: Replace your existing $sold_products = $wc_report->get_order_report_data(...) query with this: $monthly_sales = array(); for ($month = 0; $month < 4; $month++) { $start_date = strtotime( date('Y-m-01', current_time('timestamp')) . " -{$month} months" ); $end_date = strtotime('+1 month', $start_date) - 1; $wc_report->start_date = $start_date; $wc_report->end_date = $end_date; $monthly_sales[$month] =...
Full answer by user33135826: https://stackoverflow.stackexchange.com/a/80011508 (CC BY-SA 4.0).
Want it for your business? UphillTech.