Solved

Programmatically Change the Telephone number on an exisiting user in Magento

Posted on 2013-05-22
4
2,809 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
Comment Utility
Try using
$address->setSaveinAddressBook('1');

Open in new window

before saving the $address.
0
 

Author Comment

by:kenozzy
Comment Utility
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
Comment Utility
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
Comment Utility
Deleting first worked great.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

744 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now