Email the site admin when a customer changes their billing or shipping address in WooCommerce.
Last week someone emailed me asking me to write code to store WooCommerce customer address changes in a custom field. I did a little research and directed them to the ‘woocommerce_after_save_address_validation‘ action that I had found.
Although I wasn’t willing to write the requested code for free (or even the tiny amount of money offered), I felt that emailing the site admin about customer address changes could be a useful feature, perhaps highlighting fraud or suspicious activity on an account.
Only the changes
The ‘woocommerce_after_save_address_validation‘ action is passed the customer ID, the address type (billing or shipping), the address fields info and the customer object.
Interestingly, when you retrieve the billing or shipping address from the customer object it only returns the changed fields. This means that we don’t have to go through the old and new addresses to find the changed fields.
To highlight the changes I use the customer ID to create a new customer object and use that to retrieve the previous address fields.
As it is possible that nothing has changed e.g. the customer goes to edit the address and clicks Save without changing anything, the email is only sent when there are changes.
Here’s what the email looks like:
The email lists the changed fields with their previous value. It could easily be extended to list the full before and after address. To go even further the changed fields could be highlighted in a different colour in the updated address.
Another thought is to create a WooCommerce email class so that the email can be a nicer format, with the WooCommerce email header and footer. This would also allow it to be managed from WooCommerce/Settings/Email area.
The code is not run when the address is changed from within the Dashboard (Users/Your Profile) so there might be another hook that is involved there. This is a low priority because the customer would generally not have access to the Dashboard to make their address changes there.