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!


Brook Lhel (not verified)

I learned a new thing and i have got it.

Rozi Mendis (not verified)

Making a booking is easy. All you have to do is, pick up the phone and just call your friendly receptionists.

Escorts in Delhi (not verified)

Mb-9643090422 Female 4 Fun Delhi Escorts Agency Offer hi-profile Escorts in Delhi and Book Delhi Escort Service with top class independent Call Girls in Delhi

Aerocity Escorts (not verified)

new (not verified)

gift ideas for employees DO VISIT MY WEBSITE TO KNOW MORE

Anonymous (not verified)
Anonymous (not verified) hyderabad call girls number juhu escorts andheri escorts kurla escorts kandivali escorts colaba escorts dadar escorts vashi escorts Russian escorts thane escorts kukatpally escorts dumdum escorts park street escorts banjara hills escorts high-tech city escorts lokhandwala escorts banjara hills escorts jubilee hills escorts

Dale Morris (not verified)

I learned a new thing and i have got it. Thanks to sharing a informative post.
Apple customer service number for itunes
Apple phone number
Apple customer support number

Seema Singh (not verified)
Pune Escorts (not verified)

Im 21 yrs old young INDEPENDENT college GIRL of same pic as you see. I am really staying INDEPENDENTLY alone in my house. No middlemen or brokers its direct contact and I will PROVIDE FULL SERVICES AND FULL SATISFACTION ACCORDING TO YOUR NEEDS AND WISHES. I am here fulfill your requirements in all ways at a reasonable price, I will give 100% percent satisfaction on my bed Pune Call Girls.. Female Escort In Pune.
Pune Escorts
Pune Escorts
Viman Nagar Escorts Wakad Escorts Hinjewadi Escorts....

McAfee Support ... (not verified)

I just want to say thanks for your wonderful post, it is contain a lot of knowledge and information that i needed right now. You really help me out my friend, thanks!

Add new comment