• Resolved freddyeee

    (@freddyeee)


    Hi

    is it possible to show only the geolocated user country into Woocommerce checkout country field ?

    since I sell to different countries, but products have higher prices depending of that, I’d need to just show the geolocated user country.

    • This topic was modified 3 months, 1 week ago by freddyeee.
Viewing 4 replies - 1 through 4 (of 4 total)
  • Saif

    (@babylon1999)

    If you have the default customer location set to “Geolocate” in WooCommerce -> Settings, you can use the following snippet to make the country field unclickable.

    add_filter( 'woocommerce_checkout_fields', 'custom_override_checkout_fields' );

    function custom_override_checkout_fields( $fields ) {
    $fields['billing']['billing_country']['custom_attributes'] = array(
    'disabled' => 'disabled',
    );
    $fields['shipping']['shipping_country']['custom_attributes'] = array(
    'disabled' => 'disabled',
    );

    return $fields;
    }

    You can use this plugin to add it.

    Keep in mind that this only disables the field on the client side, meaning anyone with HTML knowledge can modify the field and select any country they want.

    If you want to do this the right way, you should implement server-side validation to prevent customers who don’t meet specific conditions from placing an order.

    There are several plugins available that can assist with this, for example: https://woocommerce.com/products/order-restrictions-for-woocommerce/

    Free alternative: https://www.ads-software.com/plugins/woo-product-country-base-restrictions/

    Cheers!

    Thread Starter freddyeee

    (@freddyeee)

    Thanks for your response. I managed to get it done using this snippet:

    add_action('woocommerce_before_checkout_billing_form', 'limit_checkout_countries');
    add_action('woocommerce_before_checkout_shipping_form', 'limit_checkout_countries');

    function limit_checkout_countries() {
    // Get customer location using WooCommerce geolocation
    $customer_country = WC_Geolocation::geolocate_ip();

    // If the client's location cannot be determined, use the default country
    if (empty($customer_country['country'])) {
    $customer_country['country'] = WC()->countries->get_base_country();
    }

    // Filters the countries available for billing and shipping
    add_filter('woocommerce_countries_allowed_countries', function($countries) use ($customer_country) {
    return array($customer_country['country'] => $countries[$customer_country['country']]);
    });
    }

    which code would be the most efficient way ?

    Saif

    (@babylon1999)

    Your snippet is better is you’re looking for a global restriction. I thought you were trying to limit certain products only. : )

    Plugin Support Rajesh K. (woo-hc)

    (@rajeshml)

    Hello @freddyeee,

    Glad to know that you were able to resolve the issue by using a snippet.

    For reference, this particular forum is meant for general support with the core functionality of WooCommerce itself.

    For development and custom coding questions, it’s best to ask for insight related to those on either the?WooCommerce Advanced Facebook group?or the?WooCommerce Community Slack. Many of our developers hang out there and will be able to offer insights into your question.

    I hope this helps! Please let us know if you have any other questions.

Viewing 4 replies - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.