add_filter('woocommerce_checkout_fields', 'readdonly_billing_country_select_field');
function readdonly_billing_country_select_field( $fields ) {
// Set billing and shipping country to AU
WC()->customer->set_billing_country('AU');
// Make billing country field read only
$fields['billing']['billing_country']['custom_attributes'] = array( 'disabled' => 'disabled' );
return $fields;
}
2) 仅适用于结账和我的账户账单国家:
add_filter('woocommerce_billing_fields', 'readdonly_billing_country_select_field');
function readdonly_billing_country_select_field( $fields ) {
// Set billing and shipping country to AU
WC()->customer->set_billing_country('AU');
// Make billing country field read only
$fields['billing_country']['custom_attributes'] = array( 'disabled' => 'disabled' );
return $fields;
}
3. 结账时填写账单和配送国家:
add_filter('woocommerce_checkout_fields', 'readdonly_country_select_field');
function readdonly_country_select_field( $fields ) {
// Set billing and shipping country to AU
WC()->customer->set_billing_country('AU');
WC()->customer->set_shipping_country('AU');
// Make billing and shipping country field read only
$fields['billing']['billing_country']['custom_attributes'] = array( 'disabled' => 'disabled' );
$fields['shipping']['shipping_country']['custom_attributes'] = array( 'disabled' => 'disabled' );
return $fields;
}
4) 对于结账和我的账户中的账单和送货国家:
add_filter('woocommerce_default_address_fields', 'readdonly_country_select_field');
function readdonly_country_select_field( $fields ) {
// Set billing and shipping country to AU
WC()->customer->set_billing_country('AU');
WC()->customer->set_shipping_country('AU');
// Make country field read only
$fields['country']['custom_attributes'] = array( 'disabled' => 'disabled' );
return $fields;
}
woocommerce_form_field_args
将禁用属性添加到国家选择字段。functions.php
中,即可获得所需结果。add_action('woocommerce_form_field_args', 'disable_country_dropdown', 10, 3);
function disable_country_dropdown($args, $key, $value)
{
if ($key == 'billing_country') {
$args['custom_attributes'] = [
'disabled' => 'disabled',
];
}
return $args;
}
当我们将下拉选择禁用时,如果您单击“下订单”按钮,则将不会传递选项值。为了解决这个问题,我们可以添加一个隐藏字段并设置所需的值:
add_action('woocommerce_after_order_notes', 'billing_country_hidden_field');
function billing_country_hidden_field($checkout)
{
echo '<input type="hidden" class="input-hidden" name="billing_country" value="PL">';
}
只需要将value="PL"
更改为您的国家代码值,一切就会按预期工作。
输出:
此代码已与StorrFront主题测试。
/*CHECKOUT BILLING ADDRESS READ ONLY*/
add_action('woocommerce_checkout_fields','customization_readonly_billing_fields',10,1);
function customization_readonly_billing_fields($checkout_fields){
$current_user = wp_get_current_user();;
$user_id = $current_user->ID;
foreach ( $checkout_fields['billing'] as $key => $field ){
if($key == 'billing_company' || $key == 'billing_address_1' || $key == 'billing_address_2' || $key == 'billing_city' || $key == 'billing_postcode' || $key == 'billing_phone' || $key == 'invoice_email' || $key == 'purchase_order' || $key == 'ship_to_first_name' || $key == 'ship_to_last_name'){
$key_value = get_user_meta($user_id, $key, true);
if( strlen($key_value)>0){
$checkout_fields['billing'][$key]['custom_attributes'] = array('readonly'=>'readonly');
}
}
}
return $checkout_fields;
}
if ( is_user_logged_in() ) {
// your code for logged in user
add_filter('woocommerce_checkout_fields', 'readdonly_billing_country_select_field');
function readdonly_billing_country_select_field( $fields ) {
$fields['billing']['billing_country']['custom_attributes'] = array( 'disabled' => 'disabled' );
return $fields;
}
add_filter('woocommerce_checkout_fields', 'readdonly_billing_state_select_field');
function readdonly_billing_state_select_field( $fields ) {
$fields['billing']['billing_state']['custom_attributes'] = array( 'disabled' => 'disabled' );
return $fields;
}
} else {
// your code for logged out user
}
kashalo
的答案之外,我是否需要添加其他内容来修复这个问题? - melvin