Last active
November 14, 2023 17:34
-
-
Save damiencarbery/f4dda232460e759fe7b55e9da1bc4585 to your computer and use it in GitHub Desktop.
Notify admin of customer address changes - Email the site admin when a customer changes their billing or shipping address in WooCommerce. https://www.damiencarbery.com/2019/09/notify-admin-of-customer-address-changes/
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: Notify admin of customer address changes | |
Plugin URI: https://www.damiencarbery.com/2019/09/notify-admin-of-customer-address-changes/ | |
Description: Email the site admin when a customer changes their billing or shipping address in WooCommerce. | |
Author: Damien Carbery | |
Author URI: https://www.damiencarbery.com | |
Version: 0.2 | |
WC tested up to: 8.2.2 | |
*/ | |
add_action( 'woocommerce_after_save_address_validation', 'dcwd_after_save_address', 10, 4 ); | |
function dcwd_after_save_address( $user_id, $load_address, $address, $customer ) { | |
//error_log( 'User ID: ' . $user_id ); | |
//error_log( 'Load address: ' . var_export( $load_address, true ) ); | |
//error_log( 'Address: ' . var_export( $address, true ) ); | |
//error_log( 'Customer: ' . var_export( $customer, true ) ); | |
// Retrieve any changes. | |
$customer_original_info = new WC_Customer( $user_id ); | |
$fields_changed = $customer->get_changes(); | |
$existing_address = array(); | |
if ( 'billing' == $load_address ) { | |
$existing_address = $customer_original_info->get_billing(); | |
} | |
else { | |
$existing_address = $customer_original_info->get_shipping(); | |
} | |
// Create a list of the changes, with a friendly name for each field (e.g. 'Company Name' | |
// instead of 'company_name') and the old and new values. | |
$changes_text = array(); | |
// Check that there are changes before looping through the list of changed fields. | |
if ( array_key_exists( $load_address, $fields_changed ) ) { | |
foreach ( $fields_changed[ $load_address ] as $field => $value ) { | |
$changes_text[] = sprintf( '%s from "%s" to "%s"', $address[ $load_address . '_' . $field ][ 'label' ], $existing_address[ $field ], $value ); | |
} | |
} | |
if ( $changes_text ) { | |
error_log( 'Customer changed ' . ucwords( $load_address ). " address fields:\n" . implode( "\n", $changes_text ) ); | |
// Send the email to the admin. | |
$subject = sprintf( 'Customer address change: %s (%d)', $customer->get_display_name(), $user_id ); | |
$body[] = sprintf( 'Customer %s (ID: %d) changed %s address fields.', $customer->get_display_name(), $user_id, $load_address ); | |
$body[] = ''; | |
$body[] = 'Changes:'; | |
$body[] = '- ' . implode( "\n- ", $changes_text ); | |
wp_mail( get_option( 'admin_email' ), $subject, implode( "\n", $body ) ); | |
} | |
else { | |
// It's possible to save the form without changing anything. | |
//error_log( 'No changes were made.' ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment