woocommerce / woocommerce-rest-api

This is the WooCommerce core REST API Package. It runs standalone as a feature plugin too.
71 stars 46 forks source link

Orders by product returning incorrect orders #250

Open bobbydc opened 3 years ago

bobbydc commented 3 years ago

I am trying to use the API to pull orders based on a category. Since I don't see a way to do that, I am pulling all products based on category and then looping through those to pull down the orders filter by product ID. For some reason it's pulling in all sorts of orders that don't include the ID. Here's the query I'm using.

$evtProducts is confirmed as correct data and format. I can echo out the $product[ 'id' ] and it's correct.

foreach ( $evtProducts as $product ) { $data = ''; $data = $woocommerce->get( 'orders', $parameters = [ 'per_page' => '25', 'product' => $product[ 'id' ] ] ); $decode[] = json_decode( json_encode( $data ), true ); $orderData = array_merge( $orderData, $decode ); $headers = $woocommerce->http->getResponse()->getHeaders(); $totalPages = $headers[ 'x-wp-totalpages' ]; $count = 1; do { $count++; $data = ''; $data = $woocommerce->get( 'orders', $parameters = [ 'per_page' => '25', 'search' => $category[ 'name' ], 'page' => $count ] ); $decode[] = json_decode( json_encode( $data ), true ); $orderData = array_merge( $orderData, $decode ); sleep( 3 ); } while ( $count < $totalPages ); } }

It's in a do while look because it can top out at 5k orders for one product. It's not nearly that large yet as we just launched this store this month. There are only about 500 total orders in this system so I don't think it's an issue with DB site or hosting.

I did also confirm it's not caching. We manage our own servers and this is just a liquidweb cpanel site on a dedicated server. No other sites are on this server and the headers don't show any kind of caching.

My end goal is only to have a list of products, the number of units sold and total revenue generated on those product sales. It seems pretty simple but I am not finding a way to do that in the API without dumping a lot more data than I really need.

So, any thoughts?