Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make checkout country dropdown readonly in Woocommerce

I want country dropdown on woocommerce as readonly. Country Image

I already set the default country to australia but I want them to be readonly.

like image 965
pink widow baby Avatar asked Dec 07 '22 13:12

pink widow baby


2 Answers

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;
}
like image 105
LoicTheAztec Avatar answered Dec 31 '22 08:12

LoicTheAztec


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 :

enter image description here

Code is tested with StorrFront Theme.

like image 24
kashalo Avatar answered Dec 31 '22 08:12

kashalo