Black Friday & Cyber Monday SUPER SALE ALL WEEK:
Grab 40% OFF on plugins
00 Days
00 Hours
00 Minutes
00 Seconds

How to Show Selected Countries on WooCommerce Checkout for Specific Products?

Do you want to show a particular list of billing countries based on the product ID in your customer’s cart, this code snippet is the solution!

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function ts_countries_allowed_countries( $countries ) {
// Cart or checkout page
if ( is_cart() || is_checkout() ) {
// The targeted product ids
$targeted_ids = array( 96 );
// Country codes you want to show
$show_countries = array( 'IN', 'NL', 'FR', 'ES' );
$found = false;
if ( WC()->cart ) {
// Loop through cart items
foreach ( WC()->cart->get_cart() as $cart_item ) {
if ( array_intersect( $targeted_ids, array( $cart_item['product_id'], $cart_item['variation_id'] ) ) ) {
$found = true;
break;
}
}
}
if ( $found ) {
// Loop through country codes
foreach ( $countries as $key => $country ) {
// NOT found
if ( ! in_array( $key, $show_countries ) ) {
// Remove
unset( $countries[$key] );
}
}
}
}
return $countries;
}
add_filter( 'woocommerce_countries_allowed_countries', 'ts_countries_allowed_countries', 10, 1 );
function ts_countries_allowed_countries( $countries ) { // Cart or checkout page if ( is_cart() || is_checkout() ) { // The targeted product ids $targeted_ids = array( 96 ); // Country codes you want to show $show_countries = array( 'IN', 'NL', 'FR', 'ES' ); $found = false; if ( WC()->cart ) { // Loop through cart items foreach ( WC()->cart->get_cart() as $cart_item ) { if ( array_intersect( $targeted_ids, array( $cart_item['product_id'], $cart_item['variation_id'] ) ) ) { $found = true; break; } } } if ( $found ) { // Loop through country codes foreach ( $countries as $key => $country ) { // NOT found if ( ! in_array( $key, $show_countries ) ) { // Remove unset( $countries[$key] ); } } } } return $countries; } add_filter( 'woocommerce_countries_allowed_countries', 'ts_countries_allowed_countries', 10, 1 );
function ts_countries_allowed_countries( $countries ) { 
    // Cart or checkout page
    if ( is_cart() || is_checkout() ) {     
        // The targeted product ids
        $targeted_ids = array( 96 );
    
        // Country codes you want to show
        $show_countries = array( 'IN', 'NL', 'FR', 'ES' );
        $found = false;
        if ( WC()->cart ) {         
            // Loop through cart items
            foreach ( WC()->cart->get_cart() as $cart_item ) {
                if ( array_intersect( $targeted_ids, array( $cart_item['product_id'], $cart_item['variation_id'] ) ) ) {
                    $found = true;
                    break;
                }
            }
        }
        if ( $found ) {
            // Loop through country codes
            foreach ( $countries as $key => $country ) {
                // NOT found
                if ( ! in_array( $key, $show_countries ) ) {
                    // Remove
                    unset( $countries[$key] );
                }
            }
        }
    }
    return $countries;
}
add_filter( 'woocommerce_countries_allowed_countries', 'ts_countries_allowed_countries', 10, 1 );

Output

When the customer visits the checkout page and the cart contains a product with ID 96, the allowed countries will be filtered to include only ‘IN’, ‘NL’, ‘FR’, and ‘ES’ on the dropdown as shown below.

Show Selected Countries on WooCommerce Checkout for Specific Products

Similarly, you can also restrict billing to a particular country on the WooCommerce checkout page.

Browse more in: Code Snippets, WooCommerce How Tos, WooCommerce Tutorials

Share It:

Subscribe
Notify of


0 Comments
Newest
Oldest
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x
Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible.

Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.

By using our site, you acknowledge that you have read and understood our Privacy Policy and Terms & Conditions.