Home    Articles

 

Get customer orders by email in wordpress woocommerce

 

Using woocommerce is easy, but sometimes you just don't find how to do things, like getting all orders of a particular user that uses one single email address.

This is very useful when you try to see if the same user bought as Registred or as Guest.

In order to find the user orders by it's email we will use get_posts() function with these $args.

 

?$customer_orders = get_posts( array(
				    'meta_key'    => '_billing_email',
				    'meta_value'  => $the_order->billing_email,
				    'post_type'   => 'shop_order',
				    'numberposts'=>-1
				) );

Use echo '<pre/>'; print_r( $customer_orders ); to view the result.

If you want to go over all orders with a loop, use this:

foreach($customer_orders as $k => $v)
{
	$order = new WC_Order( $customer_orders[ $k ]->ID );
        echo 'Order by '.$order->billing_first_name.' '.$order->billing_last_name;
}

That's all.

by