How To Restrict Purchases To Specific Countries - SureCart

No results found. Try again with different words?

Search must be at least 3 characters.

How To Restrict Purchases To Specific Countries

If you want to restrict purchases to specific countries, you can simply add the following code directly to your functions.php file.

You can also use code snippet plugins like WPCode, CodeSnippets, etc. to add this code to your website’s footer section.

In this guide, we will show you how you can add this code by using the WPCode plugin, but feel free to use any code snippet plugin of your choice.

Restricting Purchases to Specific Countries

You can use the following code to restrict purchases to the countries of your choice.

add_action('wp_footer', function() { ?>
  <script>
    window.addEventListener('appload', (event) => {
      const select = document.querySelector('sc-order-shipping-address')
        .shadowRoot.querySelector('sc-address')
        .shadowRoot.querySelector('sc-select');

      // set a default.
      select.value = 'YOUR_COUNTRY_CODE';

      // limit choices:
      select.choices = [{
        value: 'YOUR_COUNTRY_CODE',
        label: 'YOUR_COUNTRY_NAME'
      }];
    });
  </script>
<?php });

You can of course modify this code to include any other country that you want to allow purchases from.

For this example, we will modify this code so that it contains two countries (US and Canada) and sets the default country to the US. The default country is nothing but the default first choice that will show up in the dropdown.

Just replace “YOUR_COUNTRY_CODE” with the two-letter country code of the country you want to allow purchases from.

Replace “YOUR_COUNTRY_NAME” with the name of the country you want to allow purchases from.

So, the code to restrict purchases to only the US and Canada will be:

add_action('wp_footer', function() { ?>
  <script>
    window.addEventListener('appload', (event) => {
      const select = document.querySelector('sc-order-shipping-address')
        .shadowRoot.querySelector('sc-address')
        .shadowRoot.querySelector('sc-select');

      // set a default.
      select.value = 'US';

      // limit choices:
      select.choices = [{
          value: 'US',
          label: 'United States'
        },
        {
          value: 'CA',
          label: 'Canada'
        }
      ];
    });
  </script>
<?php });

You can find these two-letter country codes by looking at the “Alpha 2″ column here.

How to Add This Code to Your WordPress Site

  • From your WordPress dashboard, navigate to the Installed Plugins > WPCode Lite and click on the Code Snippets.
  • Here, click on the Header & Footer option.
  • Scroll down a little and paste the above code in the Footer section.
  • Click on the Save Changes button to apply these changes.

The countries section will now show up like this on your checkout form:

That’s it! While it is a workaround until a native feature is available, it is an effective solution for those who need to restrict purchases to specific countries.

Be sure to test your checkout process thoroughly after implementing this code to ensure that it is working as intended.

If you face any issues, please feel free to reach out to our support team. We’re always here to help!

Was this article helpful?


Did not find a solution? We are here to help you succeed.

Start Selling With SureCart Today

Start for Free – No Credit Card Required

Start Selling With SureCart Today

Start for Free – No Credit Card Required

Trusted by Thousands of Businesses
Trusted by Thousands of Businesses
Start for Free. No Credit Card Required
World Class Support Team
Scroll to Top