create a copy of a field-collection for entity clone I have a use case where I need to create a copy of an entity (commerce product) that is home to various field collections.

the entity is created by a simple load/save combo:

// load entity
$old_entity = commerce_product_load_by_sku($sku);

// copy 
$new_entity = $old_entity;

// alter copy so it can be saved as a new entity
$new_entity-&gt;status = 0;
$new_entity-&gt;title = $old_entity-&gt;title . '(COPY)';
$new_entity-&gt;is_new = TRUE;
// [...] and a few more unsets (created, revision, copying translations...)

// -&gt;&gt;&gt; missing field_collection clone / exchange of id references in the entity here &lt;&lt;&lt;-

// save entity
commerce_product_save($new_entity);


As the field_collection id stored in the entity remains unchanged, both entities now share the field collection which leads to unwanted effects.  

Can someone point me to a reference on how to create copies of field_collections and get their id's returned, so I can exchange them in the parent entity before saving it? ... or maybe there is an even better way to deal with this?
This has been solved by chalee on d.o on the following issue: Integrate node_clone with field_collection module.

This is his function that works just fine for my use case:

&lt;?php
function clone_fc_items($entity_type, &amp;$entity, $fc_field, $language = LANGUAGE_NONE){
  $entity_wrapper = entity_metadata_wrapper($entity_type, $entity);
  $old_fc_items = $entity_wrapper-&gt;{$fc_field}-&gt;value();
  if (!is_array($old_fc_items)) {
    $old_fc_items = array($old_fc_items);
  }

  $field_info_instances = field_info_instances();
  $field_names = element_children($field_info_instances['field_collection_item'][$fc_field]);

  unset($entity-&gt;{$fc_field}[$language]);

  foreach ($old_fc_items as $old_fc_item) {
    $old_fc_item_wrapper = entity_metadata_wrapper('field_collection_item', $old_fc_item);
    $new_fc_item = entity_create('field_collection_item', array('field_name' =&gt; $fc_field));
    $new_fc_item-&gt;setHostEntity($entity_type, $entity);
    $new_fc_item_wrapper = entity_metadata_wrapper('field_collection_item', $new_fc_item);

    foreach ($field_names as $field_name) {
        //if (is_array($old_fc_item-&gt;{$field_name})){
            if (!empty($old_fc_item-&gt;{$field_name})){
                $new_fc_item-&gt;{$field_name} = $old_fc_item-&gt;{$field_name};
            }
        //}   
    }
    $new_fc_item_wrapper-&gt;save();

    //Now check if any of the fields in the newly cloned fc item is a field collection and recursively call this function to properly clone it.
    foreach ($field_names as $field_name) {
        if (!empty($new_fc_item-&gt;{$field_name})){
            $field_info = field_info_field($field_name);
            if ($field_info['type'] == 'field_collection'){
                clone_fc_items('field_collection_item',$new_fc_item, $field_name,$language);
            }    
        }
    }

  }
}
?&gt;


Again, kudos goes to @chalee - thanks :)