How to Apply Country Based Restrictions in WooCommerce

By displaying only items and payment methods that are available in the user’s country, the Country Restrictions for WooCommerce plugin allows users to completely personalize the purchasing information displayed by country. As a result, international clients will have a more positive user experience.

This plugin works by concealing prices or “add to cart” buttons for certain or all products dependent on the countries that have been specified. It also allows for rule-based management, which saves time and allows for the creation of distinct visibility settings that are applicable to different countries. Customers also have the option to show personalized text or a link in place of the pricing and add to cart buttons.

Apply Country Restrictions – Methods

There are three methods through which you can use Country Restrictions for WooCommerce. 

  1. Country Restrictions for WooCommerce by Addify
  2. Apply Country Restrictions Programmatically
  3. Default WooCommerce Settings

Method #1: Country Restrictions for WooCommerce by Addify

Using Country Restrictions for WooCommerce plugin allows you to hide the entire product or category using rule based management for all or specific countries. Additionally, you can also hide the price or Add to Cart button based on countries. Let’s dig deeper into the details of its installation and configuration settings. 

Installation:

  • To install the Country Restrictions for WooCommerce plugin, you should firstly download the .zip file from your account. 
  • Go to the WordPress Admin Panel.
  • Click Plugins.
  • Click Add New and then Upload Plugin
  • Click Install Now and Activate

Setup and Configuration

After activating the Country Restrictions for WooCommerce plugin, you can see “Country Restrictions” in the WordPress admin panel. Hover on the Country Restrictions to view settings and get started.

Bulk Visibility Settings/Multiple Rule Creation

With this plugin, you can manage visibility in bulk using multiple rules. You can hide products or prices and instead include an Add to Cart button for the countries. This also allows configuring different visibility options for different countries. 

Product & Category Based Rules 

While creating a rule for product and category restrictions, you can:

  • Add rule title for future reference
  • Specify countries
  • Select to restrict products
  • Show or Hide Products
  • Select individual products, categories or product tags
  • Configure restriction message type
  • Redirect to an internal page
  • Redirect to a custom URL
  • Show a custom message

Hide Price and Add to Cart 

With the rule based management, you can choose to hide the price and Add to Cart button. With this functionality, you can set rules for individual products like enabling or disabling it for the countries, restricting Add to Cart for that specific product, and a method to hide this button. You can also choose to restrict product price. 

Payment Methods

From the “Payment Method Settings” section, you can choose to show/hide individual payment methods based on your users’ GEO location.

General Settings

  • Go to Country Restrictions.
  • Open Settings.
  • Click on General Settings. 

This setting allows you to configure the visibility options for the countries for which you have not added any restrictions. 

Selecting to hide all products will hide the entire catalog for everyone. With the rules based management, you can allow specific customers to access specific products. 

While selecting to display all products, you can choose to hide specific products based on country restriction rules or individual product settings. 

The custom message that will appear in case of restriction can also be customized here:

Method #2: Apply Country Restrictions Programmatically

Let’s take a look at the program-based implementation of country based restrictions for WooCommerce.

  • Open WordPress Admin Panel > Appearance > Theme Editor.
  • Download the Functions.php theme file to edit.

Following this step, a separate panel of “Region Settings” will be created to your product/category’s add/edit page in the General Settings. This will create two options:

  • First, “Restriction” type which can be set to “Allow” or “Deny”.
  • Second, “Regions” type where you can specify the regions. (Note: Not specifying any region will display the product globally.)

The following code goes into your theme’s functions.php file. 

// Display Fields

add_action( 'woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields' );

// Save Fields
add_action( 'woocommerce_process_product_meta', 'woo_add_custom_general_fields_save' );

// Display Fields
function woo_add_custom_general_fields() {

  global $woocommerce, $post;

  echo '<div class="options_group">';

  ?>

    <?php
        // Select
            woocommerce_wp_select( 
            array( 
                'id'      => '_restriction-type', 
                'label'   => __( 'Restriction type', 'woocommerce' ), 
                'options' => array(
                'allow'   => __( 'Allow', 'woocommerce' ),
                'deny'   => __( 'Deny', 'woocommerce' ),
        )
    )
);

        // Create Textarea
            woocommerce_wp_textarea_input( 
            array( 
                'id'          => '_regions', 
                'label'       => __( 'Regions', 'woocommerce' ), 
                'placeholder' => '', 
                'desc_tip'    => 'true',
                'description' => __( 'Please enter two letter country codes. Each country code should be followed by a coma Example: ZW, AU, ZA, US ', 'woocommerce' ) 
        )
    );

  echo '</div>';
}


