1

I own/operate a WooCommerce Shop and I am looking for a way to Set up a Shop Notice when More than One Order comes in from the same Customer, Shipping to the Same Address.

I have been lucky enough to be blessed with tons of repeat customers. They will order from my shop on let's say a Monday and pay the shipping rate for their item(s) & then order on say a Tuesday (before I am able to ship out the first order), so I try to include the second product into first order to "SAVE" on shipping. But I can only save on Shipping if I pay attention to the customer's shipping address and bundle the items.

I would like my site to be a bit more efficient in this area and check for "OPEN" or "PROCESSING" orders that have matching shipping address's and pop up an alert when I open one of the orders in question...
Is something like this possible?

I have searched and searched and searched and nothing... I'm not completely sure where to start on this...

Is this possible with a custom script in functions.php file?
Is there a plugin that can help do this?

Any help is greatly appreciated.

Thanks.

aynber
  • 22,380
  • 8
  • 50
  • 63
JStormThaKid
  • 1,794
  • 3
  • 20
  • 26

2 Answers2

1

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:

Open orders screenshot

Community
  • 1
  • 1
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • 1
    OMG! EXACTLY what I was looking for! This works AMAZINGLY! Thank you soooo much +LoicTheAztec! – JStormThaKid Mar 09 '17 at 16:02
  • Hey +LoicTheAztec, It seems as if either WP v4.8 or WC v3.1.0 breaks this functionality... specifically with this line.. // Converting the array in a string for output $output_html = implode(', ', $results_arr); it spits out the error below Warning: implode(): Invalid arguments passed in ***PATH***/functions.php on line 138 – JStormThaKid Jul 05 '17 at 02:05
  • @JStormThaKid I have add another answer with [the **functional** and **tested code for WooCommerce 3+ in HERE**](https://stackoverflow.com/a/44916440/3730754) – LoicTheAztec Jul 05 '17 at 03:14
1

Version code for WooCommerce 3+

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 Order data (WC 3+ compatibility)
    $order_data = $order->get_data();

    // Get the shipping 'address_1' & 'postcode' fields for the CURRENT ORDER
    $order_ship_address1 = $order_data['shipping']['address_1']; # (WC 3+ compatibility)
    $order_ship_postcode = $order_data['shipping']['postcode']; # (WC 3+ compatibility)

    // 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_data['id']), # (WC 3+ compatibility)
    ) );

    // Other "Open" orders for this customer
    if( count($open_orders) != 0 ){
        // Iterating through each orders
        foreach($open_orders as $open_order){

            // Get "open" Order data (WC 3+ compatibility)
            $open_order_data = $open_order->get_data();
            // Orders Id
            $open_order_id = $open_order_data['id']; 

            if( $order_ship_address1 == $open_order_data['shipping']['address_1'] && $order_ship_postcode == $open_order_data['shipping']['postcode'] ){
                // set condition to true (There is at once 1 matched order)
                $matching = true; 

                // 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.

This code is tested and works the same way on WooCommerce 3+


See this related updated answer: How to get order details in WooCommerce 3.0+

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Hey +LoicTheAztec, I popped the updated code into the functions.php file of my Child Theme, but i still get the "Warning: implode(): Invalid arguments passed" error in the backend – JStormThaKid Jul 05 '17 at 11:03
  • @JStormThaKid I don't got any error on my test server… everything work just nice on WooCommerce 3+ – LoicTheAztec Jul 05 '17 at 16:19