Sometimes an online store needs to inform customers from specific countries that online orders are not available for them, but products can still be purchased in a physical store.
In my task, it was required that customers from Moldova could not place orders online, but they should see a message with the store’s address.
To make this work correctly and look nice, I implemented a combination of frontend and backend.
Using JS + PHP allows us to:
Immediately show a message to the user and hide checkout blocks.
Ensure the order cannot go through even if JavaScript is disabled.
JS (Frontend) — Good UX on the Checkout Page
Add the following to your functions.php file. The JavaScript shows a message immediately after selecting the country and hides the shipping, payment, and order summary sections. The user sees that online checkout is not possible and immediately gets the store’s address. Selectors may vary, and the code will need to be adapted.
add_action(‘woocommerce_after_checkout_form’, function() {
?>
<script>
jQuery(function($){
const blockedCountries = [‘MD’];
const noticeHTML = `
<div class=”checkout-country-notice”>
Thank you for your interest in our products!
You can purchase items in our store at ADDRESS_HERE.
For details and inquiries, please contact us at PHONE_NUMBER.
</div>`;
$(‘#billing_country_field’).append(noticeHTML);
function checkCountry(){
let selectedCountry = $(‘#billing_country’).val();
if(blockedCountries.includes(selectedCountry)){
$(‘.checkout-country-notice’).show();
$(‘#order_review, #order_review_heading, #payment, #shipping_method’).hide();
} else {
$(‘.checkout-country-notice’).hide();
$(‘#order_review, #order_review_heading, #payment, #shipping_method’).show();
}
}
checkCountry();
$(‘#billing_country’).on(‘change’, checkCountry);
});
</script>
<?php
});
PHP (Backend) — Reliable Server-Side Check
JavaScript can be bypassed if it’s disabled in the browser. Therefore, it’s necessary to add a server-side check that blocks the order creation and shows the same message.
add_action(‘woocommerce_checkout_process’, function(){
$blocked_countries = [‘MD’];
$country = isset($_POST[‘billing_country’]) ? sanitize_text_field($_POST[‘billing_country’]) : ”;
if (in_array(strtoupper($country), $blocked_countries)) {
wc_add_notice(
‘Thank you for your interest in our products!
You can purchase items in our store at ADDRESS_HERE.
For details and inquiries, please contact us at PHONE_NUMBER.’,
‘error’
);
}
});

