Send a Notification Email When a Woocommerce Customer Changes Their Address

EnvelopeI have a client who runs a subscription-based business on a WordPress site using Woocommerce. Since some of his subscriptions last a year or more, increasing the likelihood that a customer’s address might change, he wanted to be notified by email if the customer used the Woocommerce change-of-address form.

While it’s possible to add a custom email notification class so that the email notification will show up in Woocommerce -> Settings -> Emails, I really just needed something simple, and didn’t mind that the recipient email address would be hard-coded. I came up with the following code snippet, which does the trick in WordPress 4.6 and Woocommerce 2.6.4.

If you want to try it, copy and paste the code into the functions.php of your child theme, then change the $from_name, $from_email, and $to on lines 6, 7, and 8 to match your needs. I just used the name of the site for the $from_name. Make sure to use an actual email account on your domain for the $from_email. Some servers will reject mail that is sent from another domain or a non-existent account. The $to should be set to the email address of the person you want to receive the change-of-address notification emails.

// Send notification email when customer changes address.
add_action( 'woocommerce_customer_save_address','notify_admin_customer_address_change', 10, 2);
function notify_admin_customer_address_change( $user_id ) {

	// Set the $from_name, $from_email, and $to for your site.
	$from_name = 'Your From Name';
	$from_email = 'email@yourdomain.com';
	$to = 'email@somedomain.com';
	
	global $woocommerce, $current_user;
	
	// 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 .= "Billing Address:\n";
	
	// get the data from the profile after it's set by the customer save
	$woocommerce->customer->set_default_data();
	$message .= $woocommerce->customer->get_address() . "\n";
	$address_2 = $woocommerce->customer->get_address_2();
	if ($address_2 != '') {
		$message.= $address_2 . "\n";
	}
	$message .= $woocommerce->customer->get_city() . " ";
	$message .= $woocommerce->customer->get_state() . " ";
	$message .= $woocommerce->customer->get_postcode() . "\n";
	$message .= $woocommerce->customer->get_country() . "\n\n";
	$message .= "Shipping Address:\n";
	$message .= $woocommerce->customer->get_shipping_address() . "\n";
	$shipping_address_2 = $woocommerce->customer->get_shipping_address_2();
	if ($shipping_address_2 != '') {
		$message .= $shipping_address_2 . "\n";
	}
	$message .= $woocommerce->customer->get_shipping_city() . " ";
	$message .= $woocommerce->customer->get_shipping_state() . " ";
	$message .= $woocommerce->customer->get_shipping_postcode() . "\n";
	$message .= $woocommerce->customer->get_shipping_country();
	
	
	$headers = 'From: ' . $from_name . ' <' . $from_email . '>' . "\r\n";
	
	wp_mail($to, 'Customer Address Change Notice', $message, $headers);
}