2

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

5
  • 1
    did you include Mage.php 'require_once 'app/Mage.php'; umask(0); Mage::app("admin");
    – Amit Bera
    Apr 14, 2016 at 7:54
  • @AmitBera yes i included require_once 'app/Mage.php'; not umask(0); Mage::app("admin");.after adding those it is not working
    – Mujahidh
    Apr 14, 2016 at 8:03
  • @AmitBera i changed the code like Mage::app('default'); and $customer->getAddresses() instead of $customer->getPrimaryBillingAddress().Now i can get the values.
    – Mujahidh
    Apr 14, 2016 at 8:14
  • You don't need all that crazy mysql hassle, Magento handles it for you when you include app/Mage.php Apr 14, 2016 at 8:24
  • @JulienLachal can you please suggest me a way to reach this.Hope you understood my task.Thank you.
    – Mujahidh
    Apr 14, 2016 at 8:28

2 Answers 2

1

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();
3
  • Thank you,Your comment helped me.but i am not complete my task.
    – Mujahidh
    Apr 14, 2016 at 10:18
  • are you want save getPrimaryBillingAddress address telephone to customer attribute mobile field.Am i right?
    – Amit Bera
    Apr 14, 2016 at 10:20
  • I want to Update the mobile number(custom attribute) value to primary billing address telephone IF telephone field is empty for the same customer.
    – Mujahidh
    Apr 14, 2016 at 10:22
1

I simplify this task than i post the question.For future readers to get some idea i'll post the new way how i did here.Thanks Amith for helping me.

<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

ini_set('max_execution_time',1200);
ini_set('max_input_time',1200);

require_once 'app/Mage.php';
umask(0);
Mage::app('default');

$start = microtime(true);

for($a=1;$a<=57;$a++) {
$customers = mage::getModel('customer/customer')->getCollection()
       ->addFieldToFilter(array(
            array('attribute'=>'mobile','neq'=>'')        
        ))
        ->addAttributeToSelect('default_billing')
       ->setPageSize(2000)
       ->setCurPage($a);
foreach($customers as $customer) {

  $customer_mobile = $customer->getData('mobile');
  $customer->getAddresses();
  if(count($customer->getAddresses())!=0){
     $telephone_number =  $customer->getPrimaryBillingAddress()->getTelephone();

  echo "customer_mobile: ".$customer_mobile."<br />";
  echo "telephone_number: ".$telephone_number."<br />";


  if($telephone_number=='') { 

        $address=Mage::getModel('customer/address')->load($customer->getPrimaryBillingAddress()->getEntityId());
        $address->setTelephone($customer_mobile);
        $address->save();
        echo "Customer(".$customer->getId().") who has this #".$customer_mobile." # MObile number doesent have telephone number Updated SUccessfully</br>";
        $updated_nos++;

    } else{ 

        echo "Customer(".$customer->getId().") who has this #".$customer_mobile." # MObile number HAVE telephone number</br>";

    }
}//if customer address available only update the customer telephone number
}
}

$time_taken = microtime(true) - $start;
echo " Customers Telephone Number update in ".$time_taken." Seconds";
?>

Your Answer

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

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