Last modified by - 8 years ago
1 min read

How to get all order ids of a customer in WooCommerce

If you want to find the orders of a WooCommerce customer , since orders are a custom post type in WooCommerce you could use get_posts.

Important parts to remember in your query:

  • For post type don't hardcode to "shop_order" but use wc_get_order_types() which will get all order types
  • For order_status use wc_get_order_statuses(), which will get all the WooCommerce order statuses
  • I added fields=>'ids' to only get the order ids since that was what I needed but you could remove this to get all order data
  • For numberposts I used -1 to get all the orders of this customer , but you could put a count such as 10
  • I used orderby date and order to get the recent orders first
  • I also check with is_user_logged_in function to see if this is a logged in user and not a visitor, before creating the query

$cust_orders = get_posts( array(
'numberposts' => -1,
'meta_key' => '_customer_user',
'meta_value' => get_current_user_id(),
'fields' => 'ids',
'post_type' => wc_get_order_types(),
'post_status' => array_keys( wc_get_order_statuses() ),
'orderby' => 'date',
'order' => 'DESC'
view raw gistfile1.txt hosted with ❤ by GitHub

Related Embeds
Previous Next
Related Panels
Related Articles
Related Documents
Previous Next
Was this information helpful?