使Woocommerce中结账时的国家下拉框只读

4
我希望在WooCommerce上的国家下拉菜单是只读的。 Country Image 我已经将默认国家设置为澳大利亚,但我希望它们是只读的。
3个回答

8
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;
}

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;
}

1
非常感谢。一切都运行得非常顺利。 - pink widow baby
你的代码使字段变为禁用状态。由于禁用字段在表单中不可用,因此结账会引发验证错误。除了 kashalo 的答案之外,我是否需要添加其他内容来修复这个问题? - melvin

7
您可以使用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"更改为您的国家代码值,一切就会按预期工作。

输出:

enter image description here

此代码已与StorrFront主题测试。


不错的发现,我之前试过使用 ['custom_attributes'] = array( 'readonly' => 'readonly' ) 来使其工作...但是对于选择字段来说,“disabled”才是正确的方式。 - LoicTheAztec
@kashalo,它正在工作,但现在的问题是默认为澳大利亚的国家无法进行结账...它在这里说“账单国家是必填字段。请继续输入地址。” - pink widow baby
让我仔细检查一下,然后回复您。 - kashalo
@pinkwidowbaby,我已经更新了我的答案,并提供了解决问题的方案,请查看。 - kashalo

0
/*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 
}

你的回答可以通过提供更多支持信息来改进。请编辑以添加进一步的细节,例如引用或文档,以便他人可以确认你的答案是正确的。您可以在帮助中心中找到有关如何编写良好答案的更多信息。 - Community

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接