David
  • 0

How to get all orders of a single customer in WooCommerce?

  • 0

In WooCommerce, how can I get all orders of a single customer with a user id? Also I want to get order number of each orders of that particular user.

1 Answer

  1. You can use wc_get_orders function to get orders from WooCommerce. Try the following code to retrieve orders from a specific user.

        $customer_id = '123'; // Set the customer id to retieve orders
    
        $args = array(
            'customer_id' => $customer_id,
            'status' => array('wc-completed', 'wc-processing', 'wc-on-hold'), // Replace with the order statuses you want to retrieve
            'limit' => 10
        );
        
        $customer_orders = wc_get_orders( $args );
        
        // Loop through each order
        foreach ( $customer_orders as $customer_order ) {
            // Access order details
            $order_number = $customer_order->get_order_number(); // get order number
            // do your code here
        }
    

    You will get 10 recent orders of a customer by using this code. If you want to get all orders, then you can change limit to “-1” as limit => -1. But it is not recommended to use limit “-1” as it may lead to performance issue if your site has large number of orders.

Leave an answer

You must login to add an answer.