Tag Archives: address

Magento: Changing street line count of customer address programatically

Nowadays I’m facing interesting programming challenges. The last one was the changing of street line count of a customer address. As you may know, Magento doesn’t have a good documentation for similar situations. So here it is how I did it.

Create an upgrade script in your module. Inside you should have something like:

$installer = $this;
$installer->startSetup();
$db = $this->getConnection();
$path = "customer/address/street_lines";
$value = 3; // change this with your new street line value
// here you will read the config id of the street line
// if exists update it, else insert it, naturally you could have used a replace into syntax also
// this is just for simplicity. Be careful, this solution does not treat cases when
// you are using multiple stores with different settings
$sql = $db->prepare("
    SELECT config_id
    FROM core_config_data 
    WHERE 
        path = :path
    ");
$sql->execute(array(
    ":path" => $path
));
$result = $sql->fetch();
if (!empty($result))
{
    $sql = $db->prepare("
        UPDATE core_config_data
        SET
            value = :value
        WHERE
            config_id = :config_id
    ");
    $sql->execute(array(
        ":config_id" => $result['config_id'],
        ":value" => $value
    ));
}
else
{
    $sql = $db->prepare("
        INSERT INTO core_config_data (path, value)
        VALUES(:path, :value)
    ");
    $sql->execute(array(
        ":path" => $path,
        ":value" => $value
    ));
}
// next step is to read the attribute id of "street" from the eav_attribute table
$sql = $db->prepare("
    SELECT attribute_id
    FROM eav_attribute
    WHERE 
        entity_type_id = 2 AND
        attribute_code = 'street'
    ");
$sql->execute();
$result = $sql->fetch();
$streetAttributeId = $result['attribute_id'];
// update "multiline_count" column in "customer_eav_attribute" table
$sql = $db->prepare("
UPDATE customer_eav_attribute
SET multiline_count = :value
WHERE
attribute_id = :street_attribute_id
");
$sql->execute(array(
":value" => $value,
":street_attribute_id" => $streetAttributeId
));

$installer->endSetup();

Briefly: you have to save in core_config_data and also in customer_eav_attribute table your new street line value.