Forked from zgordon/update-woo-order-billing-when-user-saves-address.php
Created
May 12, 2021 18:46
-
-
Save bhubbard/8c297b88e913b7e8f3e176c0d1a4b5d9 to your computer and use it in GitHub Desktop.
Update WooCommerce Order Address Dynamically When Customer Updates Their Address
This file contains hidden or 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 | |
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