Create commerce product programatically


Using Entity Metadata Wrapper.

Get raw version
  1. $wrapper = entity_metadata_wrapper('commerce_product', commerce_product_new('product'));
  2. $wrapper->title = 'my title';
  3. $wrapper->sku = 'my SKU';
  4. $wrapper->commerce_price->amount = 500; // Price in minors units 500 = 5$
  5. $wrapper->commerce_price->currency_code = 'USD'; // Use your currency... obviously :-)
  7. // You can assign other fields with the same construction: $wrapper->field_name = ...
  9. $wrapper->save();


Phil's picture

Thanks for this, it's a lot easier to follow than mocking up a form submission as per a lot of other posts around.

Al's picture


I'm trying to update a price which contain a tax rate. I've spend hours looking a nice solution...

I got the following code:

  1. $wrapper = entity_metadata_wrapper('commerce_product', commerce_product_load($PRODUCTID));
  2. $wrapper->title = 'NEW TITLE';
  3. //attempting to change "including tax rate"
  4. $wrapper->commerce_price->data->include_tax = 'vat_16';
  5. $wrapper->save();

Unfortunately it doesn't work... How can change "include_tax" value ? Its not simple as I thought.

I've tried $wrapper->commerce_price->data->getPropertyInfo(); however seems its not found...