WooCommerce: Show Message Upon Country Selection @ Checkout

If you have country-specific shipping conditions, or you want to show a message conditionally after country selection on the checkout page, here’s a simple WooCommerce PHP snippet you can freely use. This can be slightly customized to target state selection instead.

I’ve also edited the initial code to make this work on load as well, so that the message would conditionally show even then. Enjoy!

Show a notification based on Billing Country selection @ WooCommerce Checkout

PHP Snippet: Display Conditional Content @ WooCommerce Checkout

/**
 * @snippet       Show Conditional Message Upon Country Selection @ WooCommerce Checkout
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 5
 * @community     https://businessbloomer.com/club/
 */
 
// Part 1
// Add the message notification and place it over the billing section
// The "display:none" hides it by default
 
add_action( 'woocommerce_before_checkout_billing_form', 'bbloomer_echo_notice_shipping' );
 
function bbloomer_echo_notice_shipping() {
   echo '<div class="shipping-notice woocommerce-info" style="display:none">Please allow 5-10 business days for delivery after order processing.</div>';
}
 
// Part 2
// Show or hide message based on billing country
 
add_action( 'woocommerce_after_checkout_form', 'bbloomer_show_notice_shipping' );
 
function bbloomer_show_notice_shipping(){
    
   wc_enqueue_js( "
 
      // Set the country code that will display the message
      var countryCode = 'FR';

      // Get country code from checkout
      selectedCountry = $('select#billing_country').val();

      // Function to toggle message
      function toggle_upsell( selectedCountry ) {	
			if( selectedCountry == countryCode ){
				$('.shipping-notice').show();
			}
			else {
				$('.shipping-notice').hide();
			}
		}

      // Call function
		toggle_upsell( selectedCountry );
		$('select#billing_country').change(function(){
			toggle_upsell( this.value );			
		});
 
   " );
    
}

Where to add custom code?

You should place custom PHP in functions.php and custom CSS in style.css of your child theme: where to place WooCommerce customization?

This code still works, unless you report otherwise. To exclude conflicts, temporarily switch to the Storefront theme, disable all plugins except WooCommerce, and test the snippet again: WooCommerce troubleshooting 101

Related content

  • WooCommerce: Cart and Checkout on the Same Page
    This is your ultimate guide – complete with shortcodes, snippets and workarounds – to completely skip the Cart page and have both cart table and checkout form on the same (Checkout) page. But first… why’d you want to do this? Well, if you sell high ticket products (i.e. on average, you sell no more than […]
  • WooCommerce: Disable Payment Method If Product Category @ Cart
    Today we take a look at the WooCommerce Checkout and specifically at how to disable a payment gateway (e.g. PayPal) if a specific product category is in the Cart. There are two tasks to code in this case: (1) based on all the products in the Cart, calculate the list of product categories in the […]
  • WooCommerce: Add Privacy Policy Checkbox @ Checkout
    Here’s a snippet regarding the checkout page. If you’ve been affected by GDPR, you will know you now need users to give you Privacy Policy consent. Or, you might need customer to acknowledge special shipping requirements for example. So, how do we display an additional tick box on the Checkout page (together with the existing […]
  • WooCommerce: Redirect to Custom Thank you Page
    How can you redirect customers to a beautifully looking, custom, thank you page? Thankfully you can add some PHP code to your functions.php or install a simple plugin and define a redirect to a custom WordPress page (as opposed to the default order-received endpoint). This is a great way for you to add specific up-sells, […]
  • WooCommerce: Disable Payment Gateway by Country
    You might want to disable PayPal for non-local customers or enable a specific gateway for only one country… Either way, this is a very common requirement for all of those who trade internationally. Here’s a simple snippet you can further customize to achieve your objective. Simply pick the payment gateway “slug” you want to disable/enable […]

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza. Follow @rmelogli

41 thoughts on “WooCommerce: Show Message Upon Country Selection @ Checkout

  1. Thanks for the snippet. The only big issue is that if you enter a shipping country in the cart, the country will already be entered when you arrive at checkout and the message will not show because it is looking for a select change, but not the current selected value.

    1. I see, the code would need a little edit in this case

  2. how can I delete the warning messages in the checkout screen

    1. Hi Husney, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  3. How can I show the message for 2 countries?

    1. Hi Kazi, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  4. You are really awesome!!! Your snippets help me out to solve so many WooCommerce development issues.

    I have to show notice in a popup and I use bootstrap popup and try to write js in the footer and it does not work properly. But getting this snippet I got the idea. really helpful.

    1. Great!

  5. Hello,

    Thanks for this, this was really helpful!

    However one point, the function doesn’t load until the user interacts with the select field. Therefore if a user has saved Finland as they’re billing address or my sites default shipping address is Finland the display is hidden straight away. The only way it shows is if you click the select, choose another country and then select Finland again.

    Is there a way around this?

    Thanks for any help you can offer,
    Jack

    1. Hi Jack, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  6. This is awesome, thank you so much. The only thing that I was wondering is if someone is shipping to a different country the message doesn’t show up. It only shows up when they choose Canada for the billing address.

    Is there a way to adjust it so that it shows up if they choose shipping to Canada but have a billing address in the US for example?

    Thanks.

    1. Hello Gigi, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  7. Hey Rodolfo,
    This is working perfectly for me – Thank you! Quick question — can it be edited to display only when a customer is NOT in that country? i.e. var countryCode [not = to] ‘FR’. My goal is to display the message to everyone who does not select US as their billing country. Thanks much for the advice. We really appreciate your work!

    1. Hello Amy, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  8. Hi,

    This is great, the issue is that I need to display this message if the ‘Ships From’ location is not United States

    1. I also need the message to display when the item is selected by the customer, before adding to their cart.

      1. Hi Rodney, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  9. I’m trying this for our site based on location for Canada and the US and not having any joy. Any help would be appreciated.

    1. Hi Simon, thanks for your comment! I just tested this again with Storefront theme and it works perfectly. Maybe your theme (or another plugin) is messing/conflicting with my snippet?

      To troubleshoot, disable all plugins but WooCommerce and also switch temporarily to “Twentyseventeen” theme (load the snippet there in functions.php) – does it work? If yes, you have a problem with your current theme or one of the plugins.

      Hope this helps!

      R

    2. The snippet works with one country (AU) but not another (CA/Canada)

  10. perfectly done the job. super thanks to you.

    1. ๐Ÿ™‚

  11. Rudolfo – Just what I was looking for! Works perfect.

    I did have a couple of questions. 1) How would you move the message below the Billing Details, and 2), How can you add the code to the “Ship to a Different Address” area?

    Thanks for the help.
    Dwight

    1. Dwight, thanks so much for your comment! Yes, this is possible – but unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding! ~R

  12. Nice, this worked perfectly for me ๐Ÿ™‚ All I had to do was to change to my message and country code, very easy!

    1. Brilliant ๐Ÿ™‚

  13. This is the only post i found mentioning exactly what i need. I have applied the code and doesnt work in the latest WC 4.95. anyone has an idea of how to accomplish this. any help will be appreaciated

    1. Hey Ale, thanks so much for your comment! I’ve revised and updated the snippet. Just change “billing_country” with “billing_state” and it should work for you ๐Ÿ™‚

  14. Hi,

    I tried your sample code and modified the placement of the ‘message’ before the payment section, however, it’s not working.

    Please find the below modified codes.

    function add_checkout_notice() {
    	if ( ! is_ajax() ) {
    	echo '<p class="checkout_notice woocommerce-info" style="display:none;">any message to show</p>';
    	} 
    }
    	add_action( 'woocommerce_review_order_before_payment', 'add_checkout_notice', 10); 
    	add_action( 'woocommerce_review_order_before_payment', 'show_checkout_notice', 12);
    
    	  
    function show_checkout_notice() {
    	global $woocommerce;
    	$msg_states = array( 'AU','GB');
    
    	if( in_array( WC()->customer->shipping_country, $msg_states ) ) {
    	echo "<style>.checkout_notice{display:block !important; }</style>";
    	 
    	} else {
    	echo '<style>.checkout_notice{display:none !important; }</style>';
    	}
    }
    
    

    Appreciate your help.

    Thank you.

    1. Hey Bon, thanks so much for your comment! Let’s say “echo” is not the best strategy in this case, I will need to revise this snippet when I find some time ๐Ÿ™‚

  15. Dear Rodolfo,

    I was so lucky to find this, it works almost perfectly as I need, but I am experiencing the same problem as Jim. The message is showing 2 or even 3 times.

    I tried to use the code you replied to Jim, but that doesn’t work for me. I tried to update it somehow into your original code, but still, no success.

    I am using the storefront theme. And this is how my code looks like:

     
    function add_checkout_notice() { 
    echo '<p class="checkout_notice" style="color:red;float:left;display:none">MY MESSAGE</p>';
    } 
    add_action( 'woocommerce_review_order_before_cart_contents', 'add_checkout_notice', 10 ); 
    add_action( 'woocommerce_review_order_before_cart_contents', 'show_checkout_notice', 12 );
     
    function show_checkout_notice() {
     
    global $woocommerce;
    $msg_states = array( 'AU','CA','PK','CL','ID','CO','JM','GD','KW','EG','AE','ZA','BH' );
     
    if( in_array( WC()->customer->shipping_country, $msg_states ) ) {
    echo '<style>.checkout_notice{display:block !important}</style>';
    
    } else {
    echo '<style>.checkout_notice{display:none !important}</style>';
    }
    }
    

    I would be more than glad for your help!

    Peter

    1. Wow! It looks I just fixed it!

      I just changed the first function (where the ECHO is):

      function add_checkout_notice() {
      
      if ( ! is_ajax() ) {
      echo '<p class="checkout_notice" style="color:red;float:left;display:none">any message to show</p>';
      } 
      
      }
      
  16. Hello, Rodolfo.

    Still not working, it triggers multiple divs when you change country. Is there any way to limit this? I mean if I select one country, it shows up one time. If I select another country, one more div will be added.

    1. Ciubotaru, thanks for your comment and you’re totally right, “echo” is not the best thing to use in this case. I will add this snippet to my to-revise list and will comment back when I release it. Thank you ๐Ÿ™‚

  17. When I try to use it, it loads the message multiple times. I’m assuming it is because of the Ajax but not sure. Any suggestions?

    1. Hey Jim, appreciate your comment! So far I came up with this revision – it’s not 100% perfect but let me know:

      
      add_action( 'woocommerce_review_order_before_cart_contents', 'show_checkout_notice', 12 );
       
      function show_checkout_notice() {
      	global $woocommerce;
       	$msg_states = array( 'AL','FL','GA','SC','NC','MD','DC','PA','NJ','NY','CT','RI','MA','VT','NH','ME','VA' );
      	ob_start();
      	echo '<p class="checkout_notice" style="color:red">This text here will show when one of the chosen states is selected</p>';
      	if( in_array( WC()->customer->get_shipping_state(), $msg_states ) ) {	
      		ob_end_flush();
      	} else ob_end_clean();
      }
      
      
      1. Hi!
        Can you show my how to modify this code in a way that it shows a message when a specific country (instead of a state) is selected?

        Many thanks in advance!

        1. Hey Nate, thanks for your comment! This is pretty easy – instead of “WC()->customer->shipping_state” try using “WC()->customer->shipping_country” ๐Ÿ™‚

          1. Hi,

            What if I wanted to add states AND countries? So if either one of them was selected as the state or country, that notice in red would show to the customer?

            Thanks

            1. Hi, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

Questions? Feedback? Customization? Leave your comment now!
_____

If you are writing code, please wrap it like so: [php]code_here[/php]. Failure to complying with this, as well as going off topic or not using the English language will result in comment disapproval. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining the Business Bloomer Club to get quick WooCommerce support. Thank you!

Your email address will not be published. Required fields are marked *