Php 在Woocommerce中使签出国家/地区下拉列表为只读
我希望woocommerce上的国家下拉列表为只读Php 在Woocommerce中使签出国家/地区下拉列表为只读,php,woocommerce,checkout,readonly,country,Php,Woocommerce,Checkout,Readonly,Country,我希望woocommerce上的国家下拉列表为只读 我已将默认国家/地区设置为澳大利亚,但我希望它们为只读。您可以使用woocommerce\u form\u field\u args将禁用属性添加到quntry select字段 将以下代码添加到functions.php中,您将获得所需的结果 add_action('woocommerce_form_field_args', 'disable_country_dropdown', 10, 3); function disable_coun
我已将默认国家/地区设置为澳大利亚,但我希望它们为只读。您可以使用
woocommerce\u form\u field\u args
将禁用属性添加到quntry select字段
将以下代码添加到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;
}
当我们禁用select drowpdown时,当您单击下订单时,选项值不会传递,为了解决此问题,我们可以添加具有所需值的隐藏字段,如下所示:
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">';
}
add_action('u order_notes'后面的woocommerce_,'billing_country_hidden_field');
功能计费\国家\隐藏\字段($checkout)
{
回声';
}
只需将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;
}
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;
}
2) 仅适用于结帐和我的帐户计费国家/地区:
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;
}
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;
}
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;
}
4) 对于结帐和我的帐户帐单和发货国家/地区:
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;
}
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;
}
很好的提示,我很早就尝试使用['custom_attributes']=array('readonly'=>'readonly')…但是“disabled”是选择字段的正确方式。@kashalo它可以工作,但是现在的问题是澳大利亚默认的国家不能进行签出…这里说“计费国家/地区是必填字段。请输入一个地址以继续。“让我再检查一遍,然后再回去u@pinkwidowbaby我用问题的解决方案更新了我的答案,请检查。非常感谢。一切都很好。您的代码使字段被禁用。由于禁用的字段在表单中不可用,签出将抛出验证错误。除了kashalo的答案之外,我还有什么需要补充的吗?