0

Is it possible to save the customer details as well as the address details in the same controller? When I try, it fails saying "customer email is required".

Here's my code:

$customer = Mage::getModel("customer/customer");
$customer   ->setWebsiteId($websiteId)
        ->setStore($store)
        ->setFirstname('John')
        ->setLastname('Doe')
        ->setEmail('[email protected]')
        ->setPassword('somepassword');

$address = Mage::getModel("customer/address");
$address->setCustomerId($customer->getId())
    ->setFirstname($customer->getFirstname())
    ->setMiddleName($customer->getMiddlename())
    ->setLastname($customer->getLastname())
    ->setPostcode('Test')
    ->setCity('Test')
    ->setTelephone('01234567890')
    ->setFax('01234567890')
    ->setCompany('Test')
    ->setStreet('Kersov')
    ->setIsDefaultBilling('1')
    ->setIsDefaultShipping('1')
    ->setSaveInAddressBook('1');

try {
    $customer->save();
    $address->save();
}
catch (Exception $e) {
    Mage::getSingleton('core/session')->addError('There has been a problem with your registration. ' . $e->getMessage());
}

If I comment out $address->save() it successfully saves the customer (but obviously not the address).

3
  • What if you do setCustomer($customer) instead of setCustomerId(...)? It would improve performance because your object is already loaded, but I wonder if it wouldn't solve your issue too. Something spooky must be happening when the model is loaded Nov 3, 2016 at 16:54
  • 1
    It should not work. you have used $customer->getId() to set in address model. but still that id doesn't exit. You should save csutomer first then load customer model to get id and then you can save address.
    – Kul
    Nov 3, 2016 at 16:57
  • @Liam McArthur I have posted working code. Please check It should work for you as well.
    – Kul
    Nov 3, 2016 at 18:42

1 Answer 1

1

Please try below code. it will work. in your code you have used customer id which still doesn't exit. id will generate after save customer.

$websiteId = Mage::app()->getWebsite()->getId();
$store = Mage::app()->getStore();

$customer = Mage::getModel("customer/customer");
$customer   ->setWebsiteId($websiteId)
            ->setStore($store)
            ->setFirstname('John')
            ->setLastname('Doe')
            ->setEmail('[email protected]')
            ->setPassword('somepassword');         
    $customer->save();


$address = Mage::getModel("customer/address");
$address->setCustomerId($customer->getId())
    ->setFirstname($customer->getFirstname())
    ->setMiddleName($customer->getMiddlename())
    ->setLastname($customer->getLastname())
    ->setPostcode('Test')
    ->setCity('Test')
    ->setTelephone('01234567890')
    ->setFax('01234567890')
    ->setCompany('Test')
    ->setStreet('Kersov')
    ->setIsDefaultBilling('1')
    ->setIsDefaultShipping('1')
    ->setSaveInAddressBook('1');    
    $address->save();

Hope this will help.

3
  • This makes perfect sense and works perfectly. Thank you for your swift solution! Nov 4, 2016 at 9:42
  • I know all the states of the customer, How can i add the state to all the customers, my magento ver 1.9.2.3
    – zus
    Apr 19, 2018 at 7:45
  • 1
    @zus you can try setRegion() or setRegionId()
    – Kul
    Apr 19, 2018 at 12:51

Your Answer

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.