Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Mamaduka/718799841232ca0654ac to your computer and use it in GitHub Desktop.
Save Mamaduka/718799841232ca0654ac to your computer and use it in GitHub Desktop.
<?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.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 ) {
$user = get_user_by( 'id', $user_id );
// Bail, if we don't have user
if ( ! $user ) {
return;
}
// get admin email
$email = get_option( 'admin_email', '' );
$subject = "Customer Changed Address";
// make sure we have all of the required data
if ( empty ( $email ) ) {
return;
}
// get shipping data
$shipping_address = patricks_get_formatted_shipping_address( $_POST );
// format email
$message = 'Username: ' . $user->user_login . "\n";
$message .= 'User email: ' . $user->user_email . "\n";
$message .= 'User first name: ' . $user->user_firstname . "\n";
$message .= 'User last name: ' . $user->user_lastname . "\n";
$message .= "\n";
$message .= "\n";
$message .= 'New shipping address: ' . $shipping_address . "\n";
// send email
wp_mail( $email, $subject, $message );
}
add_action( 'woocommerce_customer_save_address', 'patricks_email_customer_address', 20 );
function patricks_get_formatted_shipping_address() {
$address = '';
if ( isset( $_POST['shipping_address_1'] ) ) {
$address .= $_POST['shipping_address_1'];
$address .= "\n";
}
if ( isset( $_POST['shipping_address_2'] ) ) {
$address .= $_POST['shipping_address_2'];
$address .= "\n";
}
if ( isset( $_POST['shipping_city'] ) ) {
$address .= $_POST['shipping_city'];
$address .= " ";
}
if ( isset( $_POST['shipping_state'] ) ) {
$address .= $_POST['shipping_state'];
$address .= ", ";
}
if ( isset( $_POST['shipping_postcode'] ) ) {
$address .= $_POST['shipping_postcode'];
$address .= "\n";
}
if ( isset( $_POST['shipping_country'] ) ) {
$address .= $_POST['shipping_country'];
}
return $address;
}
// That's all folks!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment