簡體   English   中英

以編程方式更新magento客戶

[英]update magento customer programmatically

我正在嘗試以編程方式修改magento上的現有客戶數據,但是我遇到了錯誤,希望有人可以對此提供幫助。

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();  

這是錯誤消息

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

您可以將保存新電子郵件的條件修改為:

$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();
 }

更新的條件將避免致命錯誤。

修改后的代碼是

$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();  

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM