问题
I want country dropdown on woocommerce as readonly.
I already set the default country to australia but I want them to be readonly.
回答1:
The answer of Kashalo is correct… You can also use one of this multiple other ways:
1) For Checkout Billing country only:
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) For Checkout and My account Billing country only:
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 For Checkout billing and shipping country:
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) For Checkout and My account billing and shipping country:
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;
}
回答2:
you can use woocommerce_form_field_args
to add disabled attribute to the quntry select field.
add the following code to your functions.php
and you will get the desired result.
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;
}
the issue when we puted the select drowpdown disabled the option value is not passed when you click place order and in order to solve this issue we can add hidden field with our desired value as follow:
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">';
}
just change the value="PL"
to your country code value and everything will work as expected.
OutPut :
Code is tested with StorrFront Theme.
来源:https://stackoverflow.com/questions/52279966/make-checkout-country-dropdown-readonly-in-woocommerce