2

I'm trying to modify existing customers data on magento programmatically, but i have errors, hope someone can help me with this.

require_once('../app/Mage.php');
ini_set("error_reporting",E_ALL);
ini_set("display_errors",true);
umask (0);
Mage::app('admin');

$email = $_REQUEST['email'];
$firstname = $_REQUEST['firstname'];
$lastname = $_REQUEST['lastname'];
$newEmail = $_REQUEST['newEmail'];
$method = $_REQUEST['method'];
$user_id = $_REQUEST['user_id'];


$customer = Mage::getModel('customer/customer');
        $customer->setWebsiteId(Mage::app()->getWebsite()->getId());
        $customer->loadByEmail($email);
        $customer->setFirstname = $firstname; 
        $customer->setLastname = $lastname; 
        $customer->setEmail = $newEmail; 
        $customer->save();  

Here is the error message

Fatal error: Uncaught exception 'Mage_Customer_Exception' with message 'Customer email is required' in /home/insateck/www/zzWEBS/MAGENTO/ZZpruebasEXPRIMENET/app/Mage.php:580 Stack trace: #0 /home/insateck/www/zzWEBS/MAGENTO/ZZpruebasEXPRIMENET/app/code/core/Mage/Customer/Model/Resource/Customer.php(76): Mage::exception('Mage_Customer', 'Customer email ...') 
#1 /home/insateck/www/zzWEBS/MAGENTO/ZZpruebasEXPRIMENET/app/code/core/Mage/Eav/Model/Entity/Abstract.php(1122): Mage_Customer_Model_Resource_Customer->_beforeSave(Object(Mage_Customer_Model_Customer)) 
#2 /home/insateck/www/zzWEBS/MAGENTO/ZZpruebasEXPRIMENET/app/code/core/Mage/Core/Model/Abstract.php(318): Mage_Eav_Model_Entity_Abstract->save(Object(Mage_Customer_Model_Customer))
#3 /home/insateck/www/zzWEBS/MAGENTO/ZZpruebasEXPRIMENET/syncadaptax/update_client_methods.php(33): Mage_Core_Model_Abstract->save() 
#4 {main} thrown in /home/insateck/www/zzWEBS/MAGENTO/ZZpruebasEXPRIMENET/app/Mage.php on line 580
2
  • What's the output of var_dump($_REQUEST['newEmail']);?
    – Adelmar
    Jul 11, 2014 at 23:08
  • The output is string([email protected])
    – Vincent
    Jul 11, 2014 at 23:40

2 Answers 2

5

You can modify the condition for saving the new email as:

$customer = Mage::getModel('customer/customer');
$customer->setWebsiteId(Mage::app()->getWebsite()->getId());
$customer->loadByEmail($email);
if($customer->getId()>1){
     $customer2 = Mage::getModel('customer/customer');
     $customer2->setWebsiteId(Mage::app()->getWebsite()->getId());
     $customer2->loadByEmail($newEmail);
     if($customer2->getId()<1){
        $customer->setEmail($newEmail); 
     }
     $customer->setFirstname($firstname); 
     $customer->setLastname ($lastname); 
     $customer->save();
 }

The updated condition will avoid the fatal error.

0
0

Modified code is

$customer = Mage::getModel('customer/customer');
        $customer->setWebsiteId(Mage::app()->getWebsite()->getId());
        $customer->loadByEmail($email);
        if($customer->getId()<1){
            $customer->setEmail($newEmail); 

    }
    $customer->setFirstname($firstname); 
        $customer->setLastname ($lastname); 
        $customer->save();  
5
  • No I have this error Fatal error: Uncaught exception 'Mage_Customer_Exception' with message 'This customer email already exists' Im not sure i was clear; what im trying to do is to update an existing client, the name, last name or even, if its possible, the email
    – Vincent
    Jul 12, 2014 at 4:25
  • @Vincent setFirstname(), setLastname(), setEmail() are methods. Compare $customer->setFirstname($firstname); from the answer and yours $customer->setFirstname = $firstname; Jul 12, 2014 at 6:34
  • @PavelNovitsky that new error is with the implementation of the answer... :(
    – Vincent
    Jul 12, 2014 at 6:41
  • Never mind the answer works great for the name and last name, but it does not update/modify the email...
    – Vincent
    Jul 12, 2014 at 6:52
  • i have busy,will reply later
    – Amit Bera
    Jul 12, 2014 at 7:23

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

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

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