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!
PHP Snippet: Display Conditional Content @ WooCommerce Checkout
/**
* @snippet Show Conditional Message Upon Country Selection @ WooCommerce Checkout
* @how-to businessbloomer.com/woocommerce-customization
* @author Rodolfo Melogli, Business Bloomer
* @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 );
});
" );
}
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.
I see, the code would need a little edit in this case
how can I delete the warning messages in the checkout screen
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!
How can I show the message for 2 countries?
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!
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.
Great!
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
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!
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.
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!
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!
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!
Hi,
This is great, the issue is that I need to display this message if the ‘Ships From’ location is not United States
I also need the message to display when the item is selected by the customer, before adding to their cart.
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!
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.
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
The snippet works with one country (AU) but not another (CA/Canada)
perfectly done the job. super thanks to you.
🙂
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
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
Nice, this worked perfectly for me 🙂 All I had to do was to change to my message and country code, very easy!
Brilliant 🙂
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
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 🙂
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.
Appreciate your help.
Thank you.
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 🙂
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:
I would be more than glad for your help!
Peter
Wow! It looks I just fixed it!
I just changed the first function (where the ECHO is):
Awesome 🙂
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.
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 🙂
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?
Hey Jim, appreciate your comment! So far I came up with this revision – it’s not 100% perfect but let me know:
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!
Hey Nate, thanks for your comment! This is pretty easy – instead of “WC()->customer->shipping_state” try using “WC()->customer->shipping_country” 🙂
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
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!