5

Is there a way to get the default billing/shipping customer profile from a user?

I'm getting all customer profiles with:

$profiles = commerce_customer_profile_load_multiple(array(), array('uid' => $user->uid));

But which customer profile was set as default by the user?

3

You can get default user profile address with

commerce_addressbook_get_default_profile_id($uid, $type)

Returns the default profile id for a specific uid and profile type.

@param $uid The uid of the user whose default profile id should be returned.

@param $type The type of customer profile to look up.

@return The id of the default profile if set, FALSE otherwise.

An example if want get current user shipping addresss use below code :

global $user;
$default_pid=commerce_addressbook_get_default_profile_id($user->uid,'shipping');  
| improve this answer | |
1

I'm guessing that if you refer to the default field in relation with the user, you're using Commerce Addressbook, you've got an api function to get the default profile given a user id and the profile type: commerce_addressbook_get_default_profile_id

If you're not, you can get the profiles used by a given customer using the customer profile reference field for billing / shipping / whatever and consulting the variable commerce_customer_profile_' . $type . '_field'

So for loading the profiles of a given type you can use Entity Field Query using the customer user id against the order entity.

$query = new EntityFieldQuery();
$result = $query->entityCondition('entity_type', 'commerce_order')
  ->propertyCondition('uid', 0)
  ->execute();
if (!empty($result['commerce_order'])) {
  $orders = entity_load('commerce_order', array_keys($result['commerce_order']));
}

And then wrap the entity or use field_get_items:

$field_name = variable_get('commerce_customer_profile_billing_field', '');
$order_wrapper = entity_metadata_wrapper('commerce_order', $order);
$order_wrapper->{$field_name}->value();
| improve this answer | |
0

This might Help (Only billing i have mentioned here, you can change your condition depending on your needs).

$uid = "1"; //Define user id for which default address is to be found
$query = db_select("commerce_addressbook_defaults","c");
$query->fields("c",array("profile_id"));
$query->condition("uid",$uid);
$query->condition("type","billing");
$output = $query->execute()->fetchObject();

$profile_id = $output->profile_id;
if(!empty($profile_id)){
    $franchisee_info = commerce_customer_profile_load ($profile_id);
    echo "<pre>";
    print_r($franchisee_info);
    echo "</pre>";
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.