WooCommerce | Set billing field value

I want to pre-populate the values ​​for the billing fields for the database of the user's stored values before his first purchase .

I tried the following code:

add_filter( 'woocommerce_checkout_fields' , function ( $fields ) {
    $fields['billing']['billing_first_name']['placeholder'] = 'First Name';
    $fields['billing']['billing_first_name']['default'] = wp_get_current_user()->user_firstname;
    return $fields;
});

I read about this solution in another post . Placeholder works great, but it doesn't.

In addition, the WooCommerce Doc (Lesson 1) says nothing about the value of the 'default' array

+4
source share
1 answer

. . , , user_firstname, .

add_filter( 'woocommerce_checkout_fields' , 'kia_checkout_field_defaults', 20 );
function kia_checkout_field_defaults( $fields ) {
    $user = wp_get_current_user();
    $first_name = $user ? $user->user_firstname : '';
    $fields['billing']['billing_first_name']['placeholder'] = 'First Name';
    $fields['billing']['billing_first_name']['default'] = $first_name;
    return $fields;
}
+3

Source: https://habr.com/ru/post/1657092/


All Articles