Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save EmranAhmed/b39511f6074e046be4a09669faa5915d to your computer and use it in GitHub Desktop.
Save EmranAhmed/b39511f6074e046be4a09669faa5915d 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] );
}
};
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment