The Field_collection module allows creating entities that contain any number of fields and attaching them to nodes.
Here is a simple example of creating a node that contains an unlimited number of fields for product nid
, quantity, and price:
$node->title = 'New order';
$node->type = 'order';
node_object_prepare($node);
$node->language = LANGUAGE_NONE;
$node->status = 1;
node_save($node);
// Field_order_products - node field name.
$field_collection_item = entity_create('field_collection_item', array('field_name' => 'field_order_products'));
// Specify node to attach the field collection.
$field_collection_item->setHostEntity('node', $node);
// Populate fields of the field_collection entity itself.
$field_collection_item->field_order_field_product[LANGUAGE_NONE][]['nid'] = 1;
$field_collection_item->field_order_field_quantity[LANGUAGE_NONE][]['value'] = 55;
$field_collection_item->field_order_field_price[LANGUAGE_NONE][]['value'] = 999.99;
$field_collection_item->save();