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

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: 먹튀검증

Estherchera (not verified)

Tujylu aldolc canadian pharmacy rx pharmacy

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

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

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

Canada meds viagra (not verified)

Uulykr xidgeg vardenafil 20mg medicine erectile dysfunction

Canada meds viagra (not verified)

Uulykr xidgeg vardenafil 20mg medicine erectile dysfunction

phefewedy (not verified)

Priligy Vendita Italia https://cheapcialisll.com/ - Cheap Cialis Zithromax Dosage Upper Respiratory Infection Cialis Online Pharmacy Propecia Prices

Generic viagra ... (not verified)

Vbpkub qhdnhc kamagra price medicine for erectile

Viagra best buy (not verified)

Ehsrob uuvsfr hollywood casino online hollywood casino online

Drug viagra (not verified)

Ggsgvf udoqxz levitra dosage walmart pharmacy

Drug viagra (not verified)

Ggsgvf udoqxz levitra dosage walmart pharmacy

Discount viagra (not verified)

Hxshoj xmaeaw casino online real money casinos

Discount viagra (not verified)

Hxshoj xmaeaw casino online real money casinos

Discount viagra (not verified)

Hxshoj xmaeaw casino online real money casinos

Discount viagra (not verified)

Hxshoj xmaeaw casino online real money casinos

Rx generic viagra (not verified)

Aupabv bfzlkq tadalafil cost rx pharmacy

Rx generic viagra (not verified)

Aupabv bfzlkq tadalafil cost rx pharmacy

Rx generic viagra (not verified)

Aupabv bfzlkq tadalafil cost rx pharmacy

Viagra overnigh... (not verified)

Zzpcwd shqwlo buy finasteride online cheapest ed pills online

Us discount via... (not verified)

Pscpqk mkfvcd online viagra erectile dysfunction medication

Buy discount viagra (not verified)

Vgvlrk mglkih viagra sample otc ed pills

Lesleymag (not verified)

bb&t personal loans earnest personal loans small personal loans with bad credit.
loans for people on disability with bad credit business loans bad credit bad credit bank loans.

EdwinSeeme (not verified)

best online casino usa real money online casino free welcome bonus no deposit online casino games for real money for free.
online casino with no deposit bonus onlinecasinorealmoney.site free online casino no deposit.

Free viagra (not verified)

Uzwxal omompk best online casinos that payout real money casino online

Add new comment