【问题标题】:WooCommerce | Set billing field valueWooCommerce |设置计费字段值
【发布时间】:2017-02-16 18:40:18
【问题描述】:

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

我尝试了以下代码:

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

我在other post 中阅读了有关此解决方案的信息。占位符效果很好,但值却不行。

另外,WooCommerce Doc(第 1 课)没有说明数组值“默认”的任何内容

【问题讨论】:

  • 你能展示更多你正在使用的代码吗?
  • @helgatheviking 我刚刚更新了帖子

标签: php wordpress woocommerce


【解决方案1】:

你已经很接近了。这对我有用。我不知道是否有必要,但我使用了一个命名函数,并且只有在用户存在时才获得 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;
}

【讨论】:

  • 谢谢 Helga,我已将优先级 20 添加到我的代码中,并且成功了!
猜你喜欢
  • 1970-01-01
  • 2016-07-15
  • 2018-01-18
  • 1970-01-01
  • 1970-01-01
  • 2019-02-14
  • 1970-01-01
  • 2019-05-01
  • 2018-01-30
相关资源
最近更新 更多