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!


gabapentin 100mg (not verified)

Secure ordering isotretinoin isotrex tab

buying cialis online (not verified)

Cialis 10 E 20 Mg Prezzi

doses of gabape... (not verified)


buy cialis onli... (not verified)

Injectable Amoxicillin

neurontin 300 (not verified)

Cialis Y Circulacion Periferica

can i buy ciali... (not verified)

Maca Powder

neurontin nur (not verified)

achat cialis pharmacie suisse

buy viagra order (not verified)

Doxycycline Acne

herbal viagra nz (not verified)

Propecia What Is Side Effects

zithromax pill (not verified)

Order Cialisis With Out Prescription

venlafaxine 150... (not verified)

Rezept Viagra Arzt

buy viagra softtabs (not verified)

buy super cialis

buy viagra softtabs (not verified)

buy super cialis

best viagra pills (not verified)

Acheter Levitra Par Internet

what is azithromycin (not verified)

kamagra for sale in us

finasteride, msd (not verified)

Cephalexin Oral Suspension Dog Dosing

order proscar f... (not verified)

Le Viagra Est Il Efficace

propecia otc (not verified)

Dernier Exercice Plus Longue

order cheap viagra (not verified)

Buy Celebrex In The Uk

finasteride wit... (not verified)

Farmacia Viagra Cialis

kamagra gratis (not verified)

cialis and fainting

5RR5WGL9 www.te... (not verified)


Anthonyskalp (not verified)

Свадебные фотографы