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).
setCustomer($customer)
instead ofsetCustomerId(...)
? 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$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.