|
/ Knowledge Base /Checkout/ How to Restrict Purchases to Specific Countries

How to Restrict Purchases to Specific Countries

To restrict purchases to specific countries, you can add the following code to your functions.php file.

Alternatively, you can use a code snippet plugin like WPCode or Code Snippets to add this code to your website’s footer.

This guide will walk you through adding the code using the WPCode plugin, though you’re free to use any code snippet plugin you prefer.

Restricting Purchases to Specific Countries

First if you want to add a specific country to be selected by default, you can just update the Checkout form > Address block > Default country setting

Then, Use the following code to restrict purchases to selected countries of your choice.

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

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

You can easily modify this code to allow purchases from any country of your choice.

In this example, we’ll customize the code to allow purchases only from the US and Canada, with the US set as the default country. The default country will appear as the first choice in the dropdown menu.

To customize:

• Replace “YOUR_COUNTRY_CODE” with the two-letter code of the country you want to allow purchases from.
• Replace “YOUR_COUNTRY_NAME” with the name of that country.

The modified code to restrict purchases to only the US and Canada is as follows:

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

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

You can find the two-letter country codes in the “Alpha-2” column here.

Restrict Countries for both Shipping and Billing addresses

To update the allowed countries for both the billing and shipping address fields, use this code snippet. For example, if you want only the United States to be available for both billing and shipping addresses, apply the following code.

add_action('wp_footer', function() { ?>

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

        if (!!shippingSelect) {
            // Limit choices:
            shippingSelect.choices = [
                {
                    value: 'US',
                    label: 'United States',
                },
            ];
        }

        const updateBillingAddress = () => {
		const billingAddressElement = document.querySelector('sc-order-billing-address')?.shadowRoot?.querySelector('sc-address, sc-compact-address');
		const billingSelect = billingAddressElement?.shadowRoot?.querySelector('sc-select');

            if (!!billingSelect) {
                // Limit choices:
                billingSelect.choices = [
                    {
                        value: 'US',
                        label: 'United States',
                    },
                ];
            }
        };
	
	// fire update billing address on initial load.
	updateBillingAddress();
	
        // Add event listener to the checkbox
        const billingCheckbox = document.querySelector('sc-order-billing-address')?.shadowRoot?.querySelector('sc-checkbox')?.shadowRoot?.querySelector('input');

        billingCheckbox.addEventListener('change', (event) => {
		setTimeout(() => {
			updateBillingAddress();
		}, 50)
        });
    });
</script>
<?php });

In the code snippet above, both shipping and billing address countries are restricted to the United States by default. To allow multiple countries, simply add more options to the choices array, as shown below:

// For shipping choices.
shippingSelect.choices = [
    { value: 'US', label: 'United States' },
    { value: 'CA', label: 'Canada' }
];

// For billing choices.
billingSelect.choices = [
    { value: 'US', label: 'United States' },
    { value: 'CA', label: 'Canada' }
];

This flexibility allows you to restrict purchases to any combination of countries you choose for both shipping and billing addresses.

How to Add This Code to Your WordPress Site

  • In your WordPress dashboard, go to Installed Plugins > WPCode Lite and click on Code Snippets.
  • In the WPCode plugin interface, navigate to Code Snippets > Header & Footer as shown in the screenshot below.
  • Scroll down slightly and paste the code into the Footer section as shown below.
  • Then, click on the Save Changes button to apply the changes.

To add a default country selection in checkout forms, go to Address > Default Country and select United States (or your preferred country).

The country section will now appear like this on your checkout form:

That’s it! While this is a temporary workaround until a native feature is available, it effectively allows you to restrict purchases to specific countries.

Be sure to thoroughly test your checkout process after implementing this code to ensure everything is functioning as expected.

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

Was this doc helpful?
What went wrong?

We don't respond to the article feedback, we use it to improve our support content.

Need help? Contact Support
On this page

Download is Just A Click Away!

Enter your email address and be the first to learn about updates and new features.

E-Commerce Store Launch Checklist Download
Scroll to Top