I'm trying to programmatically add an address through php. Are there any APIs for doing so?

And also to check if a user has an address assigned to them.

Are there APIs for doing so or should I just do a SQL call, if so what are the tables?

Thanks

Comments

michaelfavia’s picture

The address field leverages drupals field api. As such there are a number of ways you can populate it with data. Programatic form executions, node_save(), etc.

trevorwh’s picture

Reviving this issue for the moment. I've added addressfield as profile item, but when I submit via drupal_submit_form(), for some reason the validation in addressfield causes a $rebuild = true; for the form, which means that it doesn't continue to the submit handler.

When i remove the addressform field, it works perfectly.

Any ideas on why this is occuring? I'm looking at line 821 in the drupal_process_form function.

if ($form_state['process_input']) {
drupal_validate_form($form_id, $form, $form_state);

// drupal_html_id() maintains a cache of element IDs it has seen,
// so it can prevent duplicates. We want to be sure we reset that
// cache when a form is processed, so scenarios that result in
// the form being built behind the scenes and again for the
// browser don't increment all the element IDs needlessly.
drupal_static_reset('drupal_html_id');
var_dump($form_state);
if ($form_state['submitted'] && !form_get_errors() && !$form_state['rebuild']) {
}

Because form_state['rebuild'] is true after drupal_validate form, it doesn't continue.

drupalninja99’s picture

+1. What if an address isn't a node, like with the commerce module the address belongs to entities.

drupalninja99’s picture

OR programatically save commerce_customer_profile, which includes addresses, havent figured that out yet.

rszrama’s picture

Title: Programmatically Add Address for user. » How do I set the value of an address field in code?
Version: 7.x-1.0-beta2 » 7.x-1.x-dev
Component: Miscellaneous » Documentation
Status: Active » Fixed

I don't know what was going on with the rebuild loop above - this module should only trigger a rebuild if it detects the country value has changed and the AJAX refresh didn't occur (i.e. JavaScript was turned off).

However, for the other folks wondering how to programmatically set an address, you can either use the Fields API directly or use the entity metadata wrapper from the Entity API module like so:

$profile = commerce_customer_profile_new('billing', 1);
$wrapper = entity_metadata_wrapper('commerce_customer_profile', $profile);
$wrapper->commerce_customer_address->country = 'US';
$wrapper->commerce_customer_address->name_line = 'Joe Buyer';
// Repeat for individual address field elements...
);

(Note: this example comes from Commerce; change the entity type and field name for your situation, but note that you always have to set the country.)

Status: Fixed » Closed (fixed)

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

Marko B’s picture

Issue summary: View changes

I would try with this here http://drupal.stackexchange.com/questions/124056/save-user-fields-in-com... it has much more information that in the post from Ryan