phpmagentoregistration

Magento registration with Billing & Shipping address


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

enter image description here


Solution

  • 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