Question

The following is some code to save an address to the customer address book... what value do I need to be putting in the region_id and is it needed?

  $_custom_address = array (
                        'firstname' => 'Honkey',
                        'lastname' => 'Joseph',
                        'street' => array (
                            '0' => 'Sample address part1 SECOND',
                            '1' => 'Sample address part2 SECOND',
                        ),
                        'city' => 'Orlando',
                        'region_id' => '',
                        'region' => 'MN',
                        'postcode' => '31000',
                        'country_id' => 'US',
                        'telephone' => '0038531555444',
                    );
                    $customAddress = Mage::getModel('customer/address');
                    //$customAddress = new Mage_Customer_Model_Address();
                    $customAddress->setData($_custom_address)
                                ->setCustomerId($customer->getId())
                                ->setSaveInAddressBook('1');
                    try {
                        $customAddress->save();
                    } 
                    catch (Exception $ex) {
                        Mage::log('Could not save customer',null,'caitlin.log');
                        Mage::log($ex->getMessage(),null,'caitlin.log');
                    }
Was it helpful?

Solution

In the default settings for customer addresses the region_id is required. So if you add an address for a customer via this code they would not be able to edit it or use it to checkout without updating the address first to include the region.

With regards to which value it should be the matching id of the region that has been selected. You can find these by viewing the front end of the website or by looking into your database at the table "directory_country_region".

The region attribute uses a source model of type: Mage_Customer_Model_Resource_Address_Attribute_Source_Region

Which loads the options via the following code:

public function getAllOptions()
{
    if (!$this->_options) {
        $this->_options = Mage::getResourceModel('directory/region_collection')->load()->toOptionArray();
    }
    return $this->_options;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top