Solved

Programmatically Change the Telephone number on an exisiting user in Magento

Posted on 2013-05-22
4
2,993 Views
Last Modified: 2016-03-02
I have code that sucessfully sets the phone number when creating a new user in Magento programmatically.

$customer = Mage::getModel('customer/customer');
$customer->setWebsiteId(Mage::app()->getWebsite()->getId());
            
  //setting data such as email, firstname, lastname, and password
$customer->firstname = $firstname;
$customer->lastname = $lastname;
$customer->email = $email;
$customer->password_hash = md5($password);
if($customer->save())
{
      $address = Mage::getModel("customer/address");
      $address->setCustomerId($customer->getId());
      $address->setFirstname($customer->firstname);
      $address->setLastname($customer->lastname);
      $address->setTelephone("55555555555);
                  $address->save();
      echo $customer->firstname." ".$customer->lastname." information is saved!";
}            

However, I can't my code to work on an existing customer:                  
      
$customer = Mage::getModel('customer/customer');
$customer->loadByEmail($email);
if($customer->getId())
{
      $address = Mage::getModel("customer/address");  
      $address->setTelephone($points);
      $address->save();
}

What am I doing wrong?
0
Comment
Question by:kenozzy
  • 2
  • 2
4 Comments
 
LVL 26

Expert Comment

by:EddieShipman
ID: 39193033
Try using
$address->setSaveinAddressBook('1');

Open in new window

before saving the $address.
0
 

Author Comment

by:kenozzy
ID: 39195340
Ok, here's the code with your addition... this does update the phone number, but it creates a new address every time.

	$customer = Mage::getModel('customer/customer');
	$customer->loadByEmail($email);
	if($customer->getId()) 
	{
		$address = Mage::getModel("customer/address"); 
  	                  $address->setCustomerId($customer->getId());
		$address->setTelephone($points);
		$address->setSaveinAddressBook('1'); 
		$address->save();
                  }

Open in new window

0
 
LVL 26

Accepted Solution

by:
EddieShipman earned 500 total points
ID: 39197056
What we do is delete the address that we are modifying and then when it creates the new one...
0
 

Author Closing Comment

by:kenozzy
ID: 39215887
Deleting first worked great.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An enjoyable and seamless user experience can go a long way on an eCommerce site. While a cohesive layout and engaging copy play roles in creating a positive user experience, some sites neglect aspects that seem marginal but in actuality prove very …
There’s a good reason for why it’s called a homepage – it closely resembles that of a physical house and the only real difference is that it’s online. Your website’s homepage is where people come to visit you. It’s the family room of your website wh…
This video teaches users how to migrate an existing Wordpress website to a new domain.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question