Magento 1.7 – How to Update Customer Telephone in Billing Address

billing-addresscustomer-addressmagento-1.7

I want to update the customer telephone number if the customer doesn't have.
In our website we disabled the telephone mandatory in adding address field.
We recently implement a payment gateway that the telephone field is mandatory.
I want to update the customer telephone table if the customer doesn't have the telephone number in there shipping or billing address.
I tried this way.

Created a file update_phone.php in the root

require_once 'app/Mage.php';

$conn = mysql_connect("localhost","user","password");
$db   = mysql_select_db("dbname",$conn);

$sql = "SELECT ce.entity_id,ce.email, ea.attribute_code, cev.value 
    FROM customer_entity AS ce
    LEFT JOIN eav_attribute AS ea ON ce.entity_type_id = ea.entity_type_id AND ea.backend_type = 'varchar'
    LEFT JOIN customer_entity_varchar AS cev ON ce.entity_id = cev.entity_id AND ea.attribute_id = cev.attribute_id
    WHERE attribute_code = 'mobile'
    ORDER BY entity_id DESC LIMIT 1";

//mobile is a custom attribute it will collect during registration

$result = mysql_query($sql);

while($row = mysql_fetch_assoc($result)){
    $customerId = $row['entity_id'];
    echo $customerId;//Upto this line code is working. 
    $customer = Mage::getModel('customer/customer')
        ->load($customerId)->getData(); // insert customer ID
    //Mage::getModel('customer/customer')->load($id)->getData();

    foreach ($customer->getPrimaryBillingAddress() as $address)
    {
        $data = $address->toArray();
        var_dump($data);
    }
}

Up to echo $customerId; code is running.

My scope is get the mobile number and check the billing telephone number is available in the customer address with the customer id and if telephone number is not available want to update with mobile number.
please Help me in this task

Best Answer

for getting data change

from

$customer = Mage::getModel('customer/customer')
        ->load($customerId)->getData(); // insert customer ID
    //Mage::getModel('customer/customer')->load($id)->getData();

    foreach ($customer->getPrimaryBillingAddress() as $address)
    {
        $data = $address->toArray();
        var_dump($data);
    }

to

   $customer = Mage::getModel('customer/customer')
        ->load($customerId); // insert customer ID
    //Mage::getModel('customer/customer')->load($id)->getData();
    echo "<pre>";
    print_r( $customer->getPrimaryBillingAddress()->getData());

If you want to update primary billing address telephone from mobile number(custom attribute) IF telephone field is empty

then using customer address model(Mage::getModel('customer/address')) update this address telephone.

$address=Mage::getModel('customer/address')->load($customer->getPrimaryBillingAddress()->getEntityId());
$address->setTelephone($customer->getMobile());
$address->save();
Related Topic