It’s mandatory for the online store to use the State/Province field in their system. But in some countries, there is no need to include a few states at the time of checkout.

In this article, I will show you how to restrict some US States in the checkout. For this, I have created a US State restriction selection drop-down in the admin panel system configuration. In the example below, you may find Dckap as Namespace and CustomerAddresses as ModuleName.

Step 1

We have to create a system configuration in the admin panel for getting the allowed US states for checkout. Create a system.xml file in the path below and the configuration will be displayed in Stores → Configuration → Sales → Checkout

Path: app/code/Dckap/CustomerAddresses/etc/adminhtml/system.xml

First of all, we need to get the list of the allowed states, so we will need to gather such details from the Magento\Directory\Model\ResourceModel\Region\Collection. From this collection, we will be able to get the allowed country name, and by using the country details, we will get the states’ data, so we will have to override some files for collecting such information.

Path: app/code/Dckap/CustomerAddresses/etc/frontend/di.xml

Then we will have to update the US state list in that multi-select box form using below class.

Dckap\CustomerAddresses\Model\Config\Region\RegionInformationProvider

Path : Dckap\CustomerAddresses\Model\Config\Region\RegionInformationProvider

Planning to implement Magento 2

Our Magento-certified experts come up with best-in-class solutions to improve conversions, increase sales and also provide a great user experience.

Get In Touch

Step 2

In Magento2 admin panel, go to Stores → Configuration from the left-side navigation panel. Then Goto General section → General → Country options → Choose ‘United States’ in Allowed Countries. Because we only list the allowed countries states in our custom system configuration field.

Magento 2 Checkout

And then select the US States from the Allowed US States in Checkout drop-down which you will want to display in the checkout form.

Magento 2 Checkout

Step 3

To get the chosen allowed US States for the checkout page by using the below set of code, we must override the Magento\Directory\Model\ResourceModel\Region\Collection for the checkout form State/Province drop down.

Here,

$this->allowedUsStates => This variable has an array of allowed US state values.

$option => This variable has all the US states.

By using the above after function, we must compare both the state values and return the chosen state value, if that was present in the $option.

Step 4

Open your terminal and go to the Magento 2 root. Run from there with the following command:

Finally, the chosen states will show up in the checkout page state/province selection drop down.

If you found this blog helpful, then please do let us know if you have any queries.

Be the first to Know

Subscribe to our blog to get the latest articles directly to your inbox.

READY TO DRIVE SALES?

Request a Quote Today

Request a Quote

Leave a Reply

4 Comments on "How to restrict specific US states in checkout – Magento 2"


Guest
jos
1 month 6 days ago

Hi Rama,
I ‘ve followed your method but it’s not working for me. I can’t make the configuration show on the checkout backend.

Guest
Eran
1 month 29 days ago

Hello and thank you for this module.
Is there a way to show all states regarding to what i choose in the module on billing form?

Thank you again

Guest
Syed Muneeb Ul Hasan
4 months 24 days ago

Thanks for this great tutorial.

Well, there is one more step that you can mention in your post i,e Running CLI Commands.

As running some CLI commands are necessary like:

php bin/magento setup:upgrade
php bin/magento setup:di:compile
php bin/magento setup:static-content:deploy -f
php bin/magento cache:clean
php bin/magento cache:flush

Keep up the good work 🙂

Rama Lekshmi
Author
3 months 21 days ago

Hi Syed,

Thanks for your feedback. I have updated my blog with the above CLI command.