kloon / WooCommerce-REST-API-Client-Library

A PHP wrapper for the WooCommerce REST API
GNU General Public License v3.0
356 stars 175 forks source link

Fetching sales reports incl. refunds #175

Open rbsmidt opened 8 years ago

rbsmidt commented 8 years ago

Hi

I'm facing some issues while trying to fetch sales reports using your PHP wrapper. I need to extend the sales reports, that WooCommerce generates automatically, to ease/automate some accounting tasks. Hence i've build a little Dashboard Widget to fetch sales numbers given a start and end date, and split the sales by payment method, regions (Native country, (rest of) EU, Rest of World) and how much of the total is from sale of shipping. However i have some issues when it comes to orders with partial refunds. By partial refunds i'm referring to and order of more than one item, where only some of the items are refunded. The order will still have "completed" as status, but in the bottom of the order view in WP-Admin, the refund of a certain amout will show as subtracted from the totals.

What i do is i fetch orders in the given date range, and loop through these while adding order total to the current payment method total. The API Call is this:

$orders = $client->orders->get( null, [ 'status' => 'completed', 'filter[limit]' => -1, 'filter[created_at_min]' => $start->format('Y-m-d'), 'filter[created_at_max]' => $end->format('Y-m-d') ] );

If i just fetch orders by status => completed in the date range, the response won't take partial refunds into account. I then tried adding an extra API Call in my foreach loop, checking if the current order has order_refunds with this code:

$refunds = $client->orders->get( $order->id.'/refunds', [] );

This actually fetches the partial refund object, and lets me subtract the refund from the order total - however it increases the request time to a degree where it's not really an option. If trying to get the numbers for a full month the request time is about a minute or more (avrg. orders pr. month is around 200).

When looking in the source code of the order-refunds, i can't seem to find an enpoint/method that isn't expecting an order id, which makes it hard for me to figure a way to fetch refunds in a smarter/more performance friendly way?

My question therefor: Is there a smarter approach to fetch these partial refunds, so i can get the right numbers without overloading my server with API Calls causing the request time to increase and perfomance to plummet?