0
votes

I want to add a field from user account into checkout fields in woocommerce. I have already a field "EU VAT Number" with its values for each user in account. I want to display this field and its value after billing details on checkout page.

I created a filter, but it displayw only the Field label with no value. Below is my code

// Display a custom field on checkout and on My account > edit 
billing address

add_filter( 'woocommerce_billing_fields' , 
'adding_billing_eu_vat_number', 20, 1 );
$vat = get_field('billing_eu_vat_number', 
      'user_'.get_current_user_id());

if( $vat = get_field( 'billing_eu_vat_number',  
   'user_'.get_current_user_id() ) ) { echo "<h3 
   style='margin:0;font-weight: 600;'>" . 'EU VAT  NUMBER: '  .  
   $vat . '</h3>' ; }


function adding_billing_eu_vat_number ( $fields ) {
 $fields['eu_vat_number'] = array(
    'label'       => __('EU VAT Number', 'woocommerce'),
    'placeholder' => _x($vat, 'placeholder', 'woocommerce'),
    'class'       => array('form-row-wide'),
    'required'    => true,
    'clear'       => true,
 );

 return $fields;
}

I need each customer not to insert each time he order the eu vat number and to be already inserted. Thank you in advance!

1

1 Answers

0
votes

You need to give it a default value, Try this

add_filter( 'woocommerce_billing_fields' , 'adding_billing_eu_vat_number', 20, 1 );

function adding_billing_eu_vat_number ( $fields ) {
  $eu_vat_number = get_field('billing_eu_vat_number', 'user_'.get_current_user_id());
  $fields['eu_vat_number'] = array(
    'label'       => __('EU VAT Number', 'woocommerce'),
    'placeholder' => _x($vat, 'placeholder', 'woocommerce'),
    'class'       => array('form-row-wide'),
    'required'    => true,
    'clear'       => true,
    'default'     => $eu_vat_number;
  );

 return $fields;
}