I have one field collection with unlimited values in Commerce Product Add form.

After performing some action, When user redirects to Add Product form, based on some predefined values in session, I want to display User a Field collection items pre-filled.

If I have 5 predefined values then 5 Field-collection items would be displayed to User and by clicking on save that product would be saved with that field collection item values.

By clicking on 'Add another item', User can add more as well. Above field collection items won't be saved until User clicks on Save form button.

I tried below code but it's not adding new items/instances :

function mymodule_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'commerce_product_ui_product_form') {
    if (isset($_SESSION['compatibilityInfo']) && $_SESSION['compatibilityInfo'] != '') {
      $compInfo = $_SESSION['compatibilityInfo'];
      $comp = explode('!c!', $compInfo);
      $cnt = count($comp);

        // Go one level up in the form, to the widgets container.
        $element = drupal_array_get_nested_value($form, array('field_part_details','und'));
        $field_name = $element['#field_name'];
        $langcode = $element['#language'];
        $parents = $element['#field_parents'];
        // Increment the items count.
        $field_state = field_form_get_state($parents, $field_name, $langcode, $form_state);
        $field_state['items_count'] = $cnt;
        field_form_set_state($parents, $field_name, $langcode, $form_state, $field_state);


2 Answers 2


I had to do this recently on a product. What I ended up doing is copying the instance of the field collection and then looping through and creating them. If your session variable keys are numerical from 0 on then you dont need the $item_count. In my case it was not so I could not simply use the foreach($value as $key => $val).

$my_loop = array(); // Array of items with your default values
$item_count = 0;
$field_collection_clone = $form['field_my_field_collection']['und'][0]; // Clone the field collection form element

//Loop through and create a new instance of the field collection form
foreach($my_loop as $loop){
 $form['field_my_field_collection']['und'][$item_count] = $field_item_base;
 $form['field_my_field_collection']['und'][$item_count]['field_my_field']['und'][0]['value']['#default_value'] = 'my default value';
  • Your solution is working but by clicking on 'Add another item' button, it removed all other added field collection items except first.
    – Adoshi
    Aug 12, 2014 at 15:56

I resolved it using below code :

$cp = commerce_product_new('product');
foreach($part as $title) {
        $my_collection = entity_create('field_collection_item', array('field_name' => 'field_part_details'));
        $my_collection->setHostEntity('commerce_product', $cp);
        $my_collection->field_year[LANGUAGE_NONE][0]['tid'] = $title->fromYear;

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.