-
-
Save BFTrick/7891074 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.0.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 ) { | |
global $current_user; | |
// get user data | |
get_currentuserinfo(); | |
// get shipping data | |
$shipping_address = patricks_get_formatted_shipping_address(); | |
// get admin email | |
$email = get_option( 'admin_email', '' ); | |
$subject = "Customer Changed Address"; | |
// 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 .= 'New shipping address: ' . $shipping_address . "\n"; | |
// make sure we have all of the required data | |
if ( empty ( $email ) ) { | |
return; | |
} | |
// send email | |
wp_mail( $email, $subject, $message ); | |
} | |
add_action( 'woocommerce_customer_save_address', 'patricks_email_customer_address', 20 ); | |
function patricks_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_address(); | |
$address .= "\n"; | |
$address .= $woocommerce->customer->get_shipping_address_2(); | |
$address .= "\n"; | |
$address .= $woocommerce->customer->get_shipping_city(); | |
$address .= " "; | |
$address .= $woocommerce->customer->get_shipping_state(); | |
$address .= ", "; | |
$address .= $woocommerce->customer->get_shipping_postcode(); | |
$address .= "\n"; | |
$address .= $woocommerce->customer->get_shipping_country(); | |
return $address; | |
} | |
// That's all folks! |
It seems like this plugin sends out the old address instead of the newly updated address. Anybody here knows how to fix it?
Works great. Thank you for this plugin.
After few days of using your script I have discovered some problems.
This script works only if the customer change the address from MyAccount page.
If customer change his address during checkout, email won't be sent.
As alfonsberger claimed that plugin sends out the old address, I can also confirm that this happened to me once, but it happened.
Ok I made some changes to this function which works for me. It gives the updated data and not the previous data. Hope it works for others as well.
`function patricks_get_formatted_shipping_address() {
global $woocommerce;
// get the data from the profile after it's set by the customer save
$curr_user = wp_get_current_user();
$user_id = $curr_user->ID;
//$woocommerce->customer->set_default_data();
//Shipping Address
$address .= get_user_meta( $user_id, 'shipping_address_1', true );//$woocommerce->customer->get_shipping_address();
$address .= "\n";
if(!empty(get_user_meta( $user_id, 'shipping_address_2', true ))){
$address .= get_user_meta( $user_id, 'shipping_address_2', true );//$woocommerce->customer->get_shipping_address_2();
$address .= "\n";
};
$address .= get_user_meta( $user_id, 'shipping_city', true );//$woocommerce->customer->get_shipping_city();
$address .= " ";
$address .= get_user_meta( $user_id, 'shipping_state', true );//$woocommerce->customer->get_shipping_state();
$address .= ", ";
$address .= get_user_meta( $user_id, 'shipping_postcode', true );//$woocommerce->customer->get_shipping_postcode();
$address .= "\n";
$address .= get_user_meta( $user_id, 'shipping_country', true );//$woocommerce->customer->get_shipping_country();
return $address;
}`
Hi,
does someone know how to make this work when customer is in checkout and then changes something in the address?
Thank you
Hi, Is this still up to date?
Thanks @BFTrick for the idea and especially thanks to @PegWeb who has solved the problem that it was sending the old data instead of the new one.
For me it works correct like this
`<?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.0.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 ) {
global $current_user;
// get user data
get_currentuserinfo();
// get shipping data
$shipping_address = patricks_get_formatted_shipping_address();
// get admin email
$email = get_option( 'admin_email', '' );
$subject = "Change data";
// format email
$message = 'User: ' . $current_user->user_login . "\n";
$message .= 'Email: ' . $current_user->user_email . "\n";
$message .= 'Name: ' . $current_user->user_firstname . "\n";
$message .= 'Lastname: ' . $current_user->user_lastname . "\n";
$message .= "\n";
$message .= "\n";
$message .= 'CHANGES: ' . $shipping_address . "\n";
// make sure we have all of the required data
if ( empty ( $email ) ) {
return;
}
// send email
wp_mail( $email, $subject, $message );
}
add_action( 'woocommerce_customer_save_address', 'patricks_email_customer_address', 20 );
function patricks_get_formatted_shipping_address() {
global $woocommerce;
// get the data from the profile after it's set by the customer save
$curr_user = wp_get_current_user();
$user_id = $curr_user->ID;
//$woocommerce->customer->set_default_data();
//Shipping Address
$address .= "\n";
$address .= "SHIPPING:\n";
$address .= get_user_meta( $user_id, 'shipping_address_1', true );//$woocommerce->customer->get_shipping_address();
$address .= "\n";
if(!empty(get_user_meta( $user_id, 'shipping_address_2', true ))){
$address .= get_user_meta( $user_id, 'shipping_address_2', true );//$woocommerce->customer->get_shipping_address_2();
$address .= "\n";
};
$address .= get_user_meta( $user_id, 'shipping_city', true );//$woocommerce->customer->get_shipping_city();
$address .= " ";
$address .= get_user_meta( $user_id, 'shipping_state', true );//$woocommerce->customer->get_shipping_state();
$address .= ", ";
$address .= get_user_meta( $user_id, 'shipping_postcode', true );//$woocommerce->customer->get_shipping_postcode();
$address .= "\n";
$address .= get_user_meta( $user_id, 'shipping_country', true );//$woocommerce->customer->get_shipping_country();
$address .= "\n";
$address .= get_user_meta( $user_id, 'shipping_company', true );
$address .= "\n";
$address .= get_user_meta( $user_id, 'shipping_phone', true );
$address .= "\n";
$address .= get_user_meta( $user_id, 'shipping_email', true );
$address .= "\n";
//billing Address
$address .= "\n";
$address .= "BILLING:\n";
$address .= get_user_meta( $user_id, 'billing_address_1', true );//$woocommerce->customer->get_billing_address();
$address .= "\n";
if(!empty(get_user_meta( $user_id, 'billing_address_2', true ))){
$address .= get_user_meta( $user_id, 'billing_address_2', true );//$woocommerce->customer->get_billing_address_2();
$address .= "\n";
};
$address .= get_user_meta( $user_id, 'billing_city', true );//$woocommerce->customer->get_billing_city();
$address .= " ";
$address .= get_user_meta( $user_id, 'billing_state', true );//$woocommerce->customer->get_billing_state();
$address .= ", ";
$address .= get_user_meta( $user_id, 'billing_postcode', true );//$woocommerce->customer->get_billing_postcode();
$address .= "\n";
$address .= get_user_meta( $user_id, 'billing_country', true );//$woocommerce->customer->get_billing_country();
$address .= "\n";
$address .= get_user_meta( $user_id, 'billing_company', true );
$address .= "\n";
$address .= get_user_meta( $user_id, 'billing_phone', true );
$address .= "\n";
$address .= get_user_meta( $user_id, 'billing_email', true );
return $address;
}
// That's all folks!`
I added this in order to evaluate if there is made any changes - feel free to copy
/*** add hidden field to frontend edit billing address form */
function my_custom_woocommerce_before_edit_address_form_billing(){
echo '';
}
add_action( 'woocommerce_before_edit_address_form_billing', 'my_custom_woocommerce_before_edit_address_form_billing' );
and then you can simply replace
if ( empty ( $email ) ) {
return;
}
with
if ( empty ( $email ) || $shipping_address == $_POST['$old_shipping_address'] ) {
return;
}
not tested
I need a solution like this - but when customer change address in checkout - please let me know if you know how I can do that
This is great, but it sends email when either billing or shipping addresses are changed. How can we restrict it only to shipping ?
Would also be good to show the timestamp of the changes. How to get the timestamp?