How to programmatically create a Drupal Commerce product with price -
i creating drupal commerce product in code using following:
$cp = commerce_product_new('product'); $cp->is_new = true; $cp->revision_id = null; $cp->uid = 1; $cp->status = 1; $cp->created = $cp->changed = time(); $cp->sku = $product[sku]; $cp->title = $product[name]; $cp->language = language_none; $cp->commerce_price->amount = $product[sale_price] ? $product[sale_price] : $product[retail_price]; $cp->commerce_price->currency_code = 'usd'; commerce_product_save($cp);
if comment out 2 lines pertaining price, product gets added, has no price.
if not comment out 2 lines, 500 error
if change pricing part be:
$cp->commerce_price = array( 'amount' => $product[sale_price] ? $product[sale_price] : $product[retail_price], 'currency_code' => 'usd', );
i not 500, message
warning: invalid argument supplied foreach() in _commerce_price_field_serialize_data() (line 148 of commerce/modules/price/commerce_price.module).
for each product being added, result being products being added , available, no price information in field_data_commerce_price table of them.
turns out needs be:
$cp->commerce_price = array(language_none => array( 0 => array( 'amount' => $product[sale_price] ? $product[sale_price] : $product[retail_price], 'currency_code' => 'usd', )));
Comments
Post a Comment