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

MelvinaSpoig (not verified)

You actually stated that wonderfully!
prescription drugs online without https://topcialisrxstore.com/order-lioresal-online-en.html online drugs

Donaldabsek (not verified)

Nicely put. Appreciate it! rocaltrol canadian pharmacy prescription discount

Donaldabsek (not verified)

Nicely put. Appreciate it! rocaltrol canadian pharmacy prescription discount

MelvinaSpoig (not verified)

You said that effectively.
order medicine online https://topviagrarxstore.com/order-amoxil-online-en.html online pharmacies canada

ipetetyi (not verified)

Examine qyt.oklo.web-tricks.org.jxd.lt work-up nasal [URL=http://gaiaenergysystems.com/product/discount-viagra/]discount viagra[/URL] [URL=http://looandfoo.com/brand-cialis/]brand name cialis without prescription[/URL] [URL=http://mytopbabyboynames.com/brand-cialis/]buy brand name cialis online[/URL] [URL=http://looandfoo.com/viagra-professional-generic/]viagra professional[/URL] [URL=http://columbiainnastoria.com/product/canadian-pharmacy/]canadian cialis online pharmacy[/URL] [URL=http://columbiainnastoria.com/product/cialis/]tadalafil generic[/URL] [URL=http://lovecamels.com/buy-lasix-online/]canadian pharmacy lasix[/URL] echo abscess, parent discount viagra discount viagra buying brand cialis online brand cialis online viagra professional pharmacy tadalafil generic buy furosemide lasix best price usa breech http://gaiaenergysystems.com/product/discount-viagra/ viagra online canada pharmacy http://looandfoo.com/brand-cialis/ brand cialis http://mytopbabyboynames.com/brand-cialis/ brand cialis online http://looandfoo.com/viagra-professional-generic/ urovaxon viagra professional doxycycline rodomicina viagra professional doxycycline rodomicina cetirizini http://columbiainnastoria.com/product/canadian-pharmacy/ canadian pharmacy pharmacy http://columbiainnastoria.com/product/cialis/ cialis without pres http://lovecamels.com/buy-lasix-online/ dose of furosemide for dogs fissures, excess.

MelvinaSpoig (not verified)

Appreciate it! Ample facts.
canadian drugs pilex generic canadian prescriptions online

MelvinaSpoig (not verified)

With thanks. I value it.
cialis from canada https://canadianpharmaceuticalsrx.com/order-decadron-online-en.html canadian online pharmacies legitimate by aarp

MelviSpoig (not verified)

Excellent information. Thank you.
northwest pharmacy/com mentat sale canadian prescription drugs

MelvinaSpoig (not verified)

With thanks. I value it.
cialis from canada https://canadianpharmaceuticalsrx.com/order-decadron-online-en.html canadian online pharmacies legitimate by aarp

MelviSpoig (not verified)

Excellent information. Thank you.
northwest pharmacy/com mentat sale canadian prescription drugs

MelviSpoig (not verified)

Excellent information. Thank you.
northwest pharmacy/com mentat sale canadian prescription drugs

Donaldabsek (not verified)

Great tips. Cheers. ceftin online online pet pharmacy

Donaldabsek (not verified)

Great tips. Cheers. ceftin online online pet pharmacy

MelviSpoig (not verified)

With thanks! Good information!
prescription prices comparison generic valtrex canadian online pharmacies legitimate by aarp

MelviSpoig (not verified)

With thanks! Good information!
prescription prices comparison generic valtrex canadian online pharmacies legitimate by aarp

MelvinaSpoig (not verified)

You actually mentioned it wonderfully!
canada rx herbolax canadian pharmacy drugs from canada

omogami (not verified)

If zee.xdcy.web-tricks.org.vka.kk anthrax [URL=http://livinlifepc.com/product/viagra-canada/]viagra online no prescription[/URL] canada viagra [URL=http://parentswithangst.com/drug/cialis-20-mg-lowest-price/]cialis 20 mg lowest price[/URL] [URL=http://center4family.com/drug/online-pharmacy/]canada pharmacy[/URL] [URL=http://columbiainnastoria.com/product/viagra-professional-100-mg-from-usa/]buy generic viagra professional[/URL] [URL=http://center4family.com/product/viagra/]sildenafil similares[/URL] [URL=http://columbiainnastoria.com/product/lyrica/]swollen hands feet lyrica[/URL] [URL=http://columbiainnastoria.com/product/generic-cialis-lowest-price/]cialis canadian pharmacy[/URL] welfare viagra cialis 20 mg lowest price online pharmacy buy generic viagra professional lowest price for viagra 100mg lyrica cialis calculated http://livinlifepc.com/product/viagra-canada/ viagra coupons viagra http://parentswithangst.com/drug/cialis-20-mg-lowest-price/ buy cialis uk http://center4family.com/drug/online-pharmacy/ canada pharmacy http://columbiainnastoria.com/product/viagra-professional-100-mg-from-usa/ where to buy viagra professional online http://center4family.com/product/viagra/ patient assistance for viagra http://columbiainnastoria.com/product/lyrica/ lyrica partial onset seizures http://columbiainnastoria.com/product/generic-cialis-lowest-price/ buy cialis online canada cialis biting, endometrium.

omogami (not verified)

If zee.xdcy.web-tricks.org.vka.kk anthrax [URL=http://livinlifepc.com/product/viagra-canada/]viagra online no prescription[/URL] canada viagra [URL=http://parentswithangst.com/drug/cialis-20-mg-lowest-price/]cialis 20 mg lowest price[/URL] [URL=http://center4family.com/drug/online-pharmacy/]canada pharmacy[/URL] [URL=http://columbiainnastoria.com/product/viagra-professional-100-mg-from-usa/]buy generic viagra professional[/URL] [URL=http://center4family.com/product/viagra/]sildenafil similares[/URL] [URL=http://columbiainnastoria.com/product/lyrica/]swollen hands feet lyrica[/URL] [URL=http://columbiainnastoria.com/product/generic-cialis-lowest-price/]cialis canadian pharmacy[/URL] welfare viagra cialis 20 mg lowest price online pharmacy buy generic viagra professional lowest price for viagra 100mg lyrica cialis calculated http://livinlifepc.com/product/viagra-canada/ viagra coupons viagra http://parentswithangst.com/drug/cialis-20-mg-lowest-price/ buy cialis uk http://center4family.com/drug/online-pharmacy/ canada pharmacy http://columbiainnastoria.com/product/viagra-professional-100-mg-from-usa/ where to buy viagra professional online http://center4family.com/product/viagra/ patient assistance for viagra http://columbiainnastoria.com/product/lyrica/ lyrica partial onset seizures http://columbiainnastoria.com/product/generic-cialis-lowest-price/ buy cialis online canada cialis biting, endometrium.

Evelynbop (not verified)

I Got Kailasa PassportTamilMadan Gowri Vlog

MelvinaSpoig (not verified)

This is nicely said! !
online order medicine generic slimfast pharmacy canada

MelvinaSpoig (not verified)

This is nicely said! !
online order medicine generic slimfast pharmacy canada

MelviSpoig (not verified)

Thanks! Plenty of postings!
canadian pharmacy viagra generic vibramycin canadian pharmacy ordering prescriptions from canada legally

MelviSpoig (not verified)

Thanks! Plenty of postings!
canadian pharmacy viagra generic vibramycin canadian pharmacy ordering prescriptions from canada legally

MelviSpoig (not verified)

Thanks! Plenty of postings!
canadian pharmacy viagra generic vibramycin canadian pharmacy ordering prescriptions from canada legally

MelvinaSpoig (not verified)

Nicely put, Kudos!
costco pharmacy pricing abana generic canadian pharmacies online

MelvinaSpoig (not verified)

Nicely put, Kudos!
costco pharmacy pricing abana generic canadian pharmacies online

Donaldabsek (not verified)

Superb data. Thanks a lot. kamagra polo usa canadian pharmacy review

efiunebiwe (not verified)

Special ufl.rybk.web-tricks.org.ggq.tj galactorrhoea compartments [URL=http://center4family.com/drug/prednisone/]buy prednisone online[/URL] [URL=http://schoolsbiz.com/pharmacy/]online pharmacy no prescription[/URL] pharmacy best price usa [URL=http://healthkosh.com/prednisone/]order prednisone[/URL] prednisone pill [URL=http://healthkosh.com/lasix/]lasix[/URL] lasix without perscription [URL=http://mytopbabyboynames.com/viagra/]viagra online cheap[/URL] [URL=http://healthkosh.com/sildalis/]sildalis kaufen[/URL] [URL=http://columbiainnastoria.com/product/lyrica-50-mg/]lyrica pfizer[/URL] lyrica pfizer lacrimal prednisone without a perscription pharmacy prednisone without a prescription lasix viagra sildalis online from canada lyrica 150mg rudimentary http://center4family.com/drug/prednisone/ prednisone http://schoolsbiz.com/pharmacy/ northwest pharmacy canada http://healthkosh.com/prednisone/ prednisone online no prescription http://healthkosh.com/lasix/ lasix http://mytopbabyboynames.com/viagra/ lowest price viagra 100mg http://healthkosh.com/sildalis/ sildalis online from canada sildalis http://columbiainnastoria.com/product/lyrica-50-mg/ lyrica pfizer lactate-free choices.

efiunebiwe (not verified)

Special ufl.rybk.web-tricks.org.ggq.tj galactorrhoea compartments [URL=http://center4family.com/drug/prednisone/]buy prednisone online[/URL] [URL=http://schoolsbiz.com/pharmacy/]online pharmacy no prescription[/URL] pharmacy best price usa [URL=http://healthkosh.com/prednisone/]order prednisone[/URL] prednisone pill [URL=http://healthkosh.com/lasix/]lasix[/URL] lasix without perscription [URL=http://mytopbabyboynames.com/viagra/]viagra online cheap[/URL] [URL=http://healthkosh.com/sildalis/]sildalis kaufen[/URL] [URL=http://columbiainnastoria.com/product/lyrica-50-mg/]lyrica pfizer[/URL] lyrica pfizer lacrimal prednisone without a perscription pharmacy prednisone without a prescription lasix viagra sildalis online from canada lyrica 150mg rudimentary http://center4family.com/drug/prednisone/ prednisone http://schoolsbiz.com/pharmacy/ northwest pharmacy canada http://healthkosh.com/prednisone/ prednisone online no prescription http://healthkosh.com/lasix/ lasix http://mytopbabyboynames.com/viagra/ lowest price viagra 100mg http://healthkosh.com/sildalis/ sildalis online from canada sildalis http://columbiainnastoria.com/product/lyrica-50-mg/ lyrica pfizer lactate-free choices.

MelviSpoig (not verified)

You made your stand extremely effectively!!
cheap prescription drugs online voveran sr sale legitimate online pharmacies

MelvinaSpoig (not verified)

Very good information. Thanks a lot!
pharmacies online plan b usa pharmacy online store

efiunebiwe (not verified)

Special ufl.rybk.web-tricks.org.ggq.tj galactorrhoea compartments [URL=http://center4family.com/drug/prednisone/]buy prednisone online[/URL] [URL=http://schoolsbiz.com/pharmacy/]online pharmacy no prescription[/URL] pharmacy best price usa [URL=http://healthkosh.com/prednisone/]order prednisone[/URL] prednisone pill [URL=http://healthkosh.com/lasix/]lasix[/URL] lasix without perscription [URL=http://mytopbabyboynames.com/viagra/]viagra online cheap[/URL] [URL=http://healthkosh.com/sildalis/]sildalis kaufen[/URL] [URL=http://columbiainnastoria.com/product/lyrica-50-mg/]lyrica pfizer[/URL] lyrica pfizer lacrimal prednisone without a perscription pharmacy prednisone without a prescription lasix viagra sildalis online from canada lyrica 150mg rudimentary http://center4family.com/drug/prednisone/ prednisone http://schoolsbiz.com/pharmacy/ northwest pharmacy canada http://healthkosh.com/prednisone/ prednisone online no prescription http://healthkosh.com/lasix/ lasix http://mytopbabyboynames.com/viagra/ lowest price viagra 100mg http://healthkosh.com/sildalis/ sildalis online from canada sildalis http://columbiainnastoria.com/product/lyrica-50-mg/ lyrica pfizer lactate-free choices.

MelvinaSpoig (not verified)

Kudos. A good amount of info.
canadian family pharmacy anacin online cheap pharmacy online

MelviSpoig (not verified)

Nicely put. Many thanks!
canadian pharmacy 365 proair inhaler without a doctor prescription canada rx

MelviSpoig (not verified)

You actually explained this perfectly.
prescription drug generic hiforce ods international drug mart canadian pharmacy online store

MelvinaSpoig (not verified)

Many thanks. Great stuff.
canadian pharmacy viagra depakote without a doctor prescription cvs online pharmacy

odeogtififel (not verified)

If san.hghc.web-tricks.org.psv.ec acquired, excystation lactate [URL=http://pukaschoolinc.com/viagra-generic/]viagra online store[/URL] [URL=http://best-online-mba.net/drug/cialis-online/]cialis[/URL] [URL=http://gaiaenergysystems.com/product/viagra/]lowest price on viagra[/URL] [URL=http://mytopbabyboynames.com/prednisone/]www.prednisone.com[/URL] [URL=http://healthkosh.com/amoxicillin-500-mg/]amoxicillin online[/URL] [URL=http://livinlifepc.com/product/prednisone/]online prednisone[/URL] [URL=http://pukaschoolinc.com/viagra/]viagra[/URL] knives black-outs viagra generic generic cialis paypal reviews generic viagra purchase prednisone buy amoxil online prednisone without an rx viagra should otitis, screen; http://pukaschoolinc.com/viagra-generic/ viagra for men http://best-online-mba.net/drug/cialis-online/ cialis online http://gaiaenergysystems.com/product/viagra/ lowest price on viagra contraindicaciones de viagra http://mytopbabyboynames.com/prednisone/ prednisone 20 mg http://healthkosh.com/amoxicillin-500-mg/ amoxicillin 500mg capsules to buy http://livinlifepc.com/product/prednisone/ 20 mg prednisone http://pukaschoolinc.com/viagra/ viagra online attack, necessary?

Donaldabsek (not verified)

Incredible a good deal of superb tips! buy voveran sr online online pharmacies canada

MelvinaSpoig (not verified)

You actually expressed this well!
drugs without prescription https://canadiantoprxstore.com/order-kamagra_gold-online-en.html canadian pharcharmy

MelvinaSpoig (not verified)

You actually expressed this well!
drugs without prescription https://canadiantoprxstore.com/order-kamagra_gold-online-en.html canadian pharcharmy

MelvinaSpoig (not verified)

You actually said it terrifically.
canada pharmacy online orders lasix generic walgreens online pharmacy

MelviSpoig (not verified)

Wow many of helpful facts.
cialis canadian pharmacy generic geriforte northwest pharmacy

MelviSpoig (not verified)

Wow many of helpful facts.
cialis canadian pharmacy generic geriforte northwest pharmacy

Donaldabsek (not verified)

You revealed that adequately! copegus generic generic viagra online pharmacy

MelvinaSpoig (not verified)

Really all kinds of good information!
canada viagra arimidex without a doctor prescription online pharmacy no prescription needed

MelviSpoig (not verified)

With thanks. Lots of information.
drugs from canada online buy voltaren sr online canada pharmaceuticals

MelvinaSpoig (not verified)

Really all kinds of good information!
canada viagra arimidex without a doctor prescription online pharmacy no prescription needed

MelvinaSpoig (not verified)

Really all kinds of good information!
canada viagra arimidex without a doctor prescription online pharmacy no prescription needed

MelvinaSpoig (not verified)

Thanks! Quite a lot of facts.
canada pharmaceuticals online generic effexor generic canada drugs direct

MelviSpoig (not verified)

Thanks a lot! Quite a lot of forum posts.
pharmacy tech viagra soft canadian pharmacy walgreens pharmacy

Add new comment