在Woocommerce中将结帐国家/地区下拉列表设为只读

时间:2018-09-11 16:00:00

标签: php woocommerce checkout readonly country

我希望woocommerce的国家/地区下拉列表为只读。 Country Image

我已经将默认国家/地区设置为澳大利亚,但我希望它们是只读的。

2 个答案:

答案 0 :(得分:3)

Kashalo的答案是正确的……您还可以使用以下多种其他方式之一:

1)仅适用于结帐结算国家/地区:

add_filter('woocommerce_checkout_fields', 'readdonly_billing_country_select_field');
function readdonly_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_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_billing_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 :(得分:2)

您可以使用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;
}

当我们禁用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">';

}

只需将value="PL"更改为您的国家/地区代码值,一切都会按预期进行。

输出:

enter image description here

代码已通过StorrFront主题进行了测试。

相关问题