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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if(is_user_logged_in()){ | |
$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' | |
)); | |
} |