function woo_add_custom_general_fields_save( $post_id ){

        // Select
            $woocommerce_select = $_POST['_restriction-type'];
            if( !empty( $woocommerce_select ) )
                update_post_meta( $post_id, '_restriction-type', esc_attr( $woocommerce_select ) );


        // Textarea
            $woocommerce_textarea = $_POST['_regions'];
            if( !empty( $woocommerce_textarea ) )
                update_post_meta( $post_id, '_regions', esc_html( $woocommerce_textarea ) );

 }


The following code goes into the template.php file for the conditional execution of the setting. With this program, when a user from restricted countries tries to access the product it will display the custom message to that customer:

  • If the product is allowed in that country, it will display that specific product.
  • If the product is restricted, it will display “Product Restricted in your country”. 
<?php global $woocommerce; 

        // Get restriction type (deny or allow) for current product
        $restriction_type = get_post_meta( $post->ID, '_restriction-type', true );

        // Get region(s) the above restriction type is applied to 
        $regions = get_post_meta( $post->ID, '_regions', true );

        // Turns this string into an array. 
        $regions_array = (explode(',', str_replace('/\s+/','', $regions)));

        // Get users current IP location from WooCommerce 
        $base_country = (..... YOU NEED TO GET THE USER LOCATION ISO COUNTRY CODE ......)

        // If user's current IP location is either allowed, is not denied or is not set in the region settings = success
        if( $restriction_type == 'allow' && in_array($base_country , $regions_array) || $restriction_type == 'deny' && !in_array($base_country , $regions_array) || $restriction_type == '' || $regions == '' ) {

                if ($restriction_type == '' || $regions == '') {

                    //  Code to execute on success if a product is not set (NOTE: It will not be restricted)

                    echo('This product\'s region control has not been set, you can set it in WP Admin');
                }

                // Code to execute on success if a products region settings are set to allow access

                echo('YOU ARE IN'); 

        } else {

                // Code to execute when region is restricted

                echo(' you are restricted,');
        }

?>

The upside of using the program based implementation of country restrictions for WooCommerce is that it allows product by product restrictions on your online store. The downside of this method is that it is time consuming along with being confusing for people who lack technical knowledge. 

Method #3: Default WooCommerce Settings

The default settings to implement country based restrictions for WooCommerce can be done by restricting product display or shipping through the general settings in your WooCommerce account. Follow these steps below:

  • Open the WooCommerce Admin Panel.
  • Open Settings and then select General.
  • On the Selling Location, choose Store Address, and set it to “All Countries”. Here, you can choose the countries from the dropdown menu for which you wish to restrict the products/categories.

For applying restrictions on shipping, follow the steps below: 

  • Visit the WooCommerce Admin Panel.
  • Open Settings and then select General.
  • On the Shipping Location, select Ship to All Countries You Sell To or Ship to Specific Countries Only. Here, you can select the countries based on your requirements.

The only major downside of using the default setting of this plugin is that all the restrictions are applied sitewide i.e. on all products/categories of the page.

Comparison of Limitations in all Methods

Let’s take a look at how the three methods discussed above differ in terms of various limitations. It can be observed that using the Country Restrictions for WooCommerce plugin by Addify takes you out of the trouble of a number of limitations. 

LimitationsProgrammaticallyDefault SettingsCountry Restrictions by Addify
Easy to use and ImplementNo NoYes
Specific product based restrictionNo NoYes
Specific category based restrictionNoNoYes
Bulk rule applicationNoNoYes
Payment method restrictionNoNoYes
Hide both add to cart and Price at onceYesNoYes
Hide Price from SEPARATELYYesNoYes
Hide Add to Cart from product & category pagesYesNoYes
Custom Text in case of RestrictionYesNoYes
Automatic Geolocation DetectionNoYesYes
Site WideNoYesYes

Final Thoughts

The country restrictions for WooCommerce plugin access the GEO location for visitors as well as registered users to reach the country and apply restrictions. 

Avail The Oppurtunity

Subscribe to Our Updates and Newsletters.