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

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

Anna (not verified)

I like this web site very much so much great info.

Have a look at my web page ... 먹튀검증 (http://thaou3245481645893.wikidot.com/)

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

Ronald (not verified)

Thanks to my father who informed me about this website, this website

is truly awesome.

Here is my blog post :: 성인용품 - www.marijuanamilitia.com,

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

Jerome (not verified)

Thanks for some other informative web site.

The place else may I get that kind of information written in such an ideal approach?

I have a undertaking that I am just now working on, and

I've been at the look out for such information.

Feel free to surf to my web blog ... 먹튀검증

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

Yetta (not verified)

My family members every time say that I am killing my time here at

web, but I know I am getting familiarity every day by reading such pleasant articles or

reviews.

My site :: 오르몰

Free viagra (not verified)

Hjtdxj toyeap viagra samples Buy pfizer viagra online

EdwinSeeme (not verified)

installment loans for self employed with bad credit badcreditloans01.com short term loans for bad credit same day.
usa online casino no deposit bonus onlinecasinonodeposit.site free online casino win real money no deposit.

Alternative for... (not verified)

Etushr xkflxp online casinos for usa players golden nugget online casino

EdwinSeeme (not verified)

utah payday loans paydayloans01.com amg services payday loans.
usa casino online real money onlinecasino002 online casino with no deposit.

Generic viagra us (not verified)

Dqeiqw dbggop casino moons online casino casino online slots

EdwinSeeme (not verified)

va loans bad credit consolidation loans for bad credit bad credit personal loans guaranteed approval ny.
online casino free spins no deposit onlinecasinonodeposit.website nj online casino no deposit.

Generic viagra ... (not verified)

Nyktwv iqznpy instant payday loans loans with bad credit

Pharmacy viagra (not verified)

Ckytan xesrvu best payday loans casino real money

Kennethglobe (not verified)

free casino slots online online casino free credit no deposit casino online no deposit.
discover personal loans contact onemain financial personal loans bankofamerica personal loans.

Sale viagra (not verified)

Ibzpco umdcyp generic tadalafil pharmacy online

Buy viagra us (not verified)

Dnlkan spoqms online loan pay day loans

Kennethglobe (not verified)

casino royale watch online free online casino no deposit bonus real vegas online casino no deposit bonus codes.
online casino real money onlinecasinorealmoney.website online casino games for real money for free.

Kennethglobe (not verified)

casino royale watch online free online casino no deposit bonus real vegas online casino no deposit bonus codes.
online casino real money onlinecasinorealmoney.website online casino games for real money for free.

Buy viagra no p... (not verified)

Stilqs gvxloc personal loan bad credit loans with bad credit

Haroldzex (not verified)

online casino games free onlinecasino001.com no deposit online casino.
cash payday loans payday loans direct lender ez payday loans locations.
guaranteed personal loans $5000 best online personal loans banks that do personal loans.
online casino real money usa onlinecasinonodeposit casino play online.

Viagra alternative (not verified)

Czkyqg vtocpw loans for bad credit big fish casino online

Charleskap (not verified)

avant personal loans review small personal loans near me personal loans for low income.
payday loans with ssi debit card paydayloans1.site payday loans colorado springs.
bad credit collateral loans bad credit loans monthly payments direct lenders bad credit installment loans not payday loans.
best debt consolidation loans for bad credit bad credit rv loans bbb accredited personal loans for bad credit.

Williamsmons (not verified)

Fantastic forum posts, Many thanks. canadian viagra

Williamsmons (not verified)

Fantastic forum posts, Many thanks. canadian viagra

WalterShund (not verified)

payday loans deposited on prepaid debit card payday loans georgetown ky get out of payday loans.
loandepot personal loans personalloans1 ally bank personal loans.
no teletrack payday loans direct lenders 100 approval online payday loans direct lenders are payday loans legal in ny.
discover card personal loans bad credit car loans pros and cons of personal loans to pay off credit card debt.

Price viagra (not verified)

Opnwrw egjtwg payday loans for bad credit online casino real money paypal

Price viagra (not verified)

Opnwrw egjtwg payday loans for bad credit online casino real money paypal

Williamsmons (not verified)

Really many of amazing facts! canadian online pharmacy

phefewedy (not verified)

No Rx Online Pharmacy https://agenericcialise.com/ - Cialis Order From Phone Amoxicillin Cialis Propecia En Valencia

phefewedy (not verified)

No Rx Online Pharmacy https://agenericcialise.com/ - Cialis Order From Phone Amoxicillin Cialis Propecia En Valencia

Add new comment