4

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.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
pink widow baby
  • 55
  • 2
  • 11

3 Answers3

8

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;
}
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • 1
    Thank you Very much..Everything is working very well – pink widow baby Sep 12 '18 at 02:38
  • Your code makes the fields disabled. Since disabled fields are not available in form, checkout throws validation error. Is there anything i have to add inorder to fix this other than `kashalo`s answer ? – melvin Sep 24 '19 at 11:58
7

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.

Community
  • 1
  • 1
kashalo
  • 3,442
  • 2
  • 11
  • 28
  • Nice catch, I was trying early to make it work with ['custom_attributes'] = array( 'readonly' => 'readonly' ) … but "disabled" is the correct way for a select field. – LoicTheAztec Sep 11 '18 at 20:34
  • @kashalo It's working but the problem now is that the country which is by default in australia can't proceed to checkout...It say here "Billing Country is a required field. Please enter an address to continue." – pink widow baby Sep 12 '18 at 03:29
  • let me double check it and get back to u – kashalo Sep 12 '18 at 09:38
  • @pinkwidowbaby i updated my answer with solution for the issue please check – kashalo Sep 12 '18 at 10:14
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 
}
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 29 '21 at 08:06