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?
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;
}
Limit to last 2 orders
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
'limit' => 2,
);
$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;
}
Getting current order and previous one only
function edit_woocommerce_order_page($order){
$customer_id = $order->get_user_id();
//We need current order id to know where we start
$order_id = $order->get_id();
if($customer_id != 0):
$args = array(
'customer_id' => $customer_id,
'status' => array('wc-completed'), //Change if needed
'return' => 'ids', // Grab all order ids for customer
'posts_per_page' => -1
);
$all_order_ids = wc_get_orders( $args );
//Find current order key
$all_order_id_keys = array_flip(array_keys($all_order_ids));
$current_order_key = array_keys($all_order_ids, $order_id);
//Grab all values from our array
$all_order_id_values = array_values($all_order_ids);
//From all values we look for current order key and we increase that key with 1 to grab prev order id by key
$previous_order_id = $all_order_id_values[$all_order_id_keys[$current_order_key[0]]+1];
$order_args = array(
'post__in' => array($order_id,$previous_order_id),
);
$orders = wc_get_orders( $order_args );
if($orders):
foreach($orders as $k=>$order):
echo $order->get_id(); // For testing
echo '<p><strong>Some text here</strong>'.$order->get_billing_address_1().'</p>';
endforeach;
endif;
endif;
}
add_action( 'woocommerce_admin_order_data_after_shipping_address', 'edit_woocommerce_order_page', 10, 1 );