0

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?

Erfan
  • 23
  • 8
  • Hi, :) where are you getting digits_phone from ? assuming you're using a plugin to manage digits numbers, I guess you should be able to get the data with a call to `get_user_meta( $current_user->ID, 'digits_phone' , true );` ... – giulp Aug 07 '21 at 06:22
  • Hi @giulp. yes digits_phone is managed by the digits plugin. so how should I edit the code to actually get `digits_phone` from user meta and put it to `billing_phone` of woocommerce order? does it have to be something like this? `add_filter('woocommerce_checkout_posted_data', 'dg_manipulate_checkout_posted_data'); function dg_manipulate_checkout_posted_data ($data) { $data['billing_phone'] = get_user_meta( $current_user->ID, 'digits_phone' , true );; return $data; }` – Erfan Aug 07 '21 at 14:34
  • You need to get current user data with `$current_user = wp_get_current_user();` – giulp Aug 07 '21 at 18:44
  • thanks for your help. actually I'm a newbie and dont really know how to use these codes altogether. can you please somehow combine the code that you sent, with the initial code that I asked in this question as an answer or correct it? – Erfan Aug 08 '21 at 14:30

1 Answers1

2
  • 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

giulp
  • 2,200
  • 20
  • 26