How to add a custom message at checkout in WooCommerce based on the selected country

woo develop

Sometimes you will find in your WooCommerce online store that you need to display a custom message to customers who select a certain country, either to tell them delivery times above normal, special conditions, discounts or whatever you need or can think of.

Mr. code

If this is your need you can easily get it with a code like this one:

/* Custom message when selecting Portugal */
// Part 1 - Create the message and we put it on checkout
add_action( 'woocommerce_before_checkout_billing_form', 'wphelp_message_portugal' );
function wphelp_message_portugal() {
echo '<div class="shipping-notice woocommerce-info" style="display:none">Delivery to Portugal takes 3 to 5 working days..</div>';
}
// Part 2 - Show/hide message depending on the country
add_action( 'woocommerce_after_checkout_form', 'wphelp_show_message_portugal' );
function wphelp_show_message_portugal(){
?>
<script>
jQuery(document).ready(function($){
// Put here the country code of the country for which the message is to be shown
var countryCode = 'PT';
$('select#billing_country').change(function(){
selectedCountry = $('select#billing_country').val();
if( selectedCountry == countryCode ){
$('.shipping-notice').show();
}
else {
$('.shipping-notice').hide();
}
});
});
</script>
<?php
}Code language: JavaScript (javascript)

What you get when the customer selects the country code at checkout is a message like this…

Where do I put Mr. code?

As it is going to be an independent code of the theme you use, ideally you should create a plugin with the code or add it to your customizations plugin.

But you can also add it to the end of the functions.php file of the active (child) theme.

What can be customized about Mr. code?

In the code you can change the text that will be shown for the one you need, as well as the country, changing the country code. In this link you have all the IBAN codes for each country.

How useful was this post?

Click on a smiley to rate it!

Average rating 5 / 5. Vote count: 1

No votes so far! Be the first to rate this post.

As you found this post useful...

Follow us on social media!

About The Author

Leave a Comment

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

Scroll to Top
Skip to content