How to create product and product variations in Drupal 8 for Drupal Commerce

Posted by admin on Wed, 01/17/2018 - 10:18

Code snippets for creating Drupal 8 products and product variations for Drupal Commerce.

Create a product variation first:

// The price of the variation.
$price = new \Drupal\commerce_price\Price('24.99', 'USD');
$variation = \Drupal\commerce_product\Entity\ProductVariation::create([
  'type' => 'default', // The default variation type is 'default'.
  'sku' => 'test-product-01', // The variation sku.
  'status' => 1, // The product status. 0 for disabled, 1 for enabled.
  'price' => $price,

First load your store. If you haven't that - create it!

$store = \Drupal\commerce_store\Entity\Store::load(1);

Create a product attached to that store with that variation created above:

$product = \Drupal\commerce_product\Entity\Product::create([
  'uid' => 1, // The user id that created the product
  'type' => 'default', // Just like variation, the default product type is 'default'
  'title' => 'My product',
  'stores' => [$store], // The store we loaded above
  'variations' => [$variation], // The variation we created above

You can also add a variation to a product using the addVariation() method.


Hey! Don't miss a new cool stuff!


shawnkemp (not verified)

Thanks a lot for the kind of perfect topic I have not a lot of information about it but I have got an extra unique info in your unique post.Perfect description you have provided thanks for sharing.

davidjoe (not verified)

Very informative and rich in content article. It is very important to taKe care of the health. I also would like to recommend this high quality custom writing service where all students can get premium help with writing effective academic assignments.

Anonymous (not verified)
asd (not verified)

Thanks a lot for sharing us about this update. karamba Hope you will not get tired on making posts as informative as this.

carl (not verified)

This is the code that I have been looking for. This code will help me solve the college-paper review mystery and that is the only thing that's bothering me right now. I am extremely pleased that the writer shared this code with us here.

aftabak (not verified)

Here are a great deal of sites made just for composing audits on the assortment of gambling clubs. hopa

asd (not verified)

A debt of gratitude is in order for posting this information. I simply need to tell you that I simply look at your site and I discover it exceptionally fascinating and educational. I can hardly wait to peruse bunches of your posts. iq option

emily seo (not verified)

you may get sets of poker chips that incorporate 100 chips, 200 chips, et cetera, as far as possible up to 1000 chips. storspelare bonus

sudoku (not verified)

If you want to play free sudoku online with fun so follow our link and play free of cost.

aftab ali (not verified)

I trust you have delighted in perusing these football wagering tips and your excitement for football lay wagering has been fuelled. 1xbet

Anonymous (not verified)

the most straightforward to bet on. Instead of ball and football, where you need to manage point spreads and other confounding stuff. nightrush

aftab ali (not verified)

We can even purchase and offer stocks on the web. Dealers love being able to take a gander at their records at whatever point they need to, and intermediaries like being able to take arranges over the Internet, rather than the phone. Jetbull

Rahul Singh (not verified)

Such a nice post it’s very informative for future generation. We should have to encourage other professionals to read this article and I am also a part of other company who provide coursework help service, which is ideal for all students. Syringe Filters

aftabak (not verified)

With online communication, online gambling club recreations like online openings can be a brilliant action for cash or for entertainment only. ahtigames

Add new comment