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;

Add new comment