After updating Woocommerce from 2.6.13 to 3.2.6 some custom code that displays pending orders and tallies up the products ordered no longer works. I am having trouble porting the code over as the woocommerce docs I have been locating appear to be out of date.
For example, I don't think this is valid anymore but I can't find the updated version
$orders = get_posts( array(
'post_type' => 'shop_order',
'post_status' => array( 'wc-processing', 'wc-completed' )
) );
I had updated it from the one below but neither are returning anything in the array
$orders = get_posts( array(
'post_type' => 'shop_order',
'post_status' => 'publish',
'tax_query' => array( array(
'taxonomy' => 'shop_order_status',
'field' => 'slug',
'terms' => array( 'processing', 'completed' )
) )
) );
What is the proper way to get orders using get_posts from woocommerce in 3.0+?
Your first snippet code is valid in WC 3+ to get WP_Post
order objects array, but you need to specify the number of posts this way:
$post_orders = get_posts( array(
'post_type' => 'shop_order',
'numberposts' => -1, // number of post (all)
'post_status' => array( 'wc-processing', 'wc-completed' )
) );
// Display the number of Post orders objects in the array
echo count($post_orders);
OR you can use this SQL query:
global $wpdb;
$post_orders = $wpdb->get_results( "
SELECT *
FROM {$wpdb->prefix}posts
WHERE post_type LIKE 'shop_order'
AND post_status IN ('wc-processing', 'wc-completed')
" );
// Display the number of WP_Post orders in the array
echo count($post_orders);
To get an array of WC_Order
objects instead, you can use:
$orders = wc_get_orders( array(
'numberposts' => -1, // number of post (all)
'post_status' => array( 'wc-processing', 'wc-completed' )
) );
// Display the number of WP_Order orders in the array
echo count($orders);