Home > Software design >  Get Woocommerce customer orders
Get Woocommerce customer orders

Time:12-12

Is is possible to get current and previous customer order? Namely I need Billing Address of current order and Billing Address of previous order per customer.

For example I need to get array of every Customer Order, namely I need Billing Address from every customer order.

So I have some code that print any text at "Edit Order page" in admin panel.

add_action( 'woocommerce_admin_order_data_after_shipping_address', 'edit_woocommerce_order_page', 10, 1 );

function edit_woocommerce_order_page($order){
global $post_id;
$order = new WC_Order( $post_id );
echo '<p><strong>Some text here</strong> ' . get_post_meta($order->get_id(), '_shipping_field_value', true ) . '</p>'; 
}

As you can see it displays Some text for every user. I guess I should get some array of every customer and display Order array with order ID and Billing Address 1. Check screenshot please Code above adds text in Edit Order page

Is it possible?

CodePudding user response:

The following function will grab all completed orders if customer is not a guest.

add_action( 'woocommerce_admin_order_data_after_shipping_address', 'edit_woocommerce_order_page', 10, 1 );

function edit_woocommerce_order_page($order){
$customer_id =  $order->get_user_id();
//Check if its guest or not
if($customer_id != 0):
    $args = array(
        'customer_id' => $customer_id,
        'status' => array('wc-completed'), //Change if needed
        'exclude' => array( $order->get_id() ), // We dont need current order
    );
    $orders = wc_get_orders( $args );
    if($orders):
        foreach($orders as $k=>$order):
            echo '<p><strong>Some text here</strong>'.$order->get_billing_address_1().'</p>';
        endforeach;
    endif;
endif;
}
  • Related