I am trying to create an instance of 'commerce_customer_profile' entity with all the extra fields of the customers address.

I am doing this inside a FeedsProcessor that will import users and their addresses from Magento.

I don't want to have to create a fake $form_state array and then use field_attach_submit() as it doesn't seem right to me.

The only other way I have found so far is:

$billing_profile = commerce_customer_profile_new('billing', $entity->uid);
$full_billing_profile = commerce_customer_profile_load($billing_profile->profile_id);

$entity is the current user being created.

Which will then give me an empty instance of the new entity ready to populate and save but again it doesn't feel right.

Any help appreciated.

3 Answers 3


for nodes we can do below codes to load all fields and display formatters aplyed on them:

$node = node_load($nid);
field_attach_prepare_view('node', array($node->nid => $node), 'teaser');
$fields = field_attach_view('node', $node, 'teaser'); 

similar way should be exists for commerce


Th OP is over a year old now, so perhaps it wasn't a mature option back then, but if you've got the entity api module installed, you should be able to programitcally create fields by going:

$profile = entity_create('commerce_customer_profile', array('field_firstname' => 'Joe'));
$created = entity_save('commerce_customer_profile', $profile);

You may want to have a look into the commerce_customer_profile entity class, to see what requirements it has for creating its entities, i.e. it might want a user in drupal to associate with, or something else. Hope that can help.


In D8 you can do this :

$data = array(
  'type' => 'article', 
  'title' => 'My new title', 
  'uid' => 1
$node = Drupal::entityTypeManager()
  ->getStorage('node')   // or other entity type name


Just specify your entity (here is node), and the attributes or fields (here type, title...)

Some entities also provide a functional interface to set fields, and you can also set field values using the field name and the "magic" methods:

$storage->field_summary_text->value = "value";

[the exact nature of "->value" depends on the field type].

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.