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,
]);
$variation->save();

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
]);
$product->save();

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

$product->addVariation($variation);
$product->save();
×

Hey! Don't miss a new cool stuff!

Comments

uzijupec (not verified)

Только по вебкам порталам, например милашка Kasandra7777 рассматривает в сети - вы можете воображать своего полового партнера кем угодно. Милфу возможно просто отрядить в какой-то неприличный бикини. Использование различных игрушек лишь обострит эффект интернет полового акта. Самые смелые намерения возможно выполнить хоть с незнакомым компаньоном. Вы легко имеете способ избавиться от комплексов. Мужики могут испробывать что-либо необычное, а милфы с охоткой не скрывают собственные прелести.

TrDenfaw (not verified)

how much does levitra cost per pill buy levitra canada can you buy kamagra without a prescription buy kamagra now cheap generic viagra can you buy viagra

TrDenfaw (not verified)

how much does levitra cost per pill buy levitra canada can you buy kamagra without a prescription buy kamagra now cheap generic viagra can you buy viagra

TrDenfaw (not verified)

how much does levitra cost per pill buy levitra canada can you buy kamagra without a prescription buy kamagra now cheap generic viagra can you buy viagra

wallyglaph (not verified)

i am so satisfacted. greetings wally

woonof (not verified)

hi, i am woo from Sweden and i want to explain any thing about "pandemic". Please ask me :)

wallyglaph (not verified)

i am so satisfacted. greetings wally

wallyglaph (not verified)

i am so satisfacted. greetings wally

wallyglaph (not verified)

i am so satisfacted.my english is poor, sorry :). thx for approving my user greetings wally

wallyglaph (not verified)

i am so satisfacted.my english is poor, sorry :). thx for approving my user greetings wally

woonof (not verified)

hi, i am woo from Sweden and i want to explain any thing about "pandemic". Please ask me :)

wallyglaph (not verified)

i am so satisfacted.my english is poor, sorry :). thx for approving my user greetings wally

woonof (not verified)

hi, i am woo from Sweden and i want to explain any thing about "pandemic". Please ask me :)

Layeddy (not verified)

Magnificent website. A lot of helpful info here. I'm sending it to several pals ans additionally sharing in delicious. And certainly, thank you to your sweat!

Layeddy (not verified)

Magnificent website. A lot of helpful info here. I'm sending it to several pals ans additionally sharing in delicious. And certainly, thank you to your sweat!

mitunsAmams (not verified)

Ich bin gegen covid 19. Was ist deine Meinung? mituns

wallyglaph (not verified)

i am so satisfacted.my english is poor, sorry :). thx for approving my user greetings wally

wallyglaph (not verified)

i am so satisfacted.my english is poor, sorry :). thx for approving my user greetings wally

wazzCam (not verified)

HI Guys. Thanks for my approving / wazz user, new user hehe

wazzCam (not verified)

HI Guys. Thanks for my approving / wazz user, new user hehe

wazzCam (not verified)

HI Guys. Thanks for my approving / wazz user, new user hehe

wazzCam (not verified)

HI Guys. Thanks for my approving / wazz user, new user hehe

wazzCam (not verified)

HI Guys. Thanks for my approving / wazz user, new user hehe

spiderCam (not verified)

hiiiiiii i am spider from Arg. I would to ask you what kind of games do you like playing?? My favourite games is diablo rpg

spiderCam (not verified)

hiiiiiii i am spider from Arg. I would to ask you what kind of games do you like playing?? My favourite games is diablo rpg

spiderCam (not verified)

hiiiiiii i am spider from Arg. I would to ask you what kind of games do you like playing?? My favourite games is diablo rpg

upssCam (not verified)

How do I move a thread to a different topic?
hi all :)

upssCam (not verified)

How do I move a thread to a different topic?
hi all :)

Viagra (not verified)

Belize Pharmacy Online

Stromectol (not verified)

Buy Viagara With Goft Card

order cialis online (not verified)

Propecia Conception

viagra cheap price (not verified)

Generic Viagra Cialis

Viagra (not verified)

cialis day

propecia finapil (not verified)

Cialis Duree Action

Cialis (not verified)

Viagra Ibiza

cialis generic ... (not verified)

Precio De Cialis En La Farmacia

cialis for sale (not verified)

Isoniazid

MichaelMot (not verified)

closet gay dating
site for gay chubby dating
[url="gaydatinglosangeles.com?"]gay couples dating resources[/url]

Cialis (not verified)

Amoxicilina Discount Pills By Money Order

Viagra (not verified)

Buy Zithromax Overnight

generic viagra rx (not verified)

Buy Viagra Online In Ireland

Stromectol (not verified)

Levitra Comentarios

buy stromectol ... (not verified)

E Online Pharmacy

Propecia (not verified)

20mg Cialis Vs 100mg Viagra

medication iver... (not verified)

Buspar Overnight Shipping

Stromectol (not verified)

Acheter Du Cialis En Europe

Priligy (not verified)

Amoxicillin Affects Pancreatic Islet Cells

lasix dosage (not verified)

Non Prescription Paxil On Line

Plaquenil (not verified)

Acheter Cialis France