简体   繁体   English

WooCommerce | 设置帐单字段值

[英]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' 另外, WooCommerce文档 (第1课)也没有提及数组值“ 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. 我不知道是否有必要,但是我使用了一个命名函数,并且只有在用户存在的情况下才获取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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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