1
votes

Is it possible that when user register in Magento, that time he also saves his Addresses (Billing & Shipping)

enter image description here

5

5 Answers

6
votes

Create a module with a controller that extends the Mage_Customer_AccountController, containing createPostAction(). I duplicated the bit that handles the billing address, find this if-block:

if ($this->getRequest()->getPost('create_address')) { 

And add this to the end of it:

if ($this->getRequest()->getPost('create_shipping_address')) {
$shippingAddress = Mage::getModel('customer/address');
$shippingAddressForm = Mage::getModel('customer/form');
$shippingAddressForm->setFormCode('customer_register_address')
    ->setEntity($shippingAddress);

$shippingAddressData = array(
    'firstname'  => $addressData['firstname'],
    'lastname'   => $addressData['lastname'],
    'company'    => $this->getRequest()->getPost('shipping_company'),
    'street'     => $this->getRequest()->getPost('shipping_street'),
    'city'       => $this->getRequest()->getPost('shipping_city'),
    'country_id' => $this->getRequest()->getPost('shipping_country_id'),
    'region'     => $this->getRequest()->getPost('shipping_region'),
    'region_id'  => $this->getRequest()->getPost('shipping_region_id'),
    'postcode'   => $this->getRequest()->getPost('shipping_postcode'),
    'telephone'  => $this->getRequest()->getPost('shipping_telephone'),
    'fax'        => $this->getRequest()->getPost('shipping_fax')
    );

$shippingAddressErrors = $addressForm->validateData($shippingAddressData);

if ($shippingAddressErrors === true) {
    $shippingAddress->setId(null)
        ->setIsDefaultBilling($this->getRequest()->getParam('shipping_default_billing', false))
        ->setIsDefaultShipping($this->getRequest()->getParam('shipping_default_shipping', false));

    $shippingAddressForm->compactData($shippingAddressData);

    $customer->addAddress($shippingAddress);

    $shippingAddressErrors = $shippingAddress->validate();

    if (is_array($shippingAddressErrors)) {
        $errors = array_merge($errors, $shippingAddressErrors);
    }
} else {
    $errors = array_merge($errors, $shippingAddressErrors);
}}

Of course you also need to duplicate the form in your themes template/customer/form/register.html, specifically the code inside this if-block:

if($this->getShowAddressFields()): ?>

Prefix all the field names IDs and in the copied code with shipping_. In the JavaScript at the bottom you need to duplicate the RegionUpdater line, like so:

new RegionUpdater('country',          'region',          'region_id', <?php echo   $this->helper('directory')->getRegionJson() ?>, undefined, 'zip');
new RegionUpdater('country', 'shipping_region', 'shipping_region_id', <?php echo $this->helper('directory')->getRegionJson() ?>, undefined, 'zip'); 

(almost) complete code can be found here:

AccountController.php: http://pastebin.com/9h9HqYAa

register.html: http://pastebin.com/Q7EawU7L

It works perfectly

1
votes

There is a way you can have one address input while registration.

Go to : template/customer/form/register.phtml and if($this->getShowAddressFields())

Just forcefully alter this condition and you will get address fields there.

0
votes

To add to Massi and Steve's answer, in Magento 1.9.0.1, I am just learning but I was able to get this to work by adding that code to the end of _getErrorsOnCustomerAddress function in an extension of Mage_Customer_AccountController.

-1
votes

Not in the default magento version. you must search for an extension for this or write your own "observer" to add an address on the same time of the registration.

-1
votes

Like butcher said,adding that code to the end of _getErrorsOnCustomerAddress. I just tried it, and it works fine for me. And I also set the value for "1" instead of "0" in the code

<input type="hidden" name="create_shipping_address" value="0" />

(look the link that Steve and Massi have given register.html: http://pastebin.com/Q7EawU7L)

Im on Magento 1.9.0.1