WooCommerce |设置计费字段值 [英] WooCommerce | Set billing field value

查看:29
本文介绍了WooCommerce |设置计费字段值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我尝试了以下代码:

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.

此外,WooCommerce 文档(第 1 课)没有说明数组值 'default' 的任何内容

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

推荐答案

你已经很接近了.这对我有用.我不知道是否有必要,但我使用了一个命名函数,如果用户存在,则只获取 user_firstname 属性.

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

这篇关于WooCommerce |设置计费字段值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