Search code examples
wordpresswoocommercehook-woocommercemeta-key

Woocommerce : How to add a custom meta_key to checkout billing_phone?


I want to enter the value of digits_phone meta key to be entered as billing_phone for every woocommerce order.

I came up with something like this but it did not work :

//Automatically add the digits phone number of the user, to woocommerce orders for every order

add_filter('woocommerce_checkout_posted_data', 'dg_manipulate_checkout_posted_data');
function dg_manipulate_checkout_posted_data ($data) {
    $data['billing_phone'] =['digits_phone'];

    return $data;
}

can anyone please help me to figure this out?


Solution

    • I have never used the plugin myself, take this as a guideline

      THIS CODE IS NOT TESTED !

    • Maybe this question is a better fit for wordpress.stackexchange.com

    From the last comment of this post I see that there should be 2 user metadata related to some digits_phone: 'digits_phone' and 'digits_phone_no'

    Assuming that the one we want is digits_phone, this code should be a hint in the right direction:

    add_filter('woocommerce_checkout_posted_data', 'dg_manipulate_checkout_posted_data'); 
    
    function dg_manipulate_checkout_posted_data ($data) {
      // save current user data in variable $current_user
      $current_user = wp_get_current_user();
    
      //get digits phone from db and save in variable $digits_phone
      $digits_phone = get_user_meta( $current_user->ID, 'digits_phone' , true );     
    
      // assign to POSTed array and return it
      $data['billing_phone'] = $digits_phone;
      return $data; 
    }
    

    Also have a look at How can I convert woocommerce checkout fields in capital letters to get a better picture of manipulating POST data