diff --git a/commerce_addressbook/commerce_addressbook.js b/aa/commerce_addressbook/commerce_addressbook.js index 6397064..2401bc1 100644 --- a/commerce_addressbook/commerce_addressbook.js +++ b/aa/commerce_addressbook/commerce_addressbook.js @@ -1,24 +1,24 @@ Drupal.behaviors.commerce_addressbook = { attach: function(context){ jQuery('[name*="collected_addresses"]').once("commerce_address-processed").change(function(){ + var id = jQuery(this).parent().parent().parent().attr('id'); var obj; obj = jQuery.parseJSON(Drupal.settings.commerce_addressbook); var selectedVal = jQuery(this).val(); - if(selectedVal != 'none'){ + if (selectedVal != 'none') { // Stores current async setting in case the dropdown changes thew country triggering the change event var async = jQuery.ajaxSettings.async; - // determines if the country has changed and triggers the onchange event before filling in the fields so that the right fields get changed - if(obj[selectedVal]['country'] != jQuery('[name*="country"]').val()){ - jQuery('[name*="country"]').val(obj[selectedVal]['country']); + if (obj[selectedVal]['country'] != jQuery('#'+ id + ' [name*="country"]').val()) { + jQuery('#'+ id + ' [name*="country"]').val(obj[selectedVal]['country']); // forces synchronous mode so that the form can be filled by jquery after the change event is triggered jQuery.ajaxSettings.async = false; - jQuery('[name*="country"]').trigger('change'); + jQuery('#'+ id + ' [name*="country"]').trigger('change'); } - for(name in obj[selectedVal]){ + for (name in obj[selectedVal]) { var val = obj[selectedVal][name]; if(name !== 'undefined'){ - jQuery('[name*="'+name+ '"]').val(val); + jQuery('#'+ id + ' [name*="'+name+ '"]').val(val); } } // returns previously set value after work is done @@ -29,7 +29,7 @@ Drupal.behaviors.commerce_addressbook = { }; jQuery(document).ready(function(){ - if(jQuery('[name*="country"]').val() != 'United States'){ + if (jQuery('[name*="country"]').val() != 'United States') { var addresses = new Array(); jQuery.ajaxSettings.async = false; jQuery('[name*="country"]').trigger('change'); @@ -39,15 +39,14 @@ jQuery(document).ready(function(){ addresses[x++] = obj[address]; } address = addresses.pop(); - console.log(address); - for(name in address){ + /*for(name in address){ var val = address[name]; if(name !== 'undefined'){ jQuery('[name*="'+name+ '"]').val(val); } - } + }*/ jQuery.ajaxSettings.async = true; } diff --git a/commerce_addressbook/commerce_addressbook.module b/aa/commerce_addressbook/commerce_addressbook.module index 63e73ae..11fcf3a 100644 --- a/commerce_addressbook/commerce_addressbook.module +++ b/aa/commerce_addressbook/commerce_addressbook.module @@ -14,32 +14,40 @@ */ function commerce_addressbook_list_field(&$form, $profiles) { global $user; - $options = array(); - $addresses = array(); - foreach ($profiles as $profile_id => $profile) { - foreach ($profile as $name => $vals) { - $profiles[$profile_id][$name] = $vals['#default_value']; - } - } - foreach ($profiles as $address) { - $bin = md5(strtoupper(implode('.', $address))); - $addresses[$bin] = $address; - } - $options['none'] = t('-- Select a saved address --'); - foreach ($addresses as $key => $address) { - $options[$key] = $address['name_line'] . ": " . $address['thoroughfare']; - } - - drupal_add_js(array('commerce_addressbook' => drupal_json_encode($addresses)), 'setting'); - $form['#attached']['js'][] = drupal_get_path('module', 'commerce_addressbook') . '/commerce_addressbook.js'; + $addresses_js = array(); $profile_type = ''; - foreach ($form as $key=>$val){ - if(strstr($key, 'customer_profile_')){ + $form['#attached']['js'][] = drupal_get_path('module', 'commerce_addressbook') . '/commerce_addressbook.js'; + + foreach ($form as $key=>$val) { + if (strstr($key, 'customer_profile_')) { $profile_types[] = $key; } } - foreach($profile_types as $profile_type){ + + foreach ($profile_types as $profile_type) { + $options = array(); + $addresses = array(); + $type = str_replace('customer_profile_', '', $profile_type); + if (!isset($profiles[$type])) continue; + + foreach ($profiles[$type] as $profile_id => $profile) { + foreach ($profile as $name => $vals) { + $profiles[$type][$profile_id][$name] = $vals['#default_value']; + } + } + foreach ($profiles[$type] as $address) { + $bin = md5(strtoupper(implode('.', $address))); + $addresses[$bin] = $address; + } + + $addresses_js += $addresses; + + $options['none'] = t('-- Select a saved address --'); + foreach ($addresses as $key => $address) { + $options[$key] = $address['name_line'] . ": " . $address['thoroughfare']; + } + $form[$profile_type]['collected_addresses'] = array( '#type' => 'select', '#title' => t('Saved Addresses'), @@ -49,7 +57,11 @@ function commerce_addressbook_list_field(&$form, $profiles) { '#weight' => -10, ); } - + + if (isset($addresses_js)) { + drupal_add_js(array('commerce_addressbook' => drupal_json_encode($addresses_js)), 'setting'); + } + return TRUE; } @@ -66,7 +78,7 @@ function commerce_addressbook_field_attach_form($entity_type, $entity, &$form, & * Adjusts the checkout form to add address book functionality */ function commerce_addressbook_form_commerce_checkout_form_checkout_alter(&$form, &$form_state, $form_id) { - if(!isset($form_state['triggering_element'])){ + if (!isset($form_state['triggering_element'])) { $profiles = commerce_addressbook_get_fields($form); if(!empty($profiles)) { commerce_addressbook_list_field($form, $profiles); @@ -86,6 +98,9 @@ function commerce_addressbook_get_fields(&$form) { if (user_is_logged_in()) { $customer_profiles = commerce_addressbook_get_customer_profiles_by_type($user->uid); if ($customer_profiles) { + // Prefill each field with the appropriate values + $fields = array(); + foreach ($customer_profiles as $type => $profiles) { // Get a list of fields for this entity bundle, to prefill them where possible $entity_info = entity_get_property_info('commerce_customer_profile'); @@ -98,9 +113,6 @@ function commerce_addressbook_get_fields(&$form) { } } - // Prefill each field with the appropriate values - $fields = array(); - if (is_array($profiles)) { // loop over each available customer profile entity foreach ($profiles as $profile_id => $profile) { @@ -111,9 +123,7 @@ function commerce_addressbook_get_fields(&$form) { foreach ($field_items as $id => $profile_values) { foreach ($profile_values as $profile_field => $default_value) { $form['customer_profile_' . $type][$field][$langcode][$id][$profile_field]['#default_value'] = $default_value; - - //dpm(str_replace('_', '-', "customer-profile-$type-$field-$langcode-$id-$profile_field")); - $fields[$profile_id][$profile_field] = $form['customer_profile_' . $type][$field][$langcode][$id][$profile_field]; + $fields[$type][$profile_id][$profile_field] = $form['customer_profile_' . $type][$field][$langcode][$id][$profile_field]; } } } @@ -124,6 +134,7 @@ function commerce_addressbook_get_fields(&$form) { } } } + return $fields; } @@ -140,6 +151,7 @@ function commerce_addressbook_get_customer_profiles_by_type($uid) { $customer_profiles[$profile->type][$profile->profile_id] = $profile; } } + return $customer_profiles; } @@ -160,10 +172,11 @@ function commerce_addressbook_get_customer_profiles($uid) { foreach ($result['commerce_customer_profile'] as $profile) { $profile_ids[] = $profile->profile_id; } + $results[$uid] = commerce_customer_profile_load_multiple($profile_ids); } - } + return $results[$uid]; }