首页 文章

WooCommerce |设置结算字段值

提问于
浏览
4

我想将结帐的结算字段的值预先填充到用户 before his first purchase 的DB存储值 .

我试过以下代码:

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中读到了这个解决方案 . 占位符效果很好,但 Value 不大 .

此外,WooCommerce Doc(第1课)没有't say about anything the array value '默认'

1 回答

  • 3

    你're pretty close. This worked for me. I don'知道是否有必要,但我使用了一个命名函数,只有在用户存在时才获取 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;
    }
    

相关问题