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);
}
}
}