Customer billing and shipping address with abbreviated state drop down for USA
-
12-12-2019 - |
Frage
With magento 1.9CE i want to display drop down for states abbreviations instead of full state names on the billing and shipping address on the checkout page
Is there is a quick and easy way i could do this ?
Lösung
if you want this for all regions not only US, you need to change the method Mage_Directory_Model_Resource_Region_Collection::toOptionArray
.
Instead of this:
$options = $this->_toOptionArray('region_id', 'default_name', array('title' => 'default_name'));
you need
$options = $this->_toOptionArray('region_id', 'code', array('title' => 'code'));
If you want it only for US regions, you need to modify the data in the table directory_country_region
and directory_country_region_name
. You will identify the state names easily.
Andere Tipps
This is what we did for states and provinces in the United States and Canada. We modified an old extension that no longer works in 1.9.x and now it works.
Create a file app/code/community/Yourname/Stateabrv/Model/Region.php
class Yourname_Stateabrv_Model_Region extends Mage_Directory_Model_Region {
public function getName()
{
$name = $this->getData('name');
if (!is_null($name)) {
/*
* Get country ID, if US will change name to code
*/
$regionCountry = $this->getData('country_id');
if($regionCountry && $regionCountry=='US') {
$name = $this->getData('code');
}
/*
* Get country ID, if Canada will change name to code
*/
if($regionCountry && $regionCountry=='CA') {
$name = $this->getData('code');
}
unset($regionCountry);
}
/*
* If $name is null, will get default region name
*/
if (is_null($name)) {
$name = $this->getData('default_name');
}
return $name;
}
}
Next create app/code/community/Yourname/Stateabrv/etc/config.xml
<?xml version="1.0" encoding="UTF-8"?>
<config>
<modules>
<Yourname_Stateabrv>
<version>0.1.0</version>
</Yourname_Stateabrv>
</modules>
<global>
<models>
<directory>
<rewrite>
<region>Yourname_Stateabrv_Model_Region</region>
</rewrite>
</directory>
</models>
</global>
</config>