当WooCommerce checkout 页面加载时,一些字段(如First_Name、Last_Name、State)被预先填充.

但其他字段(电话、城市、邮政编码)为空.

所有签出字段的数据都在用户元数据中可用.

如何使用用户元数据填充检出字段? 我try 了一些类似的代码,将其添加到我的函数.php文件中.

有什么建议吗? 向您致敬,

add_filter( 'woocommerce_checkout_fields', 'itdoc_remove_fields', 9999 );


function itdoc_remove_fields( $woo_checkout_fields_array ) {
    
    $user = wp_get_current_user();
    $dealer_phone= get_user_meta($user->ID, 'phone' , true);
    $dealer_city= get_user_meta($user->ID, 'dealer_city' , true);
    $dealer_state= get_user_meta($user->ID, 'postcode' , true);
    print($dealer_phone);

    $woo_checkout_fields_array['billing']['billing_phone']['default'] = $dealer_phone ;
    $woo_checkout_fields_array['billing']['billing_city']['default'] = $dealer_city;
    $woo_checkout_fields_array['billing']['billing_postcode']['default'] = $dealer_postcode;
    var_dump($woo_checkout_fields_array);


    return $woo_checkout_fields_array;
}

//这对签出字段没有任何影响

推荐答案

您没有使用右挂钩,请try 执行以下操作:

add_filter( 'woocommerce_checkout_get_value', 'autofill_some_checkout_fields', 10, 2 );
function autofill_some_checkout_fields( $value, $input ) {
    $user = wp_get_current_user();
    
    if( $input === 'billing_phone' && empty($value) && isset($user->phone) ) {
        $value = $user->phone;
    }
    
    if( $input === 'billing_city' && empty($value) && isset($user->dealer_city) ) {
        $value = $user->dealer_city;
    }
    
    if( $input === 'billing_postcode' && empty($value) && isset($user->postcode)  ) {
        $value = $user->postcode;
    }
    return $value;
}

应该能行得通.

Php相关问答推荐

文件大小在php下载读取文件中不可见

Symfony Monolog:使用多个格式化程序

在WooCommerce Checkout帐单地址字段后插入自定义按钮

在WooCommerce管理订单页面中显示区域名称

Htaccess-重写对&api.php";的API请求以及对";web.php";的其他请求

如何修复警告:当使用WordPress get_the_Terms()时,Foreach()参数必须是ARRAY|OBJECT类型?

目标类[Set_Locale]不存在.拉威尔

将下拉 Select 的值从WooCommerce后端保存并显示到前端

在PHP 8.2 Gloogle云引擎上加载大型php文件速度缓慢

文件::Files()响应返回空对象数组

Woocommerce 临时购物车税未根据第一个请求正确计算

根据小计向 WooCommerce Checkout 添加佣金

.htaccess 重写规则问题

如何在 PHP laravel 中将图像的透明背景转换为白色?

在shopware 6.5上可以使用什么事件来判断刚刚支付的订单的状态

如何搜索和替换 XML 文件中的一段文本?

Woocommerce注册中的自定义复选框验证错误提示问题

Laravel:ArgumentCountError: 函数 App\Mail 的参数太少

如何使用动态变量定义 bind_result?

根据数组中的文本文件编号显示星级