Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Creating Different Billing and Shipping Addresses Upon Registration
 
CreedFeed
Member
 
Total Posts:  74
Joined:  2007-08-31
Milwaukee, WI
 

I’m having a bit of a problem trying to figure out how to create two new addresses for customers upon registering with my store. I have created a custom registration form (see the attacked image screenshot) and after looking at some of the code in the basic registration form there was some base code in there to allow the user to input address data and create two addresses during registration. However the way that was set up appears to only allow the customer to input one address and that input was set for both the billing and shipping addresses.

What I would like to do is create two addresses but each uses the data from the appropriate fields from my form. In my custom form, all I did was copy the basic address fields that were used in the form and appended “ship_” to the field names. So for example the City field was orignally called “city” and in my form I added the shipping address fields with a name “ship_city”. Where and how would I go about getting Magento to create two sets of addresses, one using the address fields with “ship_” prefixed to them for the Default Shipping Address and the fields without a prefix for the Default Billing Address?

I’ve found that in the file app/code/core/Mage/Customer/controllers/AccountController.php has the following lines of code:

if ($this->getRequest()->getPost('create_address')) {
    $address 
Mage::getModel('customer/address')
        ->
setData($this->getRequest()->getPost())
        ->
setIsDefaultBilling($this->getRequest()->getParam('default_billing'false))
        ->
setIsDefaultShipping($this->getRequest()->getParam('default_shipping'false))
        ->
setId(null);
    
$customer->addAddress($address);

    
$errors $address->validate();
    if (!
is_array($errors)) {
        $errors 
= array();
    
}
}

So if you set a hidden form field “create_address” the system will create an address. By default the registration page has two more hidden fields set “default_billing” and “default_shipping” so the system will create one address and then set that address to be both billing and shipping for the customer being created. My assumption is that I’ll just need to duplicate this code and have one use ->setIsDefaultBilling() to true and the other set ->isDefaultShipping() to true, however I’m not sure how I would pass the appropriate data in the code I would set up to create the shipping address. In the code above it uses ->setData($this->getRequest()->getPost(). How would I pass only the form fields that are prefixed with “ship_” to this function so that it will create an address based on the shipping address fields I created?

Any help would be much appreciated. Thanks!

Image Attachments
custom_form.png
 
Magento Community Magento Community
Magento Community
Magento Community
 
CreedFeed
Member
 
Total Posts:  74
Joined:  2007-08-31
Milwaukee, WI
 

*bump*

Can anyone help, this is an urgent issue for me :( I have yet to figure it out…

 
Magento Community Magento Community
Magento Community
Magento Community
 
CreedFeed
Member
 
Total Posts:  74
Joined:  2007-08-31
Milwaukee, WI
 

Anyone? I still have been unable to get this working :(

 
Magento Community Magento Community
Magento Community
Magento Community
 
robbie_smith
Sr. Member
 
Avatar
Total Posts:  95
Joined:  2011-06-21
 

Did you have any success with this?

 
Magento Community Magento Community
Magento Community
Magento Community
 
AliasIO
Jr. Member
 
Avatar
Total Posts:  4
Joined:  2011-11-07
 

This is an old thread but it still comes up in search results and no solution has been posted yet… This is what I hacked up, obviously not a perfect solution but hopefully someone can improve on it.

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');

My (almost) complete code can be found here:

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

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

 
Magento Community Magento Community
Magento Community
Magento Community
 
webgalax
Jr. Member
 
Total Posts:  1
Joined:  2010-05-26
 

it works perfectly, thanks

 
Magento Community Magento Community
Magento Community
Magento Community
 
whiteorca
Jr. Member
 
Total Posts:  1
Joined:  2013-06-18
 

Hi

This is just what I need, but can’t seem to save the values for the shipping address.

1) Module:
Have created module with a controller as per your example (module has 2 files: namespace/modulename/controllers/AccountController.php and namespace/modulename/etc/config.xml
Module registered in app/etc/modules and enabled in admin interface

2) Template
Have amended the local register.phtml

Form displays the fields but nothing happens on save (saves billing address but not shipping)

Any ideas - why isn’t this just out of the box !!  wink

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top