Why does updating newly added field collection on hook_field_collection_item_insert fail? The main goal is to create product &amp; product display using drupal 7 &amp; 
drupal commerce when a user with a certain role add a new field collection.

What I'm trying to do is when a new field collection is added : 


access host entity fields and retrieve some datas 
do some custom stuff  
update 1 value of a field collection which is a field
reference to product


Steps followed :


I'm using hook_field_collection_item_insert to access newly created field collection. 
I call a function which create a product (using commerce modules suites). Above steps works just fine.
Then I call a function that update the current field collection field by adding a value which is a reference of product created in function my-module-create_product(). 


$fc_wrapper-&gt;save(TRUE); is failling to save with the following error :

Exception: Unable to save a field collection item without a valid reference to a host entity in FieldCollectionItemEntity-&gt;save() (line 520 of ../mysite.local/docroot/sites/all/modules/contrib/field_collection/field_collection.module) 

I've done some search so far without any success.

I can't figure out the fix for this, some direction or any information that will help to solve this issue will be much appreciated.

Below a simplified code that I'm using :

function my_module_field_collection_item_insert($field_collection_item) {

    $item_id = $field_collection_item-&gt;item_id;
    //dpm($item_id);

    //Check the field collection bundle &amp; if it's just have been created
    if ($field_collection_item-&gt;field_name == 'field_my_field_collection' &amp;&amp; $field_collection_item-&gt;is_new == TRUE) {

        //Access host entity
        $node_wrapper = field_collection_item_get_host_entity($field_collection_item);

        //Do custom stuff with host entity fields 
        //...
        //set $product_type, $price, $extras values 

        //Call a function that create product
        $product = my_module_create_product($product_type, $price, $extras);

        //Product creadted 
        if ($product) {

            $product_display = my_module_create_product_display($product, $item_id);

        } else {
            throw new Exception('Product creation as failed...');
        }
    } 
}




//Function that create product
function my_module_create_product($product_type, $price, $extras) {
    //create product 
    return $created_product;
}




//Function that create product display
function my_module_create_product_display($product, $item_id) {

    //Note that loading existing field collection here works but it not what I need
    //Loading created field collection
    $fc_wrapper = entity_metadata_wrapper('field_collection_item', $item_id);

    if ($fc_wrapper) {

        if ($fc_wrapper-&gt;field_product_ref-&gt;value() == NULL) {

            //Setting product reference 
            $fc_wrapper-&gt;field_coaching_product_ref-&gt;set($product-&gt;product_id);

            //Only need to save the current field collection
            $fc_wrapper-&gt;save(TRUE); 

            //I've tried bellow still don't work either
            //$fc_wrapper-&gt;save();          
            return $fc_wrapper-&gt;getIdentifier();

        } else {    
            //Product display already exist 
            return FALSE; 
        }
    } else {
        throw new Exception('Exception... ');
    }
}
You can only set the host entity while creating the field collection item (as your exception also states).

What you can do however is use $fc_wrapper-&gt;value()-&gt;updateHostEntity('node', $node); before you save the $fc_wrapper.
For that to work you will first have to load your host entity node of course (node_load() will suffice).

Hopefully this gets you on your way.