繁体   English   中英

WooCommerce | 设置帐单字段值

[英]WooCommerce | Set billing field value

我想在用户首次购买商品之前 ,将结帐帐单字段的值预填充到用户的数据库存储值中。

我尝试了以下代码:

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

我已经在一篇文章中阅读了有关此解决方案的信息 占位符效果很好,但价值不高。

另外, WooCommerce文档 (第1课)也没有提及数组值“ default”的任何内容

你很亲密 这对我有用。 我不知道是否有必要,但是我使用了一个命名函数,并且只有在用户存在的情况下才获取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