简体   繁体   中英

How to edit Magento current logged user details via php code

$session = Mage::getSingleton('customer/session'); $customer_id = $session->getId(); $customer_data = Mage::getModel('customer/customer')->load($customer_id); print_r($customer_data);

通过此代码,我可以获得用户详细信息,我需要知道如何使用Php代码以相同的方式编辑用户详细信息,如地址,密码..etc

for Password you can set using $customer_id

$password = 'Any Things'
$customer = Mage::getModel('customer/customer')->load($customer_id);
$customer->setPassword($password);
$customer->save();

For Edit address you have to load address model

For Example if you want to edit billing Address :

 $customer = Mage::getModel('customer/customer')->load($customer_id);
 $address = $customer->getDefaultBilling();

 $address->setFirstname("Test");
 $address->save();

OR : using address id get from customer object :

   $address = Mage::getModel('customer/address')->load($customerAddressId);
   $address->setFirstname("Test"); 
   $address->save();

You can use php magic get and set methods

Suppose to set password you can use $customer_data->setPassword('1234567');

$customer_data->save();

For customer address

$_custom_address = array (
'firstname' => 'firstname',
'lastname' => 'lastname',
'street' => array (
    '0' => 'Sample address part1',
    '1' => 'Sample address part2',
),
'city' => 'city',
'region_id' => '',
'region' => '',
'postcode' => '31000',
'country_id' => 'US', 
'telephone' => '0038531555444',
);

$customAddress = Mage::getModel('customer/address')

$customAddress->setData($_custom_address)
        ->setCustomerId($customer->getId())
        ->setIsDefaultBilling('1')
        ->setIsDefaultShipping('1')
        ->setSaveInAddressBook('1');

try {
    $customAddress->save();
}
catch (Exception $ex) {
    //Zend_Debug::dump($ex->getMessage());
}

For more info http://inchoo.net/ecommerce/magento/programming-magento/programatically-create-customer-and-order-in-magento-with-full-blown-one-page-checkout-process-under-the-hood/

You can use methods of Mage_Customer_Model_Customer class:

$customerSession = Mage::getSingleton('customer/session');
$customerModel = Mage::getModel('customer/customer')->load($customerSession->getId());
$customerModel->changePassword('new_password');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM