Notify Admin of Customer Address Change in WooCommerce

woocommerce banner
  1. Blogging for Benjamin Competition
  2. Why I'm Grateful to Work on the Web
  3. 24 Pull Requests
  4. Update Downloadable Product's Expiration Date in WooCommere
  5. Get Lost in the Flow and Work for More Than a Salary
  6. Why A Plugin's Popularity Matters
  7. Why You Should (Or Shouldn't) Use Premium Plugins
  8. WooCommerce Terms & Conditions
  9. Only Ship to Continental United States with WooCommerce
  10. Just Talk
  11. Why I Love Jetpack
  12. Making Jetpack Better
  13. Remove Billing Address for Free Virtual Orders in WooCommerce
  14. Notify Admin of Customer Address Change in WooCommerce
  15. Open Your Self Up To New Possibilities
  16. 2013 Resolutions Review
  17. Create a Community
  18. Tips for Starting a Community
  19. The Intent of Goals
  20. Create The Ultimate Invoicing System Using WooCommerce
  21. Change From Address in Ninja Forms
  22. Work With People Who Inspire You
  23. Contact Form 7 & MailPoet Integration
  24. Monotasking
  25. Giving Back to The Community
  26. Adding Fuctionality to Lean Plugins
  27. Choose Stripe For a Payment Gateway
  28. A Dip Into Entrepreneurship
  29. Reward Yourself
  30. Blogging for Benjamin Plugin
  31. Blogging for Benjamin Wrap Up

One of the extensions that I love working with at WooThemes is the Subscriptions extension for WooCommerce. It's probably the most complex extension we have which can make troubleshooting issues challenging but that's offset because the developer also has the best documentation bar none.

The number of possibilities with Subscriptions are really endless. One of my favorite examples is HDPiano.com which sells virtual memberships to learn how to play piano. There's also plenty of users who sell tangible products each month. I just ran into a user who creates fancy shipping labels in Photoshop and uses those labels to ship her products.

The only problem in her case was that she creates the shipping label before the new order comes in so she needed a way to be notified of a customer changing their address before the actual order came in. This is where some of those awesome WooCommerce hooks come in.

Address Change Hook

In this case I'm going to be using the woocommerce_customer_save_address action and add some additional functionality when the user changes their address on the My Account page.

I’m including the plugin for you to look at the code but you can also go the plugins home page and download it, extract the zip file, and upload the folder to your /wp-content/plugins/ directory of your site.


<?php
/**
* Plugin Name: WooCommerce Email Customer Address
* Plugin URI: https://gist.github.com/BFTrick/7891074
* Description: Email the site admin when a customer changes their address
* Author: Patrick Rauland
* Author URI: http://patrickrauland.com/
* Version: 1.0.1
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @author Patrick Rauland
* @since 1.0
*/
function patricks_email_customer_address( $user_id ) {
global $current_user;
// get user data
get_currentuserinfo();
// get shipping data
$shipping_address = patricks_get_formatted_shipping_address();
// get admin email
$email = get_option( 'admin_email', '' );
$subject = "Customer Changed Address";
// format email
$message = 'Username: ' . $current_user->user_login . "\n";
$message .= 'User email: ' . $current_user->user_email . "\n";
$message .= 'User first name: ' . $current_user->user_firstname . "\n";
$message .= 'User last name: ' . $current_user->user_lastname . "\n";
$message .= "\n";
$message .= "\n";
$message .= 'New shipping address: ' . $shipping_address . "\n";
// make sure we have all of the required data
if ( empty ( $email ) ) {
return;
}
// send email
wp_mail( $email, $subject, $message );
}
add_action( 'woocommerce_customer_save_address', 'patricks_email_customer_address', 20 );
function patricks_get_formatted_shipping_address() {
global $woocommerce;
// get the data from the profile after it's set by the customer save
$woocommerce->customer->set_default_data();
$address .= $woocommerce->customer->get_shipping_address();
$address .= "\n";
$address .= $woocommerce->customer->get_shipping_address_2();
$address .= "\n";
$address .= $woocommerce->customer->get_shipping_city();
$address .= " ";
$address .= $woocommerce->customer->get_shipping_state();
$address .= ", ";
$address .= $woocommerce->customer->get_shipping_postcode();
$address .= "\n";
$address .= $woocommerce->customer->get_shipping_country();
return $address;
}
// That's all folks!

In this case I just get all of the user data using the really useful get_currentuserinfo() function and then get the customers address using the WC_Customer class. Then send all of the information using the wp_mail function. You might notice that I didn't add any filters in this plugin and I wrote the address in a standard US style. That's because this is a really quick snippet. If I wanted this to be more modular for the future I would add extra filters to accommodate these needs.

End Result

Changed Address Email

So what does it look like at the end? Why a simple email in your inbox.

Exploring

While we don't offer customization support at WooThemes there is a lot of value in occasionally doing customizations like this. They are opportunities to dig into the code and learn a bit more about our huge WC system while helping a customer at the same time. And then if you can make the information publicly available (like this blog post) then it's indexable for future users. Win win in my opinion.

21 thoughts on “Notify Admin of Customer Address Change in WooCommerce

  1. Hello Sir,

    Thanks for the nice snippet, however the code is wrong.

    You need to get the user data from $user_id provided within a hook.

    Accessing global $current_user variable is wrong – in case admin changed some other user, he’d be notitifed about his own address.

    Moreover you should do the same for the $woocommerce functions used within the patricks_get_formatted_shipping_address.

    Regards,
    Mate

  2. Just a heads up.

    I’ve checked the hook source and the $current_user global is used properly.

    I expected this hook to be fired on the admin edit page as well, which caused the confusion – you were right in here.

    Anyway the $user_id is not passed without a purpose – it should be used in spite of the global variable.

    • Glad that the hook fires at the right time. Using the global $current_user && get_currentuserinfo() is actually something you can find in the WordPress Codex so it should be fine to use. No sense using the $user_id and calling a less efficient function.

      • The function is going to be as much efficient as it’s been. It should also be a bit faster (due to lack of access to the global) nonetheless I’m not sure why the parameter is there.

  3. There is no plugin found at mentioned link.

  4. Thiri, You need to actually click on the link in the copy, not the ‘Plugins’ link in the header menu.

  5. Hey Patrick,

    Thanks for this information. Unfortunately for me, the mail i receive contains no address information. After the message “New shipping address” it’s blank. Username, Email, First name and last name works fine.

    Any thoughts? I’m working with Woocommerce 2.2.10.

    Any help will be appreciated.

  6. I’m very new to WordPress, but I absolutely need this feature ! Do I simply copy the .php file into my theme folder ? Thanks !

  7. This plugin is letting me know that a customer has changed their address, but is not sending me the new address.

  8. Hi Patrick,

    Thanks for the code snippet, but in email its showing wrong shipping address (Older one) and not the one which i am updating from shipping form on front end, do you know whats the issue ?
    Thanks,
    Swayam

  9. Found some workaround by using $_POST[‘shipping_address_1’] instead of $woocommerce->customer->get_shipping_address() , and problem is solved, but i think this is not the right way, can you suggest something ?

  10. Hi Patrick,
    Nice plugin!
    I have a small problem though: the “New shipping address” in the email is not the new one, but the old one.
    Any idea on how to solve that?
    Thanks!

  11. For some reason I am getting the notifications, but the address in the email is still the original un-edited address — if I update the address a second time, the first change appears in the email.

    is there a way to send me an email with the updated address instead of the original?

    also, is there a way to automatically update the customers order with the new address in the system without having to manually update the order from the admin edit order screen?

  12. This works fine for me but it’s sending the old address on change – not new one.

  13. hi Patrick

    great work, very handy.

    I’ve noticed this however doesn’t seem to work if an existing customer is buying something and changes their shipping address ( in the checkout process).

  14. hi Patrick

    great work, very handy.

    I’ve noticed this however doesn’t seem to work if an existing customer is buying something and changes their shipping address ( in the checkout process)

  15. Hi,
    does someone know how to make this work when customer is in checkout and then changes something in the address?
    Thank you

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.