[wp-trac] [WordPress Trac] #50407: how to edit and save custom address fields for Woocomerce My Account/Edit-address/billing page?

WordPress Trac noreply at wordpress.org
Wed Jun 17 08:20:43 UTC 2020


#50407: how to edit and save custom address fields for Woocomerce My Account/Edit-
address/billing page?
---------------------------+-----------------------------
 Reporter:  ponnarasiaccu  |      Owner:  (none)
     Type:  defect (bug)   |     Status:  new
 Priority:  normal         |  Milestone:  Awaiting Review
Component:  General        |    Version:
 Severity:  normal         |   Keywords:
  Focuses:                 |
---------------------------+-----------------------------
 for showing custom fields on myaccount/edit-address/billing page
 {{{
 add_action( 'woocommerce_after_edit_account_address_form',
 'action_woocommerce_after_edit_account_address_form', 10, 1 );
 function action_woocommerce_after_edit_account_address_form( $array ){ ?>

 <div class="u-column1 col-1 woocommerce-Address">
         <header class="woocommerce-Address-title title">
     <h3>Order Placed Details</h3>
 </header>
     <?php
     $current_user_id = get_current_user_id();
     $order_state_id =  get_user_meta( $current_user_id,
 'billing_custom_state', true );
     global $wpdb;
     $order_State = $wpdb->get_results( "select * from wp_posts where
 ID='".$order_state_id."' order by ID desc");
     foreach ($order_State as $order_State_value)
     {
         $order_billing_state = $order_State_value->post_title;
     }
     //get location value from database start
     $order_location_id =  get_user_meta( $current_user_id,
 'billing_custom_location', true );
     global $wpdb;
     $order_location = $wpdb->get_results( $wpdb->prepare( "select * from
 wp_postmeta where meta_id='".$order_location_id."' " ) );
     if ( $order_location )
     {
         foreach ($order_location as $order_location_value)
         {
             //replace email id from address start
             //test string for checking email
             $test_patt
 ="/(?:[a-z0-9!#$%&'*+=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+=?^_`{|}~-]+)*|\"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*\")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])/";
             $replacement = "";
             $order_billing_location = preg_replace($test_patt,
 $replacement, $order_location_value->meta_value);
         }
     }
     ?>
     <address>
         State: <?php echo $order_billing_state ?><br>
         City:  <?php echo get_user_meta( $current_user_id,
 'billing_custom_city', true ); ?><br>
         Location: <?php echo $order_billing_location; ?><br>
   </address>

 </div>
 <?php }
 }}}


 for update custom address field into database

 {{{
 /**
  * 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 );
     $order_city_id =  get_user_meta( $user_id, 'billing_custom_city', true
 );
     $order_location_id =  get_user_meta( $user_id,
 'billing_custom_location', true );

     update_user_meta( $user_id, 'billing_custom_state',
 esc_attr($_POST['billing_custom_state']), $order_state_id );
     update_user_meta( $user_id, 'billing_custom_city',
 esc_attr($_POST['billing_custom_city']), $order_city_id );
     update_user_meta( $user_id, 'billing_custom_location',
 esc_attr($_POST['billing_custom_location']), $order_location_id );

     //update_post_meta( $post_id, '_billing_custom_state',
 esc_attr($_POST['billing_custom_state']), $order_state_id );*/

     $current_user_id = get_current_user_id();
     $customer_meta  = get_user_meta( $current_user_id );
     $customer_orders = get_posts( array(
         'numberposts' => -1,
         'meta_key'    => '_customer_user',
         'meta_value'  => $current_user_id,
         'post_type'   => wc_get_order_types(), //shop_order
         'post_status' => array_keys( wc_get_order_statuses() ) //wc-
 processing
     ) );

     foreach( $customer_orders as $order ) {

         update_post_meta( $order->ID, '_billing_custom_state',
 esc_attr($_POST['billing_custom_state']),
 $customer_meta['billing_custom_state'][0] );

         update_post_meta( $order->ID, '_billing_custom_city',
 esc_attr($_POST['billing_custom_city']),
 $customer_meta['billing_custom_city'][0] );

         update_post_meta( $order->ID, '_billing_custom_location',
 esc_attr($_POST['billing_custom_location']),
 $customer_meta['billing_custom_location'][0] );

     }


 }
 }}}

 using above hook i cant able to update the value kindly review my code and
 support me.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/50407>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list