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. 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

1 Comment on "How to restrict specific US states in checkout – Magento 2"


Guest
Syed Muneeb Ul Hasan
23 days 1 hour 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 🙂