简体   繁体   中英

WooCommerce | Set billing field value

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

I've 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've read about this solution in an other post . Placeholder works great, but the value doesn't.

Also, the WooCommerce Doc (Lesson 1) doesn't say about anything the array value 'default'

You're pretty close. This worked for me. I don't know if it was necessary, but I used a named function and only get the user_firstname property if the user exists.

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM