How to create Drupal Commerce products programmatically

Sometimes you just need to get your hands dirty and start adding Drupal Commerce products programmatically. Luckily that is not that hard of an thing to do.

Create a Product entity:

      // Product
      $product = commerce_product_new('product');
      $product->sku = "PRODUCT-SKU";
      $product->title = "Product Title";
      $product->language = LANGUAGE_NONE;
      $product->uid = 1;
      $product->commerce_price[LANGUAGE_NONE][0] = array(
        'amount' => 10 * 100, // $10
        'currency_code' => "USD",
      );
      commerce_product_save($product);

 

To set the product category (which is a taxonomy term):

      $product->field_product_category[LANGUAGE_NONE][0]['tid'] = 1; // 1 = the term id

 

To set the stock:

      $product->commerce_stock[LANGUAGE_NONE][0]['value'] = 1;

 

To set VAT / Sales Tax:

      $product->commerce_price[LANGUAGE_NONE][0]['data']['include_tax'] = "vat"; // "vat" = machine name for the tax
      $product->commerce_price[LANGUAGE_NONE][0]['include_tax'] = "vat"; // "vat" = machine name for the tax

 

If you modify any of these fields, remember to run "commerce_product_save($product);" at the end.

 

To create a Product display for your new product, you need to use the previously used $product variable.

      // Product display
      $node = (object)array('type' => 'product_display');
      node_object_prepare($node);
      $node->title = "Product Display Title";
      $node->uid = 1;
      $node->field_product[LANGUAGE_NONE][]['product_id'] = $product->product_id;
      $node->language = LANGUAGE_NONE;
      node_save($node);

Comments

Manoj's picture
Manoj
Mon, 05/18/2015 - 09:45

Hi Kristian,
I'm new to drupal. I was trying to add a product to a category programatically. i followed the above mentioned process. A product is getting created, but it is not creating under the given category. Please find the below code.

$product = commerce_product_new('bags_cases');
$product->sku = "KBT001";
$product->title = $producttitle;
$product->language = LANGUAGE_NONE;
$product->uid = 1;
$product->commerce_price[LANGUAGE_NONE][0] = array(
'amount' => 10 * 100, // $10
'currency_code' => "USD",
);
$product->field_product_tid[LANGUAGE_NONE][0]['tid']=$category;
$product->commerce_stock[LANGUAGE_NONE][0]['value'] = 100;
commerce_product_save($product);

where $category is the taxonomy id.
Thanks
Manoj

Manoj's picture
Manoj
Mon, 05/18/2015 - 10:34

Hi Kristian,

Just to be clear, what i'm trying to do is, Creating a product under a category on form submit. I'm having a taxonomy term id under which the product has to be created. Hoping for your help.

Thanks
Manoj

bhaskar's picture
bhaskar
Tue, 12/22/2015 - 09:42

How add product Image & product display Image programmatically?

pop's picture
pop
Tue, 01/31/2017 - 08:44

how to insert commerce_store while create new product
$product->commerce_store[LANGUAGE_NONE][]['target_id'] = $store_id;

Aneesh's picture
Aneesh
Fri, 09/15/2017 - 09:03

Hi,
I am using below code to create product. Some time I am getting this error :-

"EntityMetadataWrapperException: Invalid data value given. Be sure it matches the required data type and format. Value at commerce_line_item(10)->commerce_product: . in EntityDrupalWrapper->set()"

This error is fixed automatically when I clear the cache.

Is there anything missing in the code?

code :-

$test = commerce_product_new('test');
$test->uid = $user->uid;
$test->sku = 'test_' . $user->uid . '_' . $test_amount . '_' . microtime();
$test->commerce_price['und'][0]['amount'] = $test_amount;
$test->commerce_price['und'][0]['currency_code'] = commerce_default_currency();

$val = afl_multi_currency_rate_conversion($value);
$symbol = afl_get_multi_currency_symbol();
$comm = afl_multi_currency_rate_conversion($commision);
$test->title = 'test ('.$symbol.' '.$val.') + test Charges (' .$symbol.' '.$comm.')' ;

commerce_product_save($test);

$test_order = ($user->uid) ? commerce_order_new($user->uid, 'checkout_checkout') : commerce_cart_order_new();

commerce_cart_order_empty($test_order);

commerce_order_save($test_order);

$line_item = commerce_product_line_item_new($test, 1, $test_order->order_id);
commerce_line_item_save($line_item);

$order_wrapper = entity_metadata_wrapper('commerce_order', $test_order);
$order_wrapper->commerce_line_items[] = $line_item;
// $entity->body->set(array('value' => "abc"));
$order_wrapper->save();

$test_order->data['order_type'] = 'commerce__test';
$test_order->data['test_amount'] = $value;
$test_order->data['test_charges'] = $commision;
$test_order->data['notes'] = $form_state['values']['notes'];
commerce_order_save($test_order);

sivarooban's picture
sivarooban
Mon, 05/06/2019 - 09:42

call to undefined function commerce_product_new()

sam's picture
sam
Wed, 01/15/2020 - 14:48

Hi could you please tell me how and where can i put this code

Add new comment