Drupal - Create product programmatically
A commerce product is an entity like any other, so...
$cp->field_my_field[LANGUAGE_NONE][0]['value'] = '22';
Just a tweak: the create method on the product controller already has some defaults so no need to add things like is_new or status.
public function create(array $values = array()) {
$values += array(
'product_id' => NULL,
'is_new' => TRUE,
'sku' => '',
'revision_id' => NULL,
'title' => '',
'uid' => '',
'status' => 1,
'created' => '',
'changed' => '',
);
return parent::create($values);
}
So I'd just do:
$cp = commerce_product_new('product');
$cp->uid = 1;
$cp->sku = $product[sku];
$cp->title = $product[name];
$cp->language = LANGUAGE_NONE;
$cp->commerce_price = array(LANGUAGE_NONE => array( 0 => array(
'amount' => $product[sale_price] ? $product[sale_price] : $product[retail_price],
'currency_code' => 'USD',
)));$product[retail_price];
$cp->my_field[LANGUAGE_NONE][0]['value'] = 22;
commerce_product_save($cp);