pin*_*aby 2 php country checkout readonly woocommerce
我已经将默认国家/地区设置为澳大利亚,但我希望它们为只读。
您可以使用woocommerce_form_field_args将禁用属性添加到 quntry 选择字段。
将以下代码添加到您的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;
}
Run Code Online (Sandbox Code Playgroud)
当我们禁用 select 下拉菜单时的问题,当您单击下订单时,选项值不会传递,为了解决此问题,我们可以添加具有所需值的隐藏字段,如下所示:
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">';
}
Run Code Online (Sandbox Code Playgroud)
只需将 更改value="PL"为您的国家/地区代码值,一切都会按预期进行。
输出 :
代码使用 StorrFront 主题进行测试。
Kashalo的答案是正确的……您也可以使用以下多种其他方式之一:
1)仅适用于结帐结算国家/地区:
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;
}
Run Code Online (Sandbox Code Playgroud)
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;
}
Run Code Online (Sandbox Code Playgroud)
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;
}
Run Code Online (Sandbox Code Playgroud)
4)对于Checkout和“我的帐户”的帐单和运送国家/地区:
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;
}
Run Code Online (Sandbox Code Playgroud)