加载 woocommerce 结账页面时,会预先填充一些字段(如名字、姓氏、州)。
但其他字段(电话、城市、邮政编码)为空。
所有结账字段的数据都可以在用户元数据中找到。
如何使用用户元数据填写结帐字段? 我尝试了一些这样的代码,将其添加到我的functions.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;
}
// 这对结帐字段没有任何影响
您没有使用右钩子,请尝试以下操作:
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;
}
应该可以。