I'm seeing a lot of issue queues essentially related to the same problem. Instead of clogging those up, with "I know" I'm starting my own issue, which explains the current problems and how they can be fixed. Please chime in if you know a better solution than the one I'm proposing.

Currently the module does not handle addresses well except for the cart checkout case for which it was developed.

1. If you load any entity for which an addressfield is added, you'll get the "Saved Address" drop down. This includes editing customer profiles in the admin.

2. There's another issue related to it only showing the currently logged in users addresses, instead of the user for which you are editing.

3. Additionally the Javascript populate doesn't work in any case except for the checkout case.

To resolve this issue we have to re-structure how this module is currently functioning. I think the best method would be to move this functionality into a field_widget() instead of hook it currently works with hook_field_attach_form().

In addition to this change, I think the module should be changed to use the #ajax field hook instead of custom JS . This will provide flexibility & simplicity, but might not be as fast. We can create custom JS later, if it want to avoid the AJAX request.

Once the field widget is created, we could provide the functionality we need to Commerce, but changing the widget on the customer profile to a widget with the "Saved Address" dropdown. The beauty of this is anytime this field is shown, it will use this widget and things should just workTM.

It would also provide flexibility to allow users to choose this widget any place they use an addressfield.

This essentially also decouples this functionality from Commerce and it would more just become an add-on module for Addressfield. So people could use it for any project which addressfield is used. Ideally I would hope this addition could simply get added into the addressfield module and then remove the requirement for a new add-on.

Drupal Commerce caveats:
With Drupal Commerce though, I'm not 100% sure if we're going to be able to edit previously saved addresses, which are associated with an order. I'm not sure if the order stores the address data, or simply refers to an address field entry. If it simply refers to an addressfield entry, then we're not able to change it, as this would change the historical information in any order, which this address has been referenced.

Unfortunately this clogs up the dropdown with a lot of useless entries, most of which can look like duplicates as far as the end user is concerned. I'm not planning to deal with this issue for now, if it does in fact exist.

Add on functionality provided by an old uc_addresses module
As for the functionality uc_addresses provided, I think we should allow a configurable option to show a page on the user/UID/edit page where the user can change their current billing & shipping addresses. Again, this page will have to get created...but if my widget is available, this should be a very simple addition, as not extra logic will need to get added.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

svendecabooter’s picture

Some interesting thoughts... Perhaps instead of a field widget, it could be separate pane on the checkout form, that updates the customer profile pane(s) via the #ajax property of the select formAPI item?
This would limit its usage to the end user though, and not show up in administration pages, which might be the desired option in most use cases, but perhaps there are use cases where that would cause trouble...

With regards to creating a new customer profile vs update an existing, there is also an issue about it: #1193802: Don't store unchanged customer profiles as new entry.
I'm thinking we should store a unique combination of entered data as a new customer profile, while a customer profile with values that are 100% similar to an earlier submitted one, could just link to the earlier ID instead of adding a new one. I suppose this could be achieved with hashing the values, but don't know if that'll work in practice :)

Sven

j0rd’s picture

The thing about a widget, is you'd have the choice to enable it or keep the standard widget on any address field. This is why I think it's the most flexible way of doing it.

As for using the hash values, that won't work because the hash only works against certain entries in the field and not all of them. This could lead to different addresses with the same hash.

megensel’s picture

I have plans to go a different route as soon as I get this version up and running at least for the moment. I think I am just going to create a new entity for the addressbook and make it non commerce specific. The goal would be to be able to add the addresses in the user pages and have a checkbox that will allow you to save an address to your addressbook on checkout. This is just a thought at the moment, but in the mean time I plan on getting the current version working at least for the checkout only. The hash issue is something I took care of yesterday by changing the md5 contents to use the full address array in lowercase.

At this point if you want to make a widget version go ahead and make a new branch (or I can make one for you) and that way, like Sven mentioned in another post, we can merge it in later.

j0rd’s picture

Sounds good. I'm using most of the code from commerce_addressbook anyways, and simply adding some additional widget hooks. So it should be easy to merge.

Please make me aware of your fork when you create it, so I can keep my eye on it.

MegaChriz’s picture

Version: » 7.x-1.x-dev

Freixas and I are working on a new version of Ubercart Addresses which will get a rich API for addresses. If possible, we want to make the module to be compatible with Drupal Commerce too. The experimental code we currently have (not public available yet) is only compatible with Ubercart.
I wonder if we can work together as we are trying to accomplish the same thing?

megensel’s picture

Sounds like a good idea to put our heads together to make sure we aren't duplicating work and what not. Let me know how you want to proceed.

MegaChriz’s picture

@megensel
It's a long story to tell what our plans for Ubercart Addresses are, but shortly:
- The module will first be rewritten for Drupal 6 and then be ported to Drupal 7. See also #1104854: Any foreseen porting to Ubercart for Drupal 7 ?.
- A part of the module will be object orientated, there will be an address book class and an address class.
- For each user there will be one default shipping address and one default billing address
- There will be a template for a single address and a template for the complete list of addresses for a single user (the address book)
- There will be permissions for viewing addresses, editing addresses and deleting addresses. See #1194232: Permissions ignored for detailed list.
- Some changes to addresses should go via the address book. For each user, the address book class controls that there will only be one default shipping address and one default billing address. It also will ensure that (nick)names of addresses are unique per user.
- We looking for a way to let other modules add additional address fields. Freixas thought about a system with field handlers. These handlers could be defined as 'plugins' and the system could be implemented with the help of the Ctools module (my idea). The module Extra Fields Pane should take care for implementing the extra address fields. With Extra Fields Pane you can currently already add extra address fields (for Ubercart), but it's not fully integrated with Ubercart Addresses yet (that's why the rewrite for Ubercart Addresses was planned)

The reason why extra address fields are important? In Europe, some addres data needs to be more refined. In the Netherlands the name can exist out of 3 parts instead of two. Beside first name and surname we have also a surname prefix, in the Netherlands called 'Tussenvoegsel':
http://en.wikipedia.org/wiki/Tussenvoegsel
Other things that some shops want to be more refined: separate the house number from the street, a title field for people with a particular study level (Dr. (Doctor) for example), a gender field.

I can mail you a link to the experimental code if you want. The code is currently for Drupal 6 only. The reason why the code is not on Drupal yet is because it's not yet operational and because the API is not definitive.

MegaChriz’s picture

because it's not yet operational

I should have said "not fully operational". Except for the address edit form, the "core" features of the address book are already functional (but may need to be refined some more).

WilliamV’s picture

@svendecabouter: Appreciate your contribs a lot. Awaiting for following step in this approach: posted in drupalcommerce.org a long time ago about this issue. Greetings!

sonar_un’s picture

Thank you very much for providing a much needed contrib for commerce.

Subscribing

johnstav’s picture

Eagerly subscribing.

PePiToO’s picture

+1

vasike’s picture

subscribe

j0rd’s picture

