3

I try to add a birthdate field in the Woocommerce Checkout form, then save it as a user meta. I can display it with the code below but i can't save it in order to see it in the user profile page.

 add_filter( 'woocommerce_billing_fields', 'add_birth_date_billing_field', 20, 1 );
    function add_birth_date_billing_field($billing_fields) {
    
        $billing_fields['billing_birth_date'] = array(
            'type'        => 'date',
            'label'       => __('Birth date'),
            'class'       => array('form-row-wide'),
            'priority'    => 25,
            'required'    => true,
            'clear'       => true,
        );
        return $billing_fields;
    }
       
    
         add_action( 'woocommerce_checkout_update_customer', 'save_checkout_account_birthday_field', 10, 2 );
            function save_checkout_account_birthday_field( $customer, $data ){
                if ( isset($_POST['billing_birth_date']) && ! empty($_POST['billing_birth_date']) ) {
                     $customer->update_meta_data( 'billing_birth_date', sanitize_text_field($_POST['billing_birth_date']) );
                }
            }
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
pipoulito
  • 69
  • 7

1 Answers1

5

Here is the complete code that will display billing birthdate in checkout, in My account Addresses, In admin Orders pages and in WordPress user dashboard:

// Display Billing birthdate field to checkout and My account addresses
add_filter( 'woocommerce_billing_fields', 'display_birthdate_billing_field', 20, 1 );
function display_birthdate_billing_field($billing_fields) {

    $billing_fields['billing_birthdate'] = array(
        'type'        => 'date',
        'label'       => __('Birthdate'),
        'class'       => array('form-row-wide'),
        'priority'    => 25,
        'required'    => true,
        'clear'       => true,
    );
    return $billing_fields;
}

// Save Billing birthdate field value as user meta data
add_action( 'woocommerce_checkout_update_customer', 'save_account_billing_birthdate_field', 10, 2 );
function save_account_billing_birthdate_field( $customer, $data ){
    if ( isset($_POST['billing_birthdate']) && ! empty($_POST['billing_birthdate']) ) {
         $customer->update_meta_data( 'billing_birthdate', sanitize_text_field($_POST['billing_birthdate']) );
    }
}

// Admin orders Billing birthdate editable field and display
add_filter('woocommerce_admin_billing_fields', 'admin_order_billing_birthdate_editable_field');
function admin_order_billing_birthdate_editable_field( $fields ) {
    $fields['birthdate'] = array( 'label' => __('Birthdate', 'woocommerce') );

    return $fields;
}

// WordPress User: Add Billing birthdate editable field
add_filter('woocommerce_customer_meta_fields', 'wordpress_user_account_billing_birthdate_field');
function wordpress_user_account_billing_birthdate_field( $fields ) {
    $fields['billing']['fields']['billing_birthdate'] = array(
        'label'       => __('Birthdate', 'woocommerce'),
        'description' => __('', 'woocommerce')
    );
    return $fields;
}

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

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • great thanks, as i have multiple user fileds to create should i repeat this entire code for eah one ? – pipoulito Feb 19 '21 at 08:11
  • Do you know if in the same time i can update memberhsip profile field meta with these billing fields ? – pipoulito Feb 19 '21 at 13:43
  • thanks it is Woocommerce Membership. It allow Profile Fields creation – pipoulito Feb 19 '21 at 13:53
  • is it normal to have $fields['birthdate'] and not $fields['billing_birthdate'] ? – pipoulito Feb 19 '21 at 14:35
  • Actually fields are saved but are not displayed in backend user page – pipoulito Feb 19 '21 at 14:47
  • 1
    Yes it is normal, as the related hook is already about **billing** information. I have removed a function that was making birthdate displayed twice on admin order single pages. – LoicTheAztec Feb 19 '21 at 14:48
  • 1
    @pipoulito I just tested my code completely, and it works perfectly everywhere… Field is displayed at the end of the WP user billing section. see [this screenshot](https://i.stack.imgur.com/2OvFO.png). End. – LoicTheAztec Feb 19 '21 at 14:54
  • you right it's just perfect, i had made a mistake when adding fields. I am wondering if it is possible to use a plugin to add the fields and only keep your functions to save as user data with the id fileds from the plugin ? – pipoulito Feb 19 '21 at 15:00
  • @pipoulito If you are not going to publish a plugin for others, keep it it in the child theme functions.php file or you can also use code snippet plugin (recommended by WooCommerce itself). I had to go bye. – LoicTheAztec Feb 19 '21 at 15:06
  • worked like a charm. Im looking for a way to make the admin side birthday field to a date picker & maybe change the order of display – Mellow Monk Apr 23 '23 at 00:31