For WooCommerce version 2.5.x and 2.6.x
For WOOCOMMERCE VERSION 3.0+ see THIS UPDATE
This is completely possible using in woocommerce_admin_order_data_after_order_details
action hook a custom hooked function in which you will have to define in an array the orders statuses for your "Open orders".
Here is this functional and tested code:
add_action( 'woocommerce_admin_order_data_after_order_details', 'same_shipping_open_order', 10, 1 );
function same_shipping_open_order( $order ){
// Define HERE, in the array, your "OPEN" orders statuses
$open_order_statuses = array('wc-pending','wc-processing','wc-on-hold');
// Initialising variables
$matching = false;
// Get the shipping 'address_1' & 'postcode' fields for the CURRENT ORDER
$order_ship_address1 = $order->shipping_address_1;
$order_ship_postcode = $order->shipping_postcode;
// Getting customer orders, with an open status
$open_orders = wc_get_orders( array(
'numberposts' => -1,
'meta_key' => '_customer_user',
'meta_value' => $order->get_user_id(),
'post_type' => 'shop_order',
'post_status' => $open_order_statuses,
'exclude' => array($order->id),
) );
// Other "Open" orders for this customer
if( count($open_orders) != 0 ){
// Iterating through each orders
foreach($open_orders as $open_order){
if( $order_ship_address1 == $open_order->shipping_address_1 && $order_ship_postcode == $open_order->shipping_postcode ){
$matching = true; // set condition to true
$open_order_id = $open_order->id;
// Other orders edit url
$order_edit_url = home_url( "/wp-admin/post.php?post=$open_order_id&action=edit/" );
// Storing orders edit url + ID
$results_arr[] = "<a href='$order_edit_url'>#$open_order_id</a>";
}
}
}
// If there is matching "Open" orders shipping addresss with this order
if ( $matching ) {
## 0. Converting the array in a string for output
$output_html = implode(', ', $results_arr);
## 1. Displaying an alert message on the order
echo '<br clear="all"><p style="margin-top:12px !important;"><strong style="color:red;">'. __("Same Shipping on Open Orders IDs: ").'</strong><br>'.$output_html.'</p>';
## 2. Javascript Alert message
?>
<script>
(function($){
alert('SAME SHIPPING ON OPEN ORDERS!');
})(jQuery);
</script>
<?php
}
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
You will get an alert message when opening/editing the order and also a text with the related "Open orders Ids and edit links. See the screenshot below:
