How to add state drop down based on country selected in magento product form

How to add state drop down based on country selected in magento product form

July 7, 2026 · Illustrated author avatarBy Stefan Bogdanescu
How to add state drop down based on country selected in magento product form

Adding State Dropdown Based on Country Selection in Magento Product Form

Are you looking to display a state dropdown that changes based on the selected country in your Magento product form? I understand how challenging it can be to find the right solution for this common requirement.

In this post, we’ll guide you through a step-by-step process of adding a dynamic state dropdown to your Magento admin product creation page. This will ensure that only relevant states are displayed based on the selected country.

Prerequisites

Before starting, make sure you have:

  • A basic understanding of Magento’s admin area and customization
  • Access to your store’s backend with administrative privileges
  • Familiarity with editing XML and PHP files

Step 1: Create Your Custom State Model

Firstly, create a new file in app/code/Vendor/Module/etc named di.xml. This is where we’ll define our custom state model.

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Directory\Model\Config\Source\Country">
        <arguments>
            <argument name="data" xsi:type="array">
                <item name="country_collection" xsi:type="string">Vendor\Module\Model\ResourceModel\State\Collection</item>
            </argument>
        </arguments>
    </type>
</config>

Ensure you replace Vendor and Module with your actual vendor and module names.

Step 2: Create Your Custom State Collection

Next, create a new file in app/code/Vendor/Module/etc named di.xml. This is where we’ll define our custom state collection.

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Vendor\Module\Model\ResourceModel\State\CollectionFactory">
        <arguments>
            <argument name="data" xsi:type="array">
                <item name="model" xsi:type="string">Vendor\Module\Model\ResourceModel\State</item>
                <item name="service" xsi:type="string">Magento\Framework\App\ResourceConnection</item>
                <item name="interfaceName" xsi:type="string">Magento\Directory\Model\ResourceModel\State\enditem>
            </argument>
        </arguments>
    </type>
</config>

Step 3: Configure Your State Dropdown

Now, we need to configure the state dropdown in our app/code/Vendor/Module/etc/adminhtml/system.xml file.

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
    <system>
        <tab id="general" translate="label" sortOrder="10">
            <section id="Vendor_Module" showInDefault="1" showInWebsite="1" showInStore="1" translate="label">
                <label>State Configuration</label>
                <group id="general" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
                    <field id="Vendor_Module_state_field" translate="label" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1">
                        <label>Select State</label>
                        <frontend_type>select</frontend_type>
                        <source_model>Magento\Directory\Model\Config\Source\Country\State</source_model>
                    </field>
                </group>
            </section>
        </tab>
    </system>
</config>

Step 4: Update Your Product Form

Finally, update your app/code/Vendor/Module/view/adminhtml/layout/catalog_product_index.xml file to include the state dropdown in the product form.

<referenceBlock name="catalog_product_form" before="-">
    <block class="Magento\Directory\Block\Adminhtml\State" template="directory/state.phtml" group="product_form"/>
</referenceBlock>

Conclusion

Adding a dynamic state dropdown to your Magento admin product form is now possible with these steps. By creating custom models and collections, configuring the field in the system.xml file, and updating the layout XML file, you can achieve the desired functionality.

Remember that this solution requires some technical knowledge of Magento’s architecture and file structure. If you’re not comfortable making these changes yourself, I recommend contacting a professional developer or reaching out to our expert team at Laravel Company.

With this guide, you should now be able to display a state dropdown that automatically adjusts based on the selected country in your Magento product form. Happy customizing!

Catalog Extensions Admin Configuration

Our network