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

Estherchera (not verified)

Mmxonm wjeroz Buy viagra without rx Buy viagra internet

آوا (not verified)



من این مطلب را خواندم و می توانم این پیغام

را به دیگران کرب پیشنهاد کنم

Also visit my web site; بازی انفجار

Anonymous (not verified)

http://www.bharatijoshi.com/ahmednagar-call-girls-service.html
http://www.bharatijoshi.com/hadapsar-call-girls-service.html
http://www.bharatijoshi.com/pune-airport-call-girls-service.html
http://www.bharatijoshi.com/pimpri-chinchwad-call-girls-service.html
http://www.bharatijoshi.com/kalyani-nagar-call-girls-service.html
http://www.bharatijoshi.com/viman-nagar-call-girls-service.html
http://www.bharatijoshi.com/shivaji-nagar-call-girls-service.html
http://www.bharatijoshi.com/yerwada-call-girls-service.html
http://www.bharatijoshi.com/hinjewadi-call-girls-service.html

pills erectile ... (not verified)

Xhwkey yifqsu pills erectile dysfunction cheapest ed pills online

pills erectile ... (not verified)

Xhwkey yifqsu pills erectile dysfunction cheapest ed pills online

cheapest ed pil... (not verified)

Smkjko dbdvvh ed pills online otc ed pills

خورشید (not verified)



عالی است مطلب گردآوری شده توسط شما بسی نیک تدوین شده است و من مطمئن

هستم که این پست به خاطر خیلی از وبلاگ نویسان پسندیده است

My homepage - سایت بازی انفجار

cheapest ed pil... (not verified)

Smkjko dbdvvh ed pills online otc ed pills

Shoshana (not verified)

What a information of un-ambiguity and preserveness of valuable knowledge regarding unexpected emotions.

Also visit my blog post :: 먹튀검증

male ed pills (not verified)

Cxgnzj ubczah male ed pills buying ed pills online

pills for erection (not verified)

Zjnqjd oeevfg ed meds online buy erection pills

cheap erectile ... (not verified)

Jrevoc zwnqkg cialis 5mg price new ed pills

Estherchera (not verified)

Qlhzmb euczgq Buy viagra online Samples of viagra

Latrice (not verified)

I must thank you for the efforts you have put in writing this site.

I really hope to check out the same high-grade blog posts by you in the future

as well. In fact, your creative writing abilities

has encouraged me to get my own website now ;)

Feel free to surf to my web-site; 먹튀

bisding (not verified)

Amoxil Cream Candidiasis Benzyl Alcohol https://abuycialisb.com/ - Cialis Viagra En Farmacias Espana Buy Cialis Generic Priligy Tablets

ojefiki (not verified)

Сумки из кожи актуальны всегдаи еще при грамотном использовании сливаются практически со всей одеждой. Кожаная сумка отличается длительным временем ношения и роскошным внешним видом. При должном уходе сумочка из естественного природного вещества проработает намного дольше, чем из ситнетического и еще со временем она, как хорошее вино, становится лучше, раскрывая свой потенциал. Выбрать кожаную сумку можно воспользовавшись сайтом вайлдберриз сумки, ведь там собрано достаточно вариантов сумок. На ресурсе вы обнаружите отзывы клиентов, которые помогут и сделать хороший выбор сумочки.

Sol (not verified)

If you wish for to take a good deal from this paragraph then you have to apply such techniques

to your won website.

Also visit my page: 먹튀검증

Annmarie (not verified)

Great web site. Plenty of useful info here.

I am sending it to several friends ans also sharing in delicious.

And of course, thanks on your effort!

Feel free to surf to my web blog; 오나홀

Estherchera (not verified)

Tujylu aldolc canadian pharmacy rx pharmacy

Ferne (not verified)

Thank you for another informative blog. Where else may I get that type of info written in such an ideal way?

I have a challenge that I'm simply now running on, and I have

been on the glance out for such info.

Here is my web page ... 오르몰

bisding (not verified)

Viagra Generico Costo https://viacialisns.com/ - Cialis Skin Rash Cephalexin Cialis Levitra Beneficios

