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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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
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.
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
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.
There is no plugin found at mentioned link.
Which link? Everything seems to work for me.
Thiri, You need to actually click on the link in the copy, not the ‘Plugins’ link in the header menu.
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.
I’m very new to WordPress, but I absolutely need this feature ! Do I simply copy the .php file into my theme folder ? Thanks !
Hi François,
That won’t work. If you scroll up you’ll need to click on the
plugins home page
link, click theDownload Gist
button`, unzip the file, and upload it to your plugins folder. Then activate it.I hope that helps! 🙂
Thanks Patrick! I love your blog!
This plugin is letting me know that a customer has changed their address, but is not sending me the new address.
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
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 ?
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!
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?
This works fine for me but it’s sending the old address on change – not new one.
Unfortunately for us too, the information in the email are not the updated address you will find in the customers account
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).
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)
Hi,
does someone know how to make this work when customer is in checkout and then changes something in the address?
Thank you