Solved

Programmatically Change the Telephone number on an exisiting user in Magento

Posted on 2013-05-22
4
3,055 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

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

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

Does your audience prefer people in photos or no people? How can you best highlight what you’re selling? What are your competitors doing, and what can you do that is different and unique from them?  Continue reading to learn how to make your images …
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

730 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