2

In WooCommerce I am adding a custom billing field at the end of the checkout billing fields section, with the code below:

add_filter('woocommerce_checkout_fields', 'custom_woocommerce_billing_fields');

function custom_woocommerce_billing_fields($fields)
{
         $fields['billing']['billing_options'] = array(
        'label' => __('תאריך לידה', 'woocommerce'), // Add custom field label
        'placeholder' => _x('תאריך לידה'), // Add custom field placeholder
        'required' => true, // if field is required or not
        'clear' => false, // add clear or not
        'type' => 'date', // add field type
        'class' => array('my-css')   // add class name
    );

    return $fields;
}

How can I add this field after the first name last name field or after company field?

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
user10498931
  • 25
  • 1
  • 5

1 Answers1

4

You need to use the "priority" argument, which will allow you to set your field in the correct location (after the first and last name fields).

Normally "billing first name" has a 10 as priority and "billing last name 20 as priority. Then comes the "billing company" that has 30 as priority… So for your custom billing field use a priority of 25 (in between).

There is a little mistake in your code for the placeholder where you should replace _x() function by __().

Your code is going to be:

add_filter('woocommerce_checkout_fields', 'custom_woocommerce_billing_fields');
function custom_woocommerce_billing_fields( $fields )
{
    $fields['billing']['billing_options'] = array(
        'label'       => __('תאריך לידה', 'woocommerce'), // Add custom field label
        'placeholder' => __('תאריך לידה', 'woocommerce'), // Add custom field placeholder
        'required'    => true, // if field is required or not
        'clear'       => false, // add clear or not
        'type'        => 'date', // add field type
        'class'       => array('my-css'),   // add class name
        'priority'    => 25, // Priority sorting option
    );

    return $fields;
}

Code goes in functions.php file of your active child theme (or active theme). Tested and works.

If you want this field after the billing company, you will use a priority of 35 instead.

Related: Reordering checkout fields in WooCommerce 3

7uc1f3r
  • 28,449
  • 17
  • 32
  • 50
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399