2

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+?

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
ComputerGiant
  • 159
  • 1
  • 1
  • 12

1 Answers1

1

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);
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Thanks. I am now retrieving 11 orders using both of your methods. However the code is still not displaying the actual data. One question: If I leave out the -1 then I am only displaying 5 orders in the count vs. 11. What is the difference? – ComputerGiant Jan 20 '18 at 01:48
  • @ComputerGiant The code works on all versions since WC 2.6.x… I think that the problem is in your database… something went wrong as woocommerce update also the database when updating from 2.6.x … May be it can also be something else, as I suppose that you have updated all plugins and Wordpress too. So you should inspect the wp_post table. … My answer there, is just answering your initial question. – LoicTheAztec Jan 20 '18 at 01:55
  • Using print_r(array_values($orders)); I am able to see the order data. I just need to go through how the other programmer spit it out to figure out where he went wrong, but I am still interested in the difference in values I'm getting when I use -1 for numberposts and leaving it out entirely. – ComputerGiant Jan 20 '18 at 01:56
  • With number of post = -1, on my test server I get 78 orders and without it I get only 5 … – LoicTheAztec Jan 20 '18 at 02:05
  • 1
    Thanks. Maybe it's some form of chunking where Wordpress /WC defaults to 5 results unless that is specified. Appreciate your help! – ComputerGiant Jan 20 '18 at 02:07