WooCommerce – Differentiating WordPress New Order Notification Recipient Based on City and Province or State

WooCommerce New Order Notification

I have a  customer who when they get an e-commerce order through their website built on WordPress using WooCommerce they want the admin new order notification to be emailed to the branch that is closest to them. There wasn’t any solution I could find for this so I created this script to allow me to define a different email address for order notifications based on the customer’s province. For Alberta and Saskatchewan, my client has two locations in each of those provinces so if the user is from Alberta and Saskatchewan the script tries to determine if the user is in the city where the branch is located and return that email address. If the customer is not in those cities the script will return that provinces default email address.

Installation:

  1. Change the variables below to correlate with the states and/or provinces with defined email addresses between  //EMAIL ADDRESSES FOR EACH PROVINCE/CITY DEFINED START  and  //EMAIL ADDRESSES FOR EACH PROVINCE/CITY DEFINED END
  2. If you want to set specific city email addresses change variables under //IF USER PROVINCE IS SASKATCHEWAN DETERMINE IF CITY You can define these cities in the provinces variable
  3. Copy this code to the bottom of your functions.php file in your Child Theme directory

 

<?
add_filter('woocommerce_email_recipient_new_order', 'diff_recipients_email_notifications', 10, 2);

function diff_recipients_email_notifications($recipient, $order)
{
    $provinces = array();
    //EMAIL ADDRESSES FOR EACH PROVINCE/CITY DEFINED START
    $provinces["alberta"] = "[email protected]";
    $provinces["calgary"] = "[email protected]";
    $provinces["edmonton"] = "[email protected]";
    $provinces["saskatchewan"] = "[email protected]";
    $provinces["regina"] = "[email protected]";
    $provinces["saskatoon"] = "[email protected]";
    $provinces["ontario"] = "[email protected]";
    $provinces["quebec"] = "[email protected]";
    $provinces["british columbia"] = "[email protected]";
    $provinces["manitoba"] = "[email protected]";

    $provinces["default"] = "[email protected]";
     //EMAIL ADDRESSES FOR EACH PROVINCE/CITY DEFINED END
     
     
    //IF USER PROVINCE IS SASKATCHEWAN DETERMINE IF CITY
    //IS REGINA OR SASKATOON IF NOT DEFAULT TO PROVINCE EMAIL ADDRESS
    $user_state = strtolower($order->shipping_state);
    if (empty($user_shipping_country)) $user_state = strtolower($order->billing_state);
    if ($user_state == "saskatchewan")
    {
        if (strtolower($order->shipping_city) == "regina" || strtolower($order->shipping_city) == "saskatoon")
        {
            $user_state = strtolower($order->shipping_city);
        }
    }
    //IF USER PROVINCE IS ALBERTA DETERMINE IF CITY
    //IS CALGARY OR EDMONTON IF NOT DEFAULT TO PROVINCE 
    if ($user_state == "alberta")
    {
        if (strtolower($order->shipping_city) == "calgary" || strtolower($order->shipping_city) == "edmonton")
        {
            $user_state = strtolower($order->shipping_city);
        }
    }
    if (array_key_exists($user_state, $provinces))
    {
        $recipient = $provinces[$user_state];
    }
    else
    {
        $recipient = $provinces["default"];
    }
    //RETURN EMAIL ADDRESS
    return $recipient;
}

You may also like