I am wondering if it would be at all possible to create both billing and shipping address fields at user register. If the shipping is the same as billing it would auto populate.

Thanks

Comments

sean3z’s picture

Yes, you would need to create and load a modified version of the addresses.js though (to support all fields). Below is an example; obviously, you'll need to substitute ID/array index information. As far as auto-population, you'd need to create a javascript listener for a 'Same As' checkbox or something to carry over data or disable the fields etc.

example_module.module

function example_module_form_alter(&$form, $form_state, $form_id) {
  switch ($form_id) {
    case 'user_register':
      drupal_add_js(drupal_get_path('module', 'example_module') . '/example_module_addresses.js');
      module_load_include('inc', 'addresses');
      $form['billingcountry']['#options'] = _addresses_country_get();
      $form['billingstate']['#attributes'] = array('style' => 'display: none;');
      $form['shippingcountry']['#options'] = _addresses_country_get();
      $form['shippingstate']['#attributes'] = array('style' => 'display: none;');
    break;
  } // end switch;
} // end function;

example_module_addresses.js

Drupal.behaviors.addresses = function(context) {
  // Bind country changes to reload the province field and
  // load province select element onLoad, do it once.
  // See http://drupal.org/node/817244
  $('#edit-profile-billingcountry',context)
    .addClass('addresses-processed')
    .bind('change',function(){performProvinceAjax(this, '#edit-profile-billingstate');})
    .change();

  $('#edit-profile-shippingcountry',context)
    .addClass('addresses-processed')
    .bind('change',function(){performProvinceAjax(this, '#edit-profile-shippingstate');})
    .change();

  // Make province select list call
  function performProvinceAjax(countryElement, child) {
    // Country field's related province element
    var provinceElement=$(countryElement).parent().siblings().children(child);

    $.ajax({
      type:'GET',
      url:Drupal.settings.basePath,
      success:updateProvinceField,
      dataType:'json',
      data: {
        q:'addresses/province_ajax',
        country:$(countryElement).val(),
        field_id:provinceElement.attr('id'),
        field_name:provinceElement.attr('name'),
        passback:provinceElement.parent().attr('id'),
        //province:provinceElement.val(),
        province:$(child).val()
      }
    });
  }

  // Populate province field
  function updateProvinceField(data) {
    if(data.hide){
      $('#'+data.passback).hide();
    }else{
      $('#'+data.passback).show();
    }
    $('#'+data.passback).html(data.field);
  }
};
ColinMctavish’s picture

Hey sean3z

Thanks for responding. However, I am one of those annoying non coders using Drupal. Finding my way in the dark. I would be unsure on how to implement this into the address module or user module. I was looking for more of a turnkey solution. But thank you