How do I programmatically create an entity with its fields?
Here's some code that creates a product programmatically, including fields and properties.

/**
 * Create a product programmatically.
 *
 * This is stolen shamelessly from commerce_bpc. However, I'm not comfortable
 * with the field saving using form api. Seems quite odd.
 *
 * @param $product_type
 *   (string) The name of the product type for which products should be created.
 * @param $values
 *   Keyed array with
 *   - 'price' =&gt; actual amount owed on this installment; decimal text like '1.50'
 *   - 'amount_paid' =&gt; price amount already paid as a decimal text like '1.50';
 *   - 'original_order' =&gt; order id of the original order
 *   - 'original_line_item' =&gt; line item id of original line item
 *   - 'original_product =&gt; product id of the original product from which the
 *     new product is being created.
 * @param $extras
 *   An array for the values of  'extra fields' defined for the product type
 *   entity, or patterns for these. Recognized keys are:
 *   - status
 *   - uid
 *   - sku
 *   - title
 *   Note that the values do NOT come in the form of complex arrays (as they
 *   are not translatable, and can only have single values).
 * @return
 *   The ID of the created product.
 */
function commerce_installments_create_product($product_type, $values, $extras) {
  $form_state = array();
  $form_state['values'] = $values;
  $form = array();
  $form['#parents'] = array();

  // Generate a new product object
  $new_product = commerce_product_new($product_type);

  $new_product-&gt;status = $extras['status'];
  $new_product-&gt;uid = $extras['uid'];

  $new_product-&gt;sku = $extras['sku'];
  $new_product-&gt;title = $extras['title'];
  $new_product-&gt;created = $new_product-&gt;changed = time();

  // field_original_order[und][0][target_id]
  $order = array(LANGUAGE_NONE =&gt; array(0 =&gt; array('target_id' =&gt; $values['original_order'])));
  $form_state['values']['field_original_order'] = $order;

  // field_original_line_item[und][0][target_id]
  $line_item = array(LANGUAGE_NONE =&gt; array(0 =&gt; array('target_id' =&gt; $values['original_line_item'])));
  $form_state['values']['field_original_line_item'] = $line_item;

  $product = array(LANGUAGE_NONE =&gt; array(0 =&gt; array('target_id' =&gt; $values['original_product'])));
  $form_state['values']['field_original_product'] = $product;

  //commerce_price[und][0][amount]
  $price = array(LANGUAGE_NONE =&gt; array(0 =&gt; array(
    'amount' =&gt; $values['price'],
    'currency_code' =&gt; commerce_default_currency(),
  )));
  $form_state['values']['commerce_price'] = $price;

  // field_due_date[und][0][value][date]
  $due_date = array(LANGUAGE_NONE =&gt; array(0 =&gt; array('date' =&gt; $values['due_date'])));
  $form_state['values']['field_due_date'] = $due_date;

  // Notify field widgets to save their field data
  field_attach_submit('commerce_product', $new_product, $form, $form_state);

  commerce_product_save($new_product);
  return $new_product-&gt;product_id;
}