WooCommerce: Show Message After Country Selection @ Checkout

If you have country-specific shipping conditions, or you want to show a message conditionally upon 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.

Show a notification based on Billing Country selection @ WooCommerce Checkout

PHP Snippet: Show Conditional Content @ WooCommerce Checkout

/**
 * @snippet       Show Conditional Message Upon Country Selection @ WooCommerce Checkout
 * @how-to        Watch tutorial @ https://businessbloomer.com/?p=19055
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 3.6.2
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */
 
// 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
// The "display:none" hides it by default
 
add_action( 'woocommerce_after_checkout_form', 'bbloomer_show_notice_shipping' );
 
function bbloomer_show_notice_shipping(){
    
    ?>
 
    <script>
        jQuery(document).ready(function($){
 
            // Set the country code (That will display the message)
            var countryCode = 'FR';
 
            $('select#billing_country').change(function(){
 
                selectedCountry = $('select#billing_country').val();
                 
                if( selectedCountry == countryCode ){
                    $('.shipping-notice').show();
                }
                else {
                    $('.shipping-notice').hide();
                }
            });
 
        });
    </script>
 
    <?php
    
}

Where to add this code?

You can place PHP snippets at the bottom of your child theme functions.php file (before "?>" if you have it). CSS, on the other hand, goes in your child theme style.css file. Make sure you know what you are doing when editing such delicate files - if you need more guidance, please take a look at my free WooCommerce Customization video tutorial.

Does this snippet still work?

Please let me know in the comments if everything worked as expected. I would be happy to revise the snippet if you report otherwise (please provide screenshots). I have tested this code with Storefront theme, the WooCommerce version listed above and a WordPress-friendly hosting on PHP 7.

If you think this code saved you time & money, please join other Business Bloomer supporters and avail of 365 days of WooCommerce benefits. Thank you in advance :)

Need Help with WooCommerce?

Check out these free videos, tutorials and tips!

  • how-to-edit-woocommerce-with-php-snippets
  • woocommerce-hooks-add_action-list-visual
  • woocommerce-customize-single-product-page-PHP
  • woocommerce-customize-shop-page-PHP
  • woocommerce-advanced-customization
  • how-to-edit-woocommerce-cart
  • woocommerce-customize-checkout-page-PHP
  • woocommerce-email-customization
  • woocommerce-conditional-logic

Rodolfo Melogli

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as a 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.

24 thoughts on “WooCommerce: Show Message After Country Selection @ Checkout

  1. 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.

    2. 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!

  2. 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

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

    1. ๐Ÿ™‚

  4. 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

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

    1. Brilliant ๐Ÿ™‚

  6. 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 ๐Ÿ™‚

  7. 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 ๐Ÿ™‚

  8. 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>';
      } 
      
      }
      
  9. 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 ๐Ÿ™‚

  10. 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();
      }
      
      
    2. 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!

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

Questions? Feedback? Support? Leave your Comment Now!
_____

If you are writing code, please wrap it between: [php]code_here[/php]. Failure to complying with this (as well as going off topic) will result in comment deletion. You should expect a reply in about a week - this is a popular blog but I need to get paid work done first. Please consider joining #BloomerArmada to ask me 1-to-1 WooCommerce questions. Thank you :)

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.