How to create an Order and Order item in Drupal commerce for Drupal 8

Posted by admin on Wed, 01/17/2018 - 10:20

Code snippet to create an Order and Order Items in Drupal Commerce for Drupal 8.

$order_item = \Drupal\commerce_order\Entity\OrderItem::create([
  'type' => 'product_variation', // The default order item type is 'product_variation' which references the product variation purchasable entity type.
  'purchased_entity' => $variation, // The product variation we created above.
  'quantity' => 2,
  'unit_price' => $variation->getPrice(),
]);
$order_item->save();
 
// You can set the quantity with setQuantity.
$order_item->setQuantity('1');
$order_item->save();
 
// You can also set the price with setUnitPrice.
$unit_price = new  \Drupal\commerce_price\Price('9.99', 'USD');
$order_item->setUnitPrice($unit_price);
$order_item->save();
 
// Next we create the billing profile.
$profile = \Drupal\profile\Entity\Profile::create([
  'type' => 'customer',
  'uid' => 1, // The user id that the billing profile belongs to.
]);
$profile->save();
 
// Next, we create the order.
$order = \Drupal\commerce_order\Entity\Order::create([
  'type' => 'default', // The default order type is 'default'.
  'state' => 'draft', // The states for the default order type are draft, completed, canceled
  'mail' => 'user@example.com', // The email address for the order.
  'uid' => 1, // The user id the order belongs to.
  'ip_address' => '127.0.0.1', // The ip address the user ordered from.
  'order_number' => '6', // Sets the order number. If left out, will use the order's entity ID.
  'billing_profile' => $profile, // The profile we just created.
  'store_id' => $store->id(), // The store we created above.
  'order_items' => [$order_item], // The order item we just created.
  'placed' => time(), // The time the order was placed.
]);
$order->save();
 
// You can also add order items to an order.
$order->addItem($order_item);
$order->save();
×

Hey! Don't miss a new cool stuff!

Comments

MelviSpoig (not verified)

Superb postings, Thanks!
prescription drugs without doctor approval canada drug pharmacy walmart pharmacy online

viagra without ... (not verified)

Casually urine is highest by physicians in men or. http://plstobuy.com/ Sffwlx vqlxir

MelviSpoig (not verified)

Amazing a good deal of awesome knowledge!
best canadian online pharmacy canada online pharmacies canadian discount pharmacy

Travinareta (not verified)

Thanks a lot! Terrific stuff!
canada pharmacy cvs pharmacy online

Travinareta (not verified)

Thanks a lot! Terrific stuff!
canada pharmacy cvs pharmacy online

MelvinaSpoig (not verified)

Kudos, Plenty of write ups.
reputable canadian mail order pharmacies pharmacy online canada drugs pharmacy

MelviSpoig (not verified)

You actually explained this effectively!
northwest pharmacy canada pharmacy canada drug

MelviSpoig (not verified)

You actually explained this effectively!
northwest pharmacy canada pharmacy canada drug

MelvinaSpoig (not verified)

Kudos, Plenty of write ups.
reputable canadian mail order pharmacies pharmacy online canada drugs pharmacy

Tommyvoibe (not verified)

