Skip to content
Advertisement

Disallow customer to change country in WooCommerce checkout

I am looking for solution where I can find user’s country in woocommerce checkout page. I am doing it via geolocation. However, the user can change that. I want to restrict it.

I am using a paid plugin to give coupons to users from a specific country. The country gets loaded perfectly and coupon is applied or rejected based on country. However, the user manually can change the country at the checkout page and avail the discount. My service is online thus there is no delivery of physical goods so entering wrong country wouldn’t cause anything to the user.

I have tried 2-3 different coupon restriction plugins but all has the same problem. Does anyone has faced similar issue?

Advertisement

Answer

Updated

You can make checkout country dropdown fields disabled (read only) as follows:

add_filter( 'woocommerce_checkout_fields', 'checkout_country_fields_disabled' );
function checkout_country_fields_disabled( $fields ) {
    $fields['billing']['billing_country']['custom_attributes']['disabled'] = 'disabled';
    $fields['billing']['shipping_country']['custom_attributes']['disabled'] = 'disabled';

    return $fields;
}

As disabled select fields doesn’t get posted, you need additionally the following duplicated hidden fields with the correct values set in. It will avoid an error notice notifying that country required fields values are empty. So add this too:

// Mandatory for disable fields (hidden billing and shipping country fields with correct values)
add_filter( 'woocommerce_after_checkout_billing_form', 'checkout_country_hidden_fields_replacement' );
function checkout_country_hidden_fields_replacement( $fields ) {
    $billing_country = WC()->customer->get_billing_country();
    $shipping_country = WC()->customer->get_shipping_country();
    ?>
    <input type="hidden" name="billing_country" value="<?php echo $billing_country; ?>">
    <input type="hidden" name="shipping_country" value="<?php echo $shipping_country; ?>">
    <?php
}

Code goes in functions.php file of the active child theme (or active theme). Tested and works.


Notes:

You will need to disable shipping calculator in cart page, if it’s not done.

If you want to make country dropdown read only in Checkout and in My account Edit Address too, use the following instead:

add_filter( 'woocommerce_billing_fields', 'checkout_billing_country_field_disabled' );
function checkout_billing_country_field_disabled( $fields ) {
    $fields['billing_country']['custom_attributes']['disabled'] = 'disabled';

    return $fields;
}

add_filter( 'woocommerce_shipping_fields', 'checkout_shipping_country_field_disabled' );
function checkout_shipping_country_field_disabled( $fields ) {
    $fields['shipping_country']['custom_attributes']['disabled'] = 'disabled';

    return $fields;
}

// Mandatory for disable fields (hidden billing and shipping country fields with correct values)
add_filter( 'woocommerce_after_checkout_billing_form', 'checkout_country_hidden_fields_replacement' );
function checkout_country_hidden_fields_replacement( $fields ) {
    $billing_country = WC()->customer->get_billing_country();
    $shipping_country = WC()->customer->get_shipping_country();
    ?>
    <input type="hidden" name="billing_country" value="<?php echo $billing_country; ?>">
    <input type="hidden" name="shipping_country" value="<?php echo $shipping_country; ?>">
    <?php
}
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement