1

I'm looking to add three custom date fields using select. So the first one would be the day, second would be month and the third would be the year. However, I'm not sure how to add a code without manually entering all years pluss to have three fields count as one.

I have a code I used to add a custom field 'gender' any way to something similar for a dob select fields described above.

add_action('woocommerce_before_checkout_billing_form', 'wps_add_select_checkout_field');

function wps_add_select_checkout_field( $checkout ) {

woocommerce_form_field( 'apgen', array(
    'type'          => 'select',
    'class'         => array( 'ap-drop' ),
    'label'         => __( 'Gender' ),
    'options'       => array(
        'blank'     => __( 'Select Gender', 'ap' ),
        'male'  => __( 'Male', 'ap' ),
        'Female'    => __( 'Female', 'ap' ),
        'non-binary'    => __( 'Non-binary', 'ap' )
    )
),
$checkout->get_value( 'apgen' ));
}
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Max
  • 35
  • 1
  • 5

1 Answers1

0

Woocommerce has now a "date" field type with a very specific behavior, that has inside its field, 3 selectable number field type (days, month and year) that you can select individually, with a showable date picker (see screenshots below):

add_filter('woocommerce_checkout_fields', 'wps_add_checkout_field');
function wps_add_checkout_field( $fields ) {


    // Select field (Gender)
    $fields['billing']['billing_gender'] = array(
        'type'     => 'select',
        'class'    => array( 'form-row-wide' ),
        'label'    => __( 'Gender' ),
        'required' => true,
        'priority' => 3,
        'options'  => array(
            ''           => __( 'Select Gender', 'ap' ),
            'male'       => __( 'Male', 'ap' ),
            'Female'     => __( 'Female', 'ap' ),
            'non-binary' => __( 'Non-binary', 'ap' )
        ),
    );

    // Date field (with 3 number fields with a datepicker)
    $fields['billing']['billing_date'] = array(
        'type'     => 'date',
        'class'    => array( 'form-row-wide' ),
        'label'    => __( 'Date' ),
        'required' => true,
        'priority' => 3,
    );

    return $fields;
}

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

You will get something like:

enter image description here

Rolling in the date field:

enter image description here

Selecting days and using enter image description here to increase or decrease days:

enter image description here

Selecting months and increasing value (or typing a value):

enter image description here

Selecting years and increasing value (or typing a value):

enter image description here

Making appear the date picker enter image description here and select a date:

enter image description here


Other similar hooks that can be used:

add_filter('woocommerce_billing_fields', 'wps_add_date_type_checkout_field');

function wps_add_date_type_checkout_field( $fields ) {
// Date field (with 3 number fields with a datepicker)
    $fields['billing_date'] = array(
        'type'          => 'date',
        'class'         => array( 'form-row-wide' ),
        'label'         => __( 'Date' ),
        'priority'      => 5,
    );
    return $fields;
}

Or

add_action('woocommerce_before_checkout_billing_form', 'wps_add_date_type_checkout_field');

function wps_add_date_type_checkout_field( $checkout ) {

    woocommerce_form_field( 'billing_date', array(
        'type'          => 'date',
        'class'         => array( 'form-row-wide' ),
        'label'         => __( 'Gender' ),
    ), $checkout->get_value( 'billing_date' ));

}

Or before (or after) order notes:

add_action('woocommerce_before_order_notes', 'wps_add_date_type_checkout_field');
// add_action('woocommerce_after_order_notes', 'wps_add_date_type_checkout_field');

function wps_add_date_type_checkout_field( $checkout ) {
    echo '<div id="my_custom_checkout_field">';

    woocommerce_form_field( 'billing_date', array(
        'type'          => 'date',
        'class'        => array('my-field-class form-row-wide'),
        'label'        => __('Date'),
    ), $checkout->get_value( 'billing_date' ));

    echo '</div>';
}

Related docs: Customizing Woocommerce checkout fields using actions and filters

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Thank you for the answer! However I have entered the code above and only a text field has shown with no 3 selectable number field types displaying. – Max Aug 28 '18 at 08:57
  • @Max Which version of woocommerce are you using? – LoicTheAztec Aug 28 '18 at 09:36
  • I'm using 3.4.4 @loic – Max Aug 28 '18 at 10:00
  • @Max It seems that you get something making trouble… It could be your theme or something else… Since Woocommerce 3.2 or 3.3, the date field work as described in this answer. I have made a light code update, changing the function hook, but I don't think that it will change anything for you. You can see this in action [HERE on my test server](https://www.cbleu.net/sites/wc34/shop/) ==> Add a product and go to checkout, you will see yourself. I have tested this with different themes and it works the same way. – LoicTheAztec Aug 28 '18 at 10:24
  • I believe it may be due to me viewing on an old version of safari as i have looked at your test site and i'm getting the same result as mine. – Max Aug 28 '18 at 10:51
  • @Max Yes I confirm it doesn't work on old safari versions (tested on version 9.1.3).You should better use Chrome or firefox for web design, web development and testing. – LoicTheAztec Aug 28 '18 at 11:04
  • Just tested it on another browser and it works! Thanks Loic it's just what I wanted! Do you know the code to add it to the order notes and for it to work with the error message? – Max Aug 28 '18 at 11:12
  • @Max I have added a priority argument in the arguments array just now, so this date field will come at first… There is many different hooks that can be used. see: [Customizing checkout fields using actions and filters](https://docs.woocommerce.com/document/tutorial-customising-checkout-fields-using-actions-and-filters/) – LoicTheAztec Aug 28 '18 at 11:49
  • @Max I have added all possible alternatives at the end… – LoicTheAztec Aug 28 '18 at 11:59
  • Ah I see. I ended up using the first code you sent me as the priority was woking correctly. Is there a way to add the priority to my gender code. I did add the priority to the code but it didn't work. I also tried to add the code that enables the dob field to come up as a filed required and to display on the order meta but had no luck @Loic – Max Aug 28 '18 at 14:09
  • @Max I have updated my code (and the screenshot of the display)… I have changed the keys slugs and now the data is saved in the order meta data too. You can check that within your database under the table wp_postmeta for the order ID (post_id) – LoicTheAztec Aug 29 '18 at 16:24
  • Great thank you so much for this. So this will now show in the customers order area? Also what action do I add above the code snippet you have provided? – Max Aug 31 '18 at 08:48
  • @Max Sorry I have made a little mistake and updated again. The data is s saved in database. Now to make that visible on order and emails use `_billing_date` and `_billing_gender` meta keys using them like in [this answer code](https://stackoverflow.com/questions/45906636/add-text-to-order-summary-based-on-a-custom-field-value-in-woocommerce/45909802#45909802) … If you have any issue, ask a new question with the related code. – LoicTheAztec Aug 31 '18 at 09:16