0

I like to use wc_get_orders() function to get WooCommerce orders in front end.

Here is my code snippet to get orders.

$args = array(
    'customer_id' => get_current_user_id(),
    'return' => 'ids',
    );
$customer_orders = wc_get_orders( $args );

var_dump($customer_orders);

The above code will return all orders of current users.

The issue is, the wc_get_orders return all orders including trashed orders. Is there any option to exclude trashed orders?

More details about this function are available in this link.

  • Sorry. Previously, I got wrong output due to some error. Now I confirmed that the wc_get_orders() function will exclude trashed orders. – Sarathlal N Jan 6 at 12:15
1

Use Wp_Query instead it will give you the option to get data with status.

$args = array(
  'post_type' => 'shop_order',
 'posts_per_page' => '-1',
'post_status' => array('publish', 'pending', 'draft', 'auto-draft', 'future','private', 'inherit', 'trash')  
);

$my_query = new WP_Query($args);

$orders = $my_query->posts;

echo "<pre>";
print_r($orders);
echo "</pre>";

it will display All of your order.

hope this will help you out.

  • Thanks for the reply. Instead of WP_Query, WooCommerce suggest to use wc_get_orders() and WC_Order_Query etc. – Sarathlal N Jan 6 at 6:44

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.