This may be possible now with the hook_field_widget_form_alter() new hook which got added in Drupal 7.7 or 7.8 (i don't recall). YOu would be able to most likely replace the default widget with an alter now, but I would recommend making this it's down widget.

More info about this new hook:
http://drupal.org/node/1204230

bendiy’s picture

I'm having a hard time seeing how calling this will allow us to implement these features:

/**
 * Implements hook_field_widget_WIDGET_TYPE_form_alter().
 */
function commerce_addressbook_field_widget_addressfield_standard_form_alter(&$element, &$form_state, $context) {
  // Do some stuff here.
}

You do not have access to the $form, so adding the saved address drop down and js is not possible. If you made this into a widget, how do you use the above hook to add that widget to the addressfield? I'd love to see any code you have using a widget like this. I'll try and help finish it I can figure it out.

I think the original issues raised in the first post can be addressed through some simple changes to the existing code.

  1. If you load any entity for which an addressfield is added, you'll get the "Saved Address" drop down. This includes editing customer profiles in the admin.
  2. There's another issue related to it only showing the currently logged in users addresses, instead of the user for which you are editing.
  3. Additionally the Javascript populate doesn't work in any case except for the checkout case.

Proposed changes:

  1. I don't think this loading for all addressfields is a bad thing if #2 is fixed. The commerce_addressbook_field_attach_form() function needs some additional checks before it calls commerce_addressbook_get_fields($form) and commerce_addressbook_list_field($form, $profiles);
  2. The commerce_addressbook_get_fields() function should find out the intended user from the path, and not from global $user and pass that into commerce_addressbook_get_customer_profiles_by_type(). Hopefully securely and with some permission restrictions so only admins can get a different user's profile info.
  3. The javescript can be fix. I already have it working for custom fields #1280248: Add support to handle custom fields
bendiy’s picture

Assigned: Unassigned » bendiy
Status: Active » Needs work
FileSize
22.16 KB

I'm getting close on a working solution for this.

It's not done yet, but should be next week. You can see the progress so far in the attached patch. I still need to fix the javascript so it works on non-checkout forms. I also need to change the flow of how the functions are called to attach this to forms in a more straight forward way.

svendecabooter’s picture

Nice work bendiy!
I'm started a new branch "field_widget", that incorporates your patch.
I've been building further upon it, in the hopes to get the module working with the Drupal #ajax callbacks instead of custom javascript.
I'm also focussing on following the workflow as laid out by Ryan at http://drupal.org/node/1193802#comment-4724358

Hopefully my changes don't interfere too much with the plans you have, and I hope we can join forces to move this more generic approach forward, instead of starting yet another fork.
Catch me on IRC if you want to discuss this further, or get Git commit access to help build upon the progressed code.

Thanks so much for your help!

bendiy’s picture

@svendecabooter I took a look at the commits so far for the "field_widget" branch. If you can work out the details on the #ajax callbacks, go for it. I don't have much experience with that yet.

I noticed that you pulled out some of my code to get all the entities and changed it to just ' commerce_customer_profile'. What I had in place was able to also pull in a user's addressfields that were attached to the 'user' content type as well as any addressfields used by the Profile2 module. My goal is the make it possible to have the user enter Billing and Shipping addressed under the "My Account" profile and have the addressbook module pull those in on checkout or even use the addressbook module to populate the addressfields in their profile.

@TODO: do we really need to get all entities, or can be have hardcoded default to commerce_customer_profile?

I haven't tried your changes yet, but that is the direction I was going. Make addressbook generic enough to work on all addressfields no matter where they are used. I was also considering adding a form field to the widgets settings that would restrict it to specific entity types. For example, when you add the saved addresses widget to the checkout form, you could restrict the entities it pull from to use addressfields attached to the user or profile2. This way, each time you add the widget, you can specify what it's pulling from. Ideally, you would add the widget to the checkout form for a billing address and restrict it to just the Billing profile you have created under "My Account". You then add it to the checkout for the shipping address and restrict it to use the Shipping profile under "My Account".

I'll look over your changes in more detail tomorrow.

svendecabooter’s picture

Hi,

Thanks for your feedback.
The problem with this module seems to be that everyone has another slightly different use case, which results in a lot of attempts, but no solid release that people can actually start using. That is my focus right now.

That said, I'd also love the module to be able to cater for the use cases you described. I'd have to think about them in more detail to find out what's possible or not.
My currently committed code indeed makes some shortcuts for now and assumes customer profile entities, but the basic architecture has been created with more than customer profiles in mind. This was mainly done to get a better view of whether it was possible what I wanted to achieve.

Adding the field to other things like User entities and Profile2 entities will probably be possible. The #ajax code should also make it possible to dynamically fill in the entity fields. The main problem is see is that it might not be possible to prefill one entity type (customer profile on Commerce checkout) with values from another entity type (User entity address field). But perhaps that is functionality that should be more tightly coupled to the Addressfield module instead of the Commerce module?

svendecabooter’s picture

Just a quick overview of functionality that is currently added to the field_widget branch:

* Ability to add a 'Saved addresses' dropdown widget to customer profile entities - code & inspiration by bendiy.
This should work with multiple customer profile types, such as billing and shipping (via http://drupal.org/project/commerce_shipping)
--> TODO: make this more generic

* Ability to prefill customer profile fields by selecting a saved profile via AJAX - code & inspiration by jsacksick & rszrama
This doesn't only fill in the address fields, but also any other custom fields that have been added to the customer profile entity.
--> TODO: prefill the first address initially on page load.

* Customer profiles that are selected from the dropdown and left unchanged, are now directly referenced by the order. This avoids lots of duplicate customer profile entities being saved when users make a lot of orders with the same (address)info.

Other big TODO: remove dependency on the currently logged in user. Use the order uid instead.

You might need to reinstall the module to get everything working.

Feedback welcome.

bendiy’s picture

I thought a lot about prefilling the first address initially on page load. I initially had this working, but then removed it. I don't think it's needed. If this module evolves to be more generic and not specific to Commerce, it will be hard to figure out which address was last used for the user or profile forms.

Also, having the user start from an empty form makes the address they select or enter a deliberate choice. If you prefill the form, I think there's a greater chance that there will be an error due to the user's tendency to gloss over it since it's already filled out.

j0rd’s picture

I agree with @bendiy on the pre-filled issue. A drop down selection, then pre-fill should suffice.

If you really wanted the pre-fill, you could add this as a checkbox option in the widget.

That's my take.

svendecabooter’s picture

Assigned: bendiy » Unassigned
Status: Needs work » Fixed

The prefilling of the latest address on checkout page load is now added as an option, which is off by default.
So people who want it, can activate it.

Marking this issue as fixed, because the initial purpose of it has been implemented.
Other todo's can be discussed in seperate issues.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

drvdt’s picture

I have read all comments, but could not find where is solution?
Any help?

svendecabooter’s picture

This functionality was used in the 1.x branch of the module, but abandoned in the 2.x branch, since it turned out to be quite unusable.
So the addressbook widget is no longer a field.

bleg’s picture

How can I achieve this on the current 3.x branch? Are there any other alternatives