简体   繁体   English

创建一个已经存在的客户的客户地址-Magento

[英]Create a customer address to already exist customer - Magento

I'm trying create programmatically a new address to customers that was imported a some time ago for me. 我正在尝试以编程方式为前一段时间为我导入的客户创建一个新地址。

My Code: 我的代码:

//All variables about customer address info are filled
$customerModel = Mage::getModel('customer/customer');
$customer = $customerModel->setWebsiteId(1)->loadByEmail($_email);

if($customer->getId()) {
    $addressData =  array (
        'firstname' => $customer->getFirstname(),
        'lastname' => $customer->getLastname(),
        'street' => "$_s1
$_s2
$_s3
$_s4",
        'city' => $_city,
        'country_id' => 'BR',
        'region_id' => $_regionid,
        'postcode' => $_cep,
        'telephone' => $_tel,
        'celular' => $_cel,
        'is_default_billing' => 1,
        'is_default_shipping' => 1
    );

    $address = Mage::getModel('customer/address');
    $address->addData($addressData);
    $customer->addAddress($address);

    try {
        print_r($addressData);
        $customer->save();
    }
    catch (Exception $e) {
    }
}

Object loaded '$customer' isnt what I need: a full customer object. 对象加载的'$ customer'不是我所需要的:完整的客户对象。 Any Idea? 任何想法?

You have to save customer address in different way, following is address saving code. 您必须以其他方式保存客户地址,以下是地址保存代码。

$customerAddress = Mage::getModel('customer/address');

$customerAddress->setData($addressData)
        ->setCustomerId($customer->getId())
        ->setSaveInAddressBook('1');

$customerAddress->save();

The full code will look like: 完整的代码如下所示:

$customerModel = Mage::getModel('customer/customer');
$customer = $customerModel->setWebsiteId(1)->loadByEmail($_email);

if($customer->getId()) {
    $addressData =  array (
        'firstname' => $customer->getFirstname(),
        'lastname' => $customer->getLastname(),
        'street' => "$_s1
$_s2
$_s3
$_s4",
        'city' => $_city,
        'country_id' => 'BR',
        'region_id' => $_regionid,
        'postcode' => $_cep,
        'telephone' => $_tel,
        'celular' => $_cel,
        'is_default_billing' => 1,
        'is_default_shipping' => 1
    );

    $customerAddress = Mage::getModel('customer/address');

    $customerAddress->setData($addressData)
        ->setCustomerId($customer->getId())
        ->setSaveInAddressBook('1');

    $customerAddress->save();

    //And reload customer object
    $customer = Mage::getModel('customer/customer')->load($customer->getId());
    //Check customer data
    print_r($customer->getData());
    //Check addresses
    foreach($customer->getAddresses() as $address)
    {
        print_r($address);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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