3

I am working on script where i display all address with respected design. Now i want to update address of user by its entity id, i have multiple array of address like this

Array
(
    [0] => Array
        (
            [address_line2] => 
            [postal_code] => 123456
            [web_site_id] => 18
            [country] => US
            [updated_at] => 
            [address_line1] => 45,123 apna
            [local_id] => 
            [store_id] => 20
            [customer_id] => 974
            [city] => ALBESTA
            [resource_type] => 
            [state] => Alabama
            [id] => 992
            [is_active] => 1
            [email] => test@test.com
            [phone_number] => 
            [device_model] => 
            [is_sync] => 0
            [created_at] => 
            [is_deleted] => 0
        )
)

In above array "id" is entity of address field. And i want to update other records of array.

2 Answers 2

7

Replace this $address->setData($addressCode, $addressData[$addressValue]); with $address->setData($addressCode, $addressValue);

 <?php
    $addressId   = '5';
    $addressData = array(
        'postal_code' => 123456,
        'country' => 'US'
    );
    $address     = Mage::getModel('customer/address')->load($addressId);
    $address->setCustomerId($address->getCustomer()->getId());
    foreach ($addressData as $addressCode => $addressValue) {
        if (isset($addressData[$addressCode])) {
            $address->setData($addressCode,$addressValue);
        }
    }
    try {
        $address->setId($addressId);
        $address->save();
    }
    catch (Mage_Core_Exception $e) {
        echo $e->getMessage();
    }
    ?>
3

Try this

<?php
   $addressId = '5';
$addressData = array(
    'postal_code' => 123456,
    'country' => 'US'
);
$address = Mage::getModel('customer/address')->load($addressId);
$address->setCustomerId($address->getCustomer()->getId());
foreach($addressData as $addressCode => $addressValue) {
    if (isset($addressData[$addressCode])) {
        $address->setData($addressCode, $addressData[$addressValue]);
    }
}
try {
    $address->setId($addressId);
    $address->save();
} catch (Mage_Core_Exception $e) {
    echo $e->getMessage();
}
    ?>
5
  • It is giving me error Call to a member function getStore() on a non-object in. After "$address->save();".
    – Mathew
    Nov 24, 2015 at 12:15
  • updated code please check
    – Abdul
    Nov 24, 2015 at 12:30
  • @Abdul Is there any way to update in Database?
    – Gem
    Jan 9, 2019 at 9:24
  • @Gem which fields updates in database?
    – Abdul
    Jan 9, 2019 at 9:27
  • @Abdul How to update Customer Country in the database.
    – Gem
    Jan 10, 2019 at 4:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.