Magento: Remove the region from address form

Tweet about this on TwitterShare on LinkedInShare on Google+Share on FacebookEmail this to someone

Today I worked on task that required the region filed to be removed from all Magento address forms. I found some Magento modules that were doing the trick, but I also wanted to delve a bit more deeper and to see if I can do this by configuration from Magento admin. Luckily in the most recent version of the platform ( Magento CE 1.9 ) this is possible.

By some simple actions in the Magento admin you will be able to exclude the region field from:

  • Customer add / edit address form
  • Customer register form
  • Billing and Shipping address form on the checkout page

Below I am going to show 2 different solutions how to hide the region filed. The first is more suitable for Magento admins / store owners and the second is something that the developers can use in their data upgrade scripts.

For the both example below I assume that we have to remove the region if the country is Germany.

Remove the region field by changing the Magento admin configuration:

This is how the billing address form looks before the admin configuration is not applied.

Magnto 1.9 - region field visible in the address forms

To remove the region field you have to follow these steps:

– In Magento admin go to: System -> Configuration -> General
– In the fields group “State Options” exclude Germany from “State is required for” multiselect and change “Display not required State” to No

Magnto 1.9 - remove region field from address forms

This should do the trick the the region field should not be displayed in the address forms anymore.

Magnto 1.9 - region field removed from address forms

Remove the region field by changing the Magento admin configuration:

To make this work it’s very important to use data upgrade script, so don’t try to use use the upgrade scripts that you put in the sql folder of your modules. The data upgrade script should be put in the data folder of your modules. For more information you can check this article – Using Magento >= 1.6 data install scripts.

We assume that your data upgrade scrip is named data-upgrade-0.0.2-0.0.3.php

<?php /* @var $installer Mage_Catalog_Model_Resource_Setup */ $installer = $this; $installer->startSetup();

// 1. Changing the display config of non required regions

/** @var Mage_Core_Model_Config $coreConfig */
$coreConfig = Mage::getModel('core/config');
$coreConfig->saveConfig('general/region/display_all', 0);

// 2. Exclude Germany from the countries with required region

$regionRequiredCountries = explode(',', Mage::getStoreConfig('general/region/state_required'));

$regionRequiredCountries = array_diff($regionRequiredCountries, array('DE'));
$regionRequiredCountries = implode(',', $regionRequiredCountries);

$coreConfig->saveConfig('general/region/state_required', $regionRequiredCountries);

$installer->endSetup();

Feel free to use and modify this data upgrade script. For example you may want to remove the region field for more countries, for example Austria. Then you can use array(‘DE’, ‘AT’) rather than array(‘DE’).

This solution is not perfect, because if you run more websites or store views in one Magento instance the region field will be removed from all websites. In my case it works for me and I don’t need to bloat Magento with any third party modules.

Your thoughts / questions?

Tweet about this on TwitterShare on LinkedInShare on Google+Share on FacebookEmail this to someone

Tsvetan Stoychev

Tsvetan aka. Cecko is the founder of Cecko's Lab. He is Magento addicted since Magento CE 1.2.1.2 and has worked on over 30 Magento projects. At the moment he is in charge to take care about the money flow of the company, to keep constant communication with the clients and to keep the people in the office busy.

More Posts

Follow Me:
TwitterLinkedIn