$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>woocommerce offtopic - Shipping Location based on IP (Geolocation)|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

woocommerce offtopic - Shipping Location based on IP (Geolocation)

matteradmin9PV0评论

I want to limit the Countries in the Shippiment to another address and Billing (Checkout) to the User IP Address location. Say the user is in USA I only want this country to show as a option under the dropdown country list or disable the option of changing the country. How would I do that? Thanks in advance.

I want to limit the Countries in the Shippiment to another address and Billing (Checkout) to the User IP Address location. Say the user is in USA I only want this country to show as a option under the dropdown country list or disable the option of changing the country. How would I do that? Thanks in advance.

Share Improve this question asked Nov 28, 2017 at 22:35 Denise PereiraDenise Pereira 211 silver badge2 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

First of all to make geolocation work properly go to WooCommerce > Status and check MaxMind GeoIP database option, if there is red checkmark follow the provided instructions to download the database.

Then you can add this code to your theme (at the bottom of functions.php) or add it as a plugin or code snippet as changes in theme might get lost when you update it.

function wpse_287199_woo_checkout_country( $fields ) {
    $geoData = WC_Geolocation::geolocate_ip();
    $countries = WC()->countries->get_countries();

    $fields['billing']['billing_country'] = array(
        'type' => 'select',
        'label'     => __('Country', 'woocommerce'),
        'options' => array(
            $geoData['country'] => $countries[$geoData['country']]
        ),
        'class' => array(
            'form-row-wide',
            'address-field',
            'update_totals_on_change'
        )
    );

    $fields['shipping']['shipping_country'] = array(
        'type' => 'select',
        'label'     => __('Country', 'woocommerce'),
        'options' => array(
            $geoData['country'] => $countries[$geoData['country']]
        ),
        'class' => array(
            'form-row-wide',
            'address-field',
            'update_totals_on_change'
        )
    );

    return $fields;
}
add_filter( 'woocommerce_checkout_fields' , 'wpse_287199_woo_checkout_country' );

This code checks the client's IP geolocation and determines his country then it is used for shipping and billing as an only option.

Post a comment

comment list (0)

  1. No comments so far