Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save zgordon/8375ebd45a691ebc2aef6a5aa643199e to your computer and use it in GitHub Desktop.
Save zgordon/8375ebd45a691ebc2aef6a5aa643199e to your computer and use it in GitHub Desktop.
Update WooCommerce Order Address Dynamically When Customer Updates Their Address
<?php
add_action( 'woocommerce_customer_save_address', 'jsforwp_update_address_for_orders', 10, 2 );
function jsforwp_update_address_for_orders( $user_id ) {
$customer_meta = get_user_meta( $user_id );
$customer_orders = get_posts( array(
'numberposts' => -1,
'meta_key' => '_customer_user',
'meta_value' => $user_id,
'post_type' => wc_get_order_types(),
'post_status' => array_keys( wc_get_order_statuses() )
) );
foreach( $customer_orders as $order ) {
update_post_meta( $order->ID, '_billing_first_name', $customer_meta['billing_first_name'][0] );
update_post_meta( $order->ID, '_billing_last_name', $customer_meta['billing_last_name'][0] );
update_post_meta( $order->ID, '_billing_company', $customer_meta['billing_company'][0] );
update_post_meta( $order->ID, '_billing_address_1', $customer_meta['billing_address_1'][0] );
update_post_meta( $order->ID, '_billing_address_2', $customer_meta['billing_address_2'][0] );
update_post_meta( $order->ID, '_billing_city', $customer_meta['billing_city'][0] );
update_post_meta( $order->ID, '_billing_state', $customer_meta['billing_state'][0] );
update_post_meta( $order->ID, '_billing_postcode', $customer_meta['billing_postcode'][0] );
update_post_meta( $order->ID, '_billing_country', $customer_meta['billing_country'][0] );
update_post_meta( $order->ID, '_billing_email', $customer_meta['billing_email'][0] );
update_post_meta( $order->ID, '_billing_phone', $customer_meta['billing_phone'][0] );
}
};
?>
@ponnnarasi14
Copy link

i try this method but its not working for me.

`/**

  • Update the user meta with field value
    **/
    add_action( 'woocommerce_customer_save_address', 'action_woocommerce_customer_save_address', 10, 2 );

//$load_address = 'billing';

function action_woocommerce_customer_save_address($user_id, $load_address) {

/*$order_state_id =  get_user_meta( $user_id, 'billing_custom_state', true );

update_user_meta( $user_id, 'billing_custom_state', esc_attr($_POST['billing_custom_state']), $order_state_id );*/

$customer_meta = get_user_meta( $user_id );
$customer_orders = get_posts( array(
    'numberposts' => -1,
    'meta_key'    => '_customer_user',
    'meta_value'  => $user_id,
    'post_type'   => wc_get_order_types(),
    'post_status' => array_keys( wc_get_order_statuses() )
) );

foreach( $customer_orders as $order ) {

    update_post_meta( $order->ID, '_billing_custom_state', $customer_meta['billing_custom_state'][0] );
    update_post_meta( $order->ID, '_billing_custom_city', $customer_meta['billing_custom_city'][0] );
    update_post_meta( $order->ID, '_billing_custom_location', $customer_meta['billing_custom_location'][0] );
 
}

}`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment