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.

Revisions

  1. @zgordon zgordon revised this gist Jun 7, 2017. 1 changed file with 17 additions and 16 deletions.
    33 changes: 17 additions & 16 deletions update-woo-order-billing-when-user-saves-address.php
    Original file line number Diff line number Diff line change
    @@ -1,8 +1,10 @@
    <?php

    function custom_update_address_for_orders( $user_id ) {
    add_action( 'woocommerce_customer_save_address', 'jsforwp_update_address_for_orders', 10, 2 );

    $customer_meta = get_user_meta( $user_id );
    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',
    @@ -11,23 +13,22 @@ function custom_update_address_for_orders( $user_id ) {
    'post_status' => array_keys( wc_get_order_statuses() )
    ) );

    foreach( $customer_orders as $order ) {
    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] );

    }
    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] );

    }

    };
    add_action( 'woocommerce_customer_save_address', 'custom_update_address_for_orders', 10, 2 );

    ?>
  2. @zgordon zgordon created this gist Jun 7, 2017.
    33 changes: 33 additions & 0 deletions update-woo-order-billing-when-user-saves-address.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    <?php

    function custom_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] );

    }

    };
    add_action( 'woocommerce_customer_save_address', 'custom_update_address_for_orders', 10, 2 );

    ?>