Woocommerce: Set Country by Default in Checkout Page

WooCommerce: Set country by default in checkout page

Update (Since WooCommerce 3)

This are the woocommerce hooks and code to be used for this purpose for country (and optionally state):

add_filter( 'default_checkout_billing_country', 'change_default_checkout_country_and_state' );
add_filter( 'default_checkout_shipping_country', 'change_default_checkout_country_and_state' );
add_filter( 'default_checkout_billing_state', 'change_default_checkout_country_and_state' );
add_filter( 'default_checkout_shipping_state', 'change_default_checkout_country_and_state' );
function change_default_checkout_country_and_state( $default ) {
return null;
}

Or even shorter:

add_filter( 'default_checkout_billing_country', '__return_null' );
add_filter( 'default_checkout_shipping_country', '__return_null' );
add_filter( 'default_checkout_billing_state', '__return_null' );
add_filter( 'default_checkout_shipping_state', '__return_null' );

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

Note: default_checkout_country and default_checkout_state hooks are deprecated and replaced since WooCommerce 3

Related: WooCommerce: Set country by default in checkout page for unlogged users

Default shipping country in Woocommerce Cart, not checkout

You need to go to your woocommerce settings and in the general settings set your store default country (red box at the top) and the set the default customer location to shop base address or you may use geolocation to get default customer location. Try this in incognito window in your browser and you will see it works.

Sample Image

WooCommerce: Set default Country in Billing (using Geo Location) but not in Shipping in checkout page

I've checked every hook/filter and found nothing useful. So I came up with this solution. Add this code to your child theme functions.php file:

add_action( 'wp_enqueue_scripts', 'wp_enqueue_scripts_action' );
function wp_enqueue_scripts_action() {
wp_register_script( 'test-script', get_stylesheet_directory_uri() . '/test.js', [ 'jquery', ] );
wp_enqueue_script( 'test-script' );

wp_localize_script( 'test-script', 'test_script', [
'customer_country' => get_customer_geo_location_country()
]
);
}

/**
* Returns the customer country
*
* @return string|null
*/
function get_customer_geo_location_country(): ?string {
if ( class_exists( 'WC_Geolocation' ) ) {
$location = WC_Geolocation::geolocate_ip();

if ( isset( $location['country'] ) ) {
return $location['country'];
}
}

return null;
}

add_action( 'woocommerce_after_checkout_validation', 'woocommerce_after_checkout_validation_action', 10, 2 );
function woocommerce_after_checkout_validation_action( $fields, $errors ) {
$billing_country = $fields['billing_country'];

if ( ! empty( $billing_country ) && $billing_country !== get_customer_geo_location_country() ) {
$errors->add( 'validation', 'You are not allowed to select this billing country!' );
}
}

First we add a new script. If you already have a script, you can just copy the wp_localize_script function and change the handler and the object name.

With this function we can pass the current country of the customer to our JavaScript file. Inside this file we do this stuff:

(function ( $ ) {
$( document ).ready( function () {
if (test_script.customer_country) {
$( '#billing_country option' ).each( function () {
if ($( this ).val() !== test_script.customer_country) {
$( this ).remove();
}
} );
}
} );
})( jQuery );

This little function removes every country from our billing select that doesn't match the customers country. If you want you can remove all countries in an else statement to be sure the customer can't order in case there is no country available.

The customer should now only see his home country in the billing country dropdown.

To be sure he don't hacks us, we add a little validation to the checkout where we verify the selected country again.

If you test this on localhost, no country will be available so be sure it's on a live website in the web (or even staging).

This is a basic idea. You need to test it and maybe adjust it too your needs.

WooCommerce: Set country by default in checkout page for unlogged users

Use is_user_logged_in() conditional tag as follows:

add_filter( 'default_checkout_billing_country', 'change_default_checkout_country' );
add_filter( 'default_checkout_shipping_country', 'change_default_checkout_country' );
function change_default_checkout_country( $default ) {
if ( ! is_user_logged_in() ) {
$default = null;
}
return $default;
}

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

Related: WooCommerce: Set country by default in checkout page

Change WooCommerce checkout country field default option displayed label

To change WooCommerce country field default option displayed value in checkout page, you can use the following composite hook this way:

add_filter( 'woocommerce_form_field_country', 'filter_form_field_country', 10, 4 );
function filter_form_field_country( $field, $key, $args, $value ) {
// Only in checkout page for "billing" city field
if ( is_checkout() && 'billing_country' === $key ) {
$search = esc_html__( 'Select a country / region…', 'woocommerce' ); // String to search
$replace = esc_html__( 'My new select prompt', 'woocommerce' ); // Replacement string
$field = str_replace( $search, $replace, $field );
}
return $field;
}

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

Then you can keep on your question code the following:

add_filter( 'woocommerce_checkout_fields', 'custom_override_checkout_fields' );
function custom_override_checkout_fields( $fields ) {
$fields['billing']['billing_country']['label'] = __('Are you purchasing as a Company Y/N or from the UK?', 'woocommerce');

return $fields;
}

Woocommerce how to set default_checkout_billing_country only if user is not logged in

You could use is_user_logged_in:

add_filter( 'default_checkout_billing_country', 'bbloomer_change_default_checkout_country' );

function bbloomer_change_default_checkout_country($default) {

if(is_user_logged_in()){
return $default;
}else{
return 'US';
};

};

However, sometimes, because of caching issues, is_user_logged_in doesn't work. In that case then you could use global $current_user.

add_filter( 'default_checkout_billing_country', 'bbloomer_change_default_checkout_country' );

function bbloomer_change_default_checkout_country($default) {

global $current_user;

if($current_user->ID){
return $default;
}else{
return 'US';
};

};

Let me know if you could get it to work!

How to force default country in Wordpress

Please try this code in functions.php

    //set default country in checkout
add_filter( 'default_checkout_billing_country', 'change_default_billing_country', 10, 1 );
add_filter( 'default_checkout_shipping_country', 'change_default_shipping_country', 10, 1 );

function change_default_billing_country( $country ) {
// If the user already exists, don't override country
if ( WC()->customer->get_is_paying_customer() ) {
return $country;
}

$country = "US";
return $country;
}

function change_default_shipping_country( $country ) {
// If the user already exists, don't override country
if ( WC()->customer->get_is_paying_customer() ) {
return $country;
}
$country = "US";
return $country;
}

add_filter( 'default_checkout_billing_country', 'change_default_checkout_country_and_state' );
function change_default_checkout_country_and_state( $default ) {
if(is_user_logged_in())
{
$billcountry = get_user_meta( get_current_user_id() , 'billing_country', true );
if($billcountry == '')
{
$billcountry = "US";
}
}
else
{
$billcountry = "US";
}
return $billcountry;
}

//hide country field in shipping calculator and checkout page
add_action( 'wp_footer', 'hide_calc_country_cart' );

function hide_calc_country_cart() {
if ( is_cart()) {
echo "<script type='text/javascript'>
$('[id=\"calc_shipping_country_field\"]').css('display','none');
</script>";
}
if ( is_checkout()) {
echo "<script type='text/javascript'>
$('[id=\"billing_country_field\"]').css('display','none');
</script>";
}
}


Related Topics



Leave a reply



Submit