Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get region_id From State Abbreviation - Magento 1.4.2

How can I programmatically get the region_id in Magento from a 2-character state abbreviation? I'm using Magento 1.4.2 if that matters at all.

like image 738
VinnyD Avatar asked Mar 29 '11 18:03

VinnyD


2 Answers

$regionModel = Mage::getModel('directory/region')->loadByCode($regionCode, $countryCode);
$regionId = $regionModel->getId();
like image 126
Christopher Manning Avatar answered Nov 16 '22 15:11

Christopher Manning


Get the collection of all the states/regions related to specific country.

/**
 * Get region collection
 * @param string $countryCode
 * @return array
 */
public function getRegionCollection($countryCode)
{
    $regionCollection = Mage::getModel('directory/region_api')->items($countryCode);
    return $regionCollection;
}

Populate region list with region collection. Country code (e.g. NL, NP, EN) is passed as parameter to getRegionCollection function.

$regionCollection = $this->getRegionCollection($countryCode);

<select name='customer[region]' id='customer:region' class="validate-select" >
    <option>Please select region, state or province</option>
    <?php
        foreach($regionCollection as $region) {
            ?>
            <option value="<?php echo $region['name'] ?>" ><?php echo $region['name'] ?></option>
            <?php
        }
    ?>

</select> 
like image 33
Oğuz Çelikdemir Avatar answered Nov 16 '22 14:11

Oğuz Çelikdemir