bisding (not verified)

Viagra Generico Costo https://viacialisns.com/ - Cialis Skin Rash Cephalexin Cialis Levitra Beneficios

Estherchera (not verified)

Yumhan yiwnnf best online pharmacy walmart pharmacy

Estherchera (not verified)

Qrqidp mhaxya canada drugs online walmart pharmacy

Estherchera (not verified)

Ajawjf ocxlzo Viagra overnight shipping Buy online viagra

Estherchera (not verified)

Ajawjf ocxlzo Viagra overnight shipping Buy online viagra

Estherchera (not verified)

Igotci hucsjm Drug viagra Viagra brand

bisding (not verified)

Doxazosin No Prescription https://cheapcialisir.com/ - cialis online without prescription Buy now isotretinoin cialis 20mg price at walmart Discount Secure Zentel Internet C.O.D. Overseas No Doctor

bisding (not verified)

Doxazosin No Prescription https://cheapcialisir.com/ - cialis online without prescription Buy now isotretinoin cialis 20mg price at walmart Discount Secure Zentel Internet C.O.D. Overseas No Doctor

Estherchera (not verified)

Hoqajh rudcll generic viagra online Generic viagra us

bisding (not verified)

Buy Cheap Doxycycline In Usa https://agenericcialise.com/ - Cialis Vendo Cialis En Madrid Cialis Amoxicillin Childrens Dosage 32 Bs

Estherchera (not verified)

Ozypab roflld Buy viagra in canada pharmacy online

Estherchera (not verified)

Ozypab roflld Buy viagra in canada pharmacy online

Estherchera (not verified)

Jpmedd hkefur Cialis by mail rx pharmacy

Estherchera (not verified)

Ezzbct xhvuaf Get cialis canadian pharmacy

Noemi (not verified)

Hi, I do think this is an excellent web site.

I stumbledupon it ;) I am going to revisit yet again since I book-marked

it. Money and freedom is the best way to change, may you be rich and continue to guide other

people.

Feel free to surf to my homepage: shareware sports bettingsports betting sites

Estherchera (not verified)

Uuerdw ncuray cialis generic canadian online pharmacy

Estherchera (not verified)

Uuerdw ncuray cialis generic canadian online pharmacy

Estherchera (not verified)

Uuerdw ncuray cialis generic canadian online pharmacy

Estherchera (not verified)

Afbpdo nmlzhw Buy cialis online cvs pharmacy

Estherchera (not verified)

Afbpdo nmlzhw Buy cialis online cvs pharmacy

Estherchera (not verified)

Afbpdo nmlzhw Buy cialis online cvs pharmacy

Drug viagra (not verified)

Suzwad qqknqo vardenafil pill

Issac (not verified)

First of all I would like to say awesome blog! I had a quick question in which I'd like to ask if you don't mind.

I was curious to find out how you center yourself and clear your mind

prior to writing. I have had a tough time clearing my mind in getting my thoughts out.

I do enjoy writing however it just seems like the first 10

to 15 minutes are wasted simply just trying to figure out how to begin. Any

recommendations or hints? Thanks!

Also visit my blog :: 섹스기구 (http://www.cvma-korea.org/MyBB/member.php?action=profile&uid=152083)

Analisa (not verified)

Nice blog! Is your theme custom made or did you download it from somewhere?

A theme like yours with a few simple tweeks would really

make my blog jump out. Please let me know where you got your theme.

Cheers

Feel free to surf to my page - 오르몰

Drug viagra (not verified)

Suzwad qqknqo vardenafil pill

Buy viagra no p... (not verified)

Dledkm bhzxbq levitra for sale

Buy viagra no p... (not verified)

Dledkm bhzxbq levitra for sale

phefewedy (not verified)

Cialis Se Puede Comprar https://agenericcialise.com/ - buying cialis online reviews Acheter Viagra Site Francais cialis generic online Can You Get High Offof Keflex

phefewedy (not verified)

Cialis Se Puede Comprar https://agenericcialise.com/ - buying cialis online reviews Acheter Viagra Site Francais cialis generic online Can You Get High Offof Keflex