Устали платить за контент? Перестаньте. Устраивайтесь поудобнее и насладитесь бесплатным просмотром в hd качестве. Смотрите видео в высоком качестве. Просмотр онлайн видео - все фильмы на вашем ПК, телефоне или планшете бесплатно на [url=https://hdvideo.design]https://hdvideo.design[/url]

Комедии, приколы, интервью, мелодрамы, триллеры, обзоры и многое другое — фильмы в HDVideo.design на любой вкус! Новинки и эксклюзивы.
Только легальный контент. Смотрите любимые видео, слушайте любимые песни, скачивайте ролики и делитесь ими с друзьями, близкими.
Здесь можно смотреть видео онлайн в хорошем качестве бесплатно без регистрации и смс.
https://hdvideo.design

Donaldabsek (not verified)

Truly loads of helpful information! online pharmacies online pharmacy busted

MelviSpoig (not verified)

Fantastic material, Thank you.
shoppers drug mart pharmacy global pharmacy canada canadian pharcharmy online fda approved

MelvinaSpoig (not verified)

Thanks, A lot of content!
no prescription pharmacy rx pharmacy best canadian prescription prices

MelviSpoig (not verified)

Fantastic material, Thank you.
shoppers drug mart pharmacy global pharmacy canada canadian pharcharmy online fda approved

MelvinaSpoig (not verified)

Thanks, A lot of content!
no prescription pharmacy rx pharmacy best canadian prescription prices

MelvinaSpoig (not verified)

Incredible a good deal of very good material!
canada pharma limited canada online pharmacies canadian pharmaceuticals online reviews

MelviSpoig (not verified)

You expressed it really well.
medicine online order online pharmacies canada drugs online

MelviSpoig (not verified)

You mentioned this exceptionally well.
northwest pharmacy canada pharmaceutical online ordering apollo pharmacy online

MelvinaSpoig (not verified)

You mentioned it well!
best online canadian pharcharmy erectile dysfunction medications online medicine order discount

MelviSpoig (not verified)

You mentioned this exceptionally well.
northwest pharmacy canada pharmaceutical online ordering apollo pharmacy online

MelvinaSpoig (not verified)

You mentioned it well!
best online canadian pharcharmy erectile dysfunction medications online medicine order discount

Donaldabsek (not verified)

Really loads of terrific facts! canadian pharmacies that are legit best online canadian pharmacy

MelvinaSpoig (not verified)

Incredible lots of awesome information!
online prescription drugs viagra canada pharmacy near me

MelviSpoig (not verified)

Regards, Lots of postings.
canada pharmacies online online pharmacies canadian online pharmacy reviews

MelviSpoig (not verified)

Regards, Lots of postings.
canada pharmacies online online pharmacies canadian online pharmacy reviews

esijjudanus (not verified)

So zeu.vrfa.web-tricks.org.gjm.gb intubator floor, nephrocalcinosis [URL=http://monticelloptservices.com/product/retin-a/]tretinoin cream retin a .05[/URL] [URL=http://homeairconditioningoutlet.com/drug/pharmacy/]where to buy pharmacy[/URL] [URL=http://best-online-mba.net/drug/levitra/]generic levitra 20mg[/URL] [URL=http://bibletopicindex.com/drugs/buy-cialis-online/]cialis buy online[/URL] [URL=http://allgeekguide.com/drugs/viagra-generic/]cheapviagra.com[/URL] [URL=http://lbprintery.net/drugs/cytotec/]buy cytotec[/URL] [URL=http://lovecamels.com/buy-cialis/]cialis generic canada[/URL] [URL=http://lovecamels.com/buy-lasix-online/]lasix on line[/URL] [URL=http://bibletopicindex.com/drugs/levitra-20mg/]levitra pills[/URL] [URL=http://rinconprweddingplanner.com/drugs/buy-propecia-online/]propecia online no prescription[/URL] stridor retin a cream generic pharmacy online levitra for sale cialis.com lowest price 20 mg cialis where to buy viagra online canada cytotec canadian cialis furosemide 40 mg levitra price propecia without prescription door-bell, bereaved, suprapubic http://monticelloptservices.com/product/retin-a/ retin-a cream http://homeairconditioningoutlet.com/drug/pharmacy/ northwestpharmacy.com canada http://best-online-mba.net/drug/levitra/ order levitra online http://bibletopicindex.com/drugs/buy-cialis-online/ buy cialis online http://allgeekguide.com/drugs/viagra-generic/ viagra generic http://lbprintery.net/drugs/cytotec/ where to buy misoprostol http://lovecamels.com/buy-cialis/ 20mg generic cialis http://lovecamels.com/buy-lasix-online/ buy furosemide http://bibletopicindex.com/drugs/levitra-20mg/ vardenafil generic http://rinconprweddingplanner.com/drugs/buy-propecia-online/ buy propecia online follow neuritis pelvis.

mukatot (not verified)

May xdw.eucs.web-tricks.org.zhp.lv forsake compressed: [URL=http://lowesmobileplants.com/product/prednisolone-online-usa/]prednisolone without an rx[/URL] [URL=http://parentswithangst.com/drug/lasix/]lasix without a prescription[/URL] [URL=http://lovecamels.com/retin-a/]where to buy retin a[/URL] [URL=http://lovecamels.com/drug/kamagra/]kamagra oral jelly[/URL] [URL=http://glenwoodwine.com/drugs/buy-ventolin-online/]does salbutamol[/URL] [URL=http://aminahsorganicskinspa.com/drugs/retin-a/]retin-a micro[/URL] [URL=http://monticelloptservices.com/product/cialis/]cialis.com lowest price[/URL] [URL=http://aminahsorganicskinspa.com/drugs/buy-amoxicillin/]amoxicillin 500 mg to buy[/URL] [URL=http://aminahsorganicskinspa.com/drugs/buy-amoxicillin-online/]order amoxicillin 500 mg without prescri...[/URL] canadian pharmacy amoxil [URL=http://center4family.com/drug/prednisone/]prednisone[/URL] obstruction: inspect prednisolone lasix no prescription retin a cream 0.05 kamagra for sale buy ventolin online retin a micro retin a micro buy cialis online buy cialis online amoxicillin buy online amoxicillin no prescription prednisone 20 mg image, presence http://lowesmobileplants.com/product/prednisolone-online-usa/ prednisolone http://parentswithangst.com/drug/lasix/ lasix http://lovecamels.com/retin-a/ buy retin-a online http://lovecamels.com/drug/kamagra/ kamagra oral jelly http://glenwoodwine.com/drugs/buy-ventolin-online/ ventolin online pharmacy http://aminahsorganicskinspa.com/drugs/retin-a/ tretinoin cream http://monticelloptservices.com/product/cialis/ cialis 20 mg best price http://aminahsorganicskinspa.com/drugs/buy-amoxicillin/ purchasing amoxicillin 500mg capsules amoxicillin buy online http://aminahsorganicskinspa.com/drugs/buy-amoxicillin-online/ buy amoxicillin without prescription http://center4family.com/drug/prednisone/ buy prednisone prednisone 20 mg regimen: anti-infective.

MelvinaSpoig (not verified)

You explained that well!
pharmacie canadienne online canadian pharmacy best online international pharmacies

MelviSpoig (not verified)

Amazing forum posts. Thanks a lot!
non prescription online pharmacy reviews canadian viagra prescription cost

MelvinaSpoig (not verified)

You explained that well!
pharmacie canadienne online canadian pharmacy best online international pharmacies

MelviSpoig (not verified)

Amazing forum posts. Thanks a lot!
non prescription online pharmacy reviews canadian viagra prescription cost

Donaldabsek (not verified)

Great information, With thanks. canadian drugs canadian pharmacy viagra

MelvinaSpoig (not verified)

Superb write ups. Thanks.
canadian pharmacy reviews canada drugs prescription price comparison

Travinareta (not verified)

Wow plenty of useful data.
online pharmacy list of legitimate canadian pharmacies

MelvinaSpoig (not verified)

Superb write ups. Thanks.
canadian pharmacy reviews canada drugs prescription price comparison

MelviSpoig (not verified)

Superb write ups, With thanks!
pharmacy online cheap canadian pharmacies online best canadian online pharmacy

MelvinaSpoig (not verified)

Fantastic content. Thanks!
online pharmacy school northwest pharmacy canadian pharma companies

MelviSpoig (not verified)

Awesome material, With thanks!
canada drugs pharmacy canadian pharmacy cialis canada rx

eperayeiwaee (not verified)

Don't map.oeoa.web-tricks.org.tel.au apical retrospective [URL=http://govtjobslatest.org/drugs/cipro/]cipro and side effects[/URL] [URL=http://best-online-mba.net/drug/cialis-online/]cialis online[/URL] [URL=http://fashionbillie.com/drugs/doxycycline-hyclate-100-mg/]buy doxycycline 100mg[/URL] doxycycline [URL=http://fashionbillie.com/drugs/buy-propecia/]propecia prescription[/URL] [URL=http://monticelloptservices.com/product/prednisolone/]online prednisolone no prescription[/URL] [URL=http://fashionbillie.com/drugs/tadalafil/]low cost cialis 20mg[/URL] [URL=http://rinconprweddingplanner.com/drugs/generic-propecia/]propecia 5 mg[/URL] [URL=http://lowesmobileplants.com/product/zenegra/]zenegra[/URL] [URL=http://lowesmobileplants.com/product/provigil/]provigil[/URL] buy provigil no prescription [URL=http://homeairconditioningoutlet.com/drug/cheap-levitra/]buy levitra[/URL] normoglycaemia generic cipro in canada cialis ic doxycycline propecia prescription cheap prednisolone tadalafil generic propecia zenegra 100 buy from usa natural provigil vardenafil generic reattach gynaecological, opacification, http://govtjobslatest.org/drugs/cipro/ buy cipro online http://best-online-mba.net/drug/cialis-online/ cialis cheap http://fashionbillie.com/drugs/doxycycline-hyclate-100-mg/ doxycycline walmart price http://fashionbillie.com/drugs/buy-propecia/ propecia price http://monticelloptservices.com/product/prednisolone/ prednisolone http://fashionbillie.com/drugs/tadalafil/ cialis generic cheap generic cialis 20mg http://rinconprweddingplanner.com/drugs/generic-propecia/ generic propecia http://lowesmobileplants.com/product/zenegra/ zenegra 100 mg tablets http://lowesmobileplants.com/product/provigil/ buy provigil http://homeairconditioningoutlet.com/drug/cheap-levitra/ vardenafil generic occlude research vasospasm suitable.

Donaldabsek (not verified)

Fantastic facts. Regards! pharmacy online top rated canadian pharmacies online

MelviSpoig (not verified)

Valuable info. Appreciate it.
cheap viagra online canadian pharmacy canadian pharmacies that are legit online pharmacy without scripts

MelvinaSpoig (not verified)

Nicely put, Thanks.
online pharmacy without scripts prescription drugs without doctor approval medicine online order

agahoqibite (not verified)

Unless qme.quhz.web-tricks.org.iry.vr names, [URL=http://bibletopicindex.com/drugs/prednisone-online/]buy prednisone without prescription[/URL] [URL=http://synergistichealthcenters.com/drugs/viagra/]cheap viagra generic[/URL] [URL=http://allgeekguide.com/drugs/prednisone-without-prescription/]prednisone for dogs without prescription[/URL] [URL=http://lovecamels.com/drug/cheap-viagra/]viagra[/URL] [URL=http://aminahsorganicskinspa.com/drugs/buy-retin-a/]retin a[/URL] [URL=http://proteinsportsnutrition.com/drugs/100-mg-viagra-lowest-price/]rid viagra emails[/URL] [URL=http://glenwoodwine.com/drugs/levitra-generic/]levitra generic[/URL] [URL=http://glenwoodwine.com/drugs/online-pharmacy/]online pharmacy propecia[/URL] [URL=http://futuremediaassociation.com/product/desyrel/]buy trazodone online[/URL] [URL=http://synergistichealthcenters.com/drugs/buy-levitra/]levitra[/URL] nationwide prednisone 20 mg buy brand viagra prednisone without prescription buy generic viagra viagra retin a without prescription viagra levitra pills 20 mg pharmacy rx one trazodone levitra rendering shadows http://bibletopicindex.com/drugs/prednisone-online/ prednisone online without prescription http://synergistichealthcenters.com/drugs/viagra/ viagra 100 mg http://allgeekguide.com/drugs/prednisone-without-prescription/ prednisone 20 mil grams http://lovecamels.com/drug/cheap-viagra/ viagra http://aminahsorganicskinspa.com/drugs/buy-retin-a/ buy retin a cream http://proteinsportsnutrition.com/drugs/100-mg-viagra-lowest-price/ price of 100mg viagra http://glenwoodwine.com/drugs/levitra-generic/ vardenafil 20mg price http://glenwoodwine.com/drugs/online-pharmacy/ canada pharmacy online no script http://futuremediaassociation.com/product/desyrel/ trazodone http://synergistichealthcenters.com/drugs/buy-levitra/ levitra levitra textual finger appearances mysterious.

MelvinaSpoig (not verified)

Very good data. Cheers.
cheap drugs online trust pharmacy canada online drugstore

MelvinaSpoig (not verified)

Very good data. Cheers.
cheap drugs online trust pharmacy canada online drugstore

MelviSpoig (not verified)

You stated it perfectly.
mexican border pharmacies northwest pharmacy/com pharmacy intern

Donaldabsek (not verified)

Amazing loads of beneficial info! aarp approved canadian online pharmacies drug costs

MelvinaSpoig (not verified)

Nicely put, Appreciate it.
mexican border pharmacies online pharmacy india no prescription online pharmacy

MelviSpoig (not verified)

Information well utilized.!
best 10 online canadian pharmacies mexican pharmacy online online canadian discount pharmacy

Travinareta (not verified)

Seriously tons of awesome facts.
canada drug pharmacy rx pharmacy

Add new comment