I add some fields to default user fields,like phone,address,city,... , I want If user edit or filled up his user information in user/uid/edit when submit ,if there is not any commerce shipping profile, create a profile to him/her, and if there is , update default shipping profile , how can I achieve this job? I know I can create profile with something like below code

 $new_profile = commerce_customer_profile_new($type);
  $wrapper = entity_metadata_wrapper('commerce_customer_profile', $new_profile);
  $wrapper->uid = $account;
  $wrapper->commerce_customer_address->country = $country;
  $wrapper->commerce_customer_address->name_line = $name_line;
  $wrapper->commerce_customer_address->first_name = $first_name;
  $wrapper->commerce_customer_address->last_name = $last_name;
  $wrapper->commerce_customer_address->organisation_name = $organisation;
  $wrapper->commerce_customer_address->administrative_area = $administrative_area;
  $wrapper->commerce_customer_address->sub_administrative_area = $sub_administrative_area;
  $wrapper->commerce_customer_address->locality = $locality;
  $wrapper->commerce_customer_address->dependent_locality = $dependent_locality;
  $wrapper->commerce_customer_address->postal_code = $postal_code;
  $wrapper->commerce_customer_address->thoroughfare = $thoroughfare;
  $wrapper->commerce_customer_address->premise = $premise;
  $wrapper->field_bcg_customer_phone = $phone;

but my question is , how can set it as default, after user edited (updated) if there is default profile , update it also

appreciate any help

2 Answers 2


As you mention you're looking for setting the profile as default, I'm assuming you're using Commerce Addressbook or in any case looking for a very similar feature of what that module provides.

Commerce Addressbook is a module that allows authenticated customers to reuse previously entered addresses during checkout. They can manage all entered addresses in their user panel (user/%user/addressbook).

You can use commerce_addressbook_set_default_profile function to set the default (one per type). Example:

$billing_profile = commerce_customer_profile_new('billing', $user->uid);
  $billing_profile->commerce_customer_address[LANGUAGE_NONE][0]['name_line'] = 'Sample Customer';
  $billing_profile->commerce_customer_address[LANGUAGE_NONE][0]['country'] = 'US';
  $billing_profile->commerce_customer_address[LANGUAGE_NONE][0]['thoroughfare'] = '16 Hampton Ct';
  $billing_profile->commerce_customer_address[LANGUAGE_NONE][0]['locality'] = 'Visalia';
  $billing_profile->commerce_customer_address[LANGUAGE_NONE][0]['administrative_area'] = 'CA';
  $billing_profile->commerce_customer_address[LANGUAGE_NONE][0]['postal_code'] = '93277-8329';

I'd recommend another way to create the profile, for example:

    // Create the new profile now.
    $profile = commerce_customer_profile_new('billing', $order->uid);

    // Add the address value.
    $profile_wrapper = entity_metadata_wrapper('commerce_customer_profile', $profile);

    $profile_wrapper->commerce_customer_address = array_merge(addressfield_default_values(), array(
      'country' => $country, 
      'name_line' => $name . ' ' . $last_name, 
      'first_name' => $first_name, 
      'last_name' => $last_name,

    // Save the profile.

You should provide more information about you problem set. As I see it. You save some data to user object and later you would like to reuse it for customer profile. Now you need to attach this new customer id to order. You can do it with

  • Commerce addressbook
  • Commerce singeaddress
  • with some custom code.

Now you will have new problem. Users can edit this information at checkout page. If they do so then new commerce customer profile will be created and the question is how you gonna solve this. Should they be able to edit it so or not. If they can do it then you need to update user profile and if not then they will have to go and change their profiles and now you need to update current order customer profile id if there is any.

So what I would like to say this what pcambra suggested would be easier because you would use widely tested solution. If You are really sure you need something similar to what I wrote then you should expect quite many hours of work and testing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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