Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mattpramschufer/0d645edf2acbc4e2305eb41f3c9aba85 to your computer and use it in GitHub Desktop.
Save mattpramschufer/0d645edf2acbc4e2305eb41f3c9aba85 to your computer and use it in GitHub Desktop.
Email Admin when WooCommerce Customer Changes Address
<?php
/**
* Plugin Name: WooCommerce Email Customer Address
* Description: Email the site admin when a customer changes their address
* Author: Matt Pramschufer
* Version: 1.0.0
*
*/
function greenmagazine_email_customer_address( $user_id ) {
global $current_user;
// get user data
wp_get_current_user();
// get shipping data
$shipping_address = greenmagazine_get_formatted_shipping_address();
$billing_address = greenmagazine_get_formatted_billing_address();
// get admin email
$email = get_option( 'admin_email');
$subject = "Customer Changed Address";
// format email
$message = '<strong>Username:</strong> ' . $current_user->user_login . "<br>";
$message .= '<strong>User email:</strong> ' . $current_user->user_email . "<br>";
$message .= "<br><br>";
$message .= '<strong>New Shipping address:</strong> <br>' . $shipping_address . "<br><br>";
$message .= '<strong>New Billing address:</strong> <br>' . $billing_address . "<br>";
// make sure we have all of the required data
if ( ! $email ) {
return;
}
// send email
wp_mail( $email, $subject, $message );
}
add_action( 'woocommerce_customer_save_address', 'greenmagazine_email_customer_address', 20 );
function greenmagazine_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_first_name() . ' ' . $woocommerce->customer->get_shipping_last_name();
$address .= "<br>";
$address .= $woocommerce->customer->get_shipping_address();
$address .= "<br>";
$address .= $woocommerce->customer->get_shipping_address_2();
$address .= "<br>";
$address .= $woocommerce->customer->get_shipping_city();
$address .= " ";
$address .= $woocommerce->customer->get_shipping_state();
$address .= ", ";
$address .= $woocommerce->customer->get_shipping_postcode();
$address .= "<br>";
$address .= $woocommerce->customer->get_shipping_country();
return $address;
}
function greenmagazine_get_formatted_billing_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_billing_first_name() . ' ' . $woocommerce->customer->get_billing_last_name();
$address .= "<br>";
$address .= $woocommerce->customer->get_billing_address();
$address .= "<br>";
$address .= $woocommerce->customer->get_billing_address_2();
$address .= "<br>";
$address .= $woocommerce->customer->get_billing_city();
$address .= " ";
$address .= $woocommerce->customer->get_billing_state();
$address .= ", ";
$address .= $woocommerce->customer->get_billing_postcode();
$address .= "<br>";
$address .= $woocommerce->customer->get_billing_country();
return $address;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment