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

Dwayneedine (not verified)

работа в сфере досуга краснодар

https://www.cryptic11k.com/domain/rabotavescortsochi.com

BryaneTwimb (not verified)

Amazing tips. Thanks a lot.
i need help writing a essay cheap essay writing services best custom writing website
how to write a rogerian essay j13mtn 7344674

BryaneTwimb (not verified)

Amazing tips. Thanks a lot.
i need help writing a essay cheap essay writing services best custom writing website
how to write a rogerian essay j13mtn 7344674

BryaneTwimb (not verified)

Amazing tips. Thanks a lot.
i need help writing a essay cheap essay writing services best custom writing website
how to write a rogerian essay j13mtn 7344674

DurekFak (not verified)

Wonderful posts, Thank you! colleges that require essays for admission [url=https://topswritingservices.com/]how to write an argument essay[/url] dissertation essay

BryaneTwimb (not verified)

Nicely put. Thank you!
writing a good essay for college dissertation help assignment writer
nursing essay help p52xgs f03b896

KennathTarge (not verified)

Very good content. Many thanks!
how to write a transfer essay courses work a website that writes essays for you

Переводчик ново... (not verified)
BryaneTwimb (not verified)

This is nicely said! .
custom essay services phd thesis help custom writing cheap
how to write an introduction for a persuasive essay j99gur ce10_20

BryaneTwimb (not verified)

This is nicely said! .
custom essay services phd thesis help custom writing cheap
how to write an introduction for a persuasive essay j99gur ce10_20

Dannytub (not verified)

logatomtrade отзывы https://rucoins.info/news/logatomtrade-otzyvy-skolko-mozhno-zarabotat-s-brokerom/

BryaneTwimb (not verified)

Awesome stuff. Kudos!
plagiarism college essay professional resume writing service professional writing services rates
college essay editing service w129jm 7f03b89

BryaneTwimb (not verified)

Awesome stuff. Kudos!
plagiarism college essay professional resume writing service professional writing services rates
college essay editing service w129jm 7f03b89

BryaneTwimb (not verified)

Awesome stuff. Kudos!
plagiarism college essay professional resume writing service professional writing services rates
college essay editing service w129jm 7f03b89

DurekFak (not verified)

Truly quite a lot of amazing tips. personal essay college application [url=https://service-essay.com/]dissertation definition[/url] dissertation research grants

DurekFak (not verified)

Truly quite a lot of amazing tips. personal essay college application [url=https://service-essay.com/]dissertation definition[/url] dissertation research grants

DurekFak (not verified)

Truly quite a lot of amazing tips. personal essay college application [url=https://service-essay.com/]dissertation definition[/url] dissertation research grants

BryaneTwimb (not verified)

Effectively voiced indeed! .
writing narrative essays thesis editing do my writing homework
write my essay cheap r16rpx ec17657

KennathTarge (not verified)

Nicely put, Kudos.
how to write an autobiographical essay research paper best assignment writing service

BryaneTwimb (not verified)

Thanks, Awesome stuff!
essay introduction help thesis writing help coursework writer
writing essays for money o930zf 44674ec

KennathTarge (not verified)

Cheers, I enjoy this!
how to write a literary essay dissertation definition cover letter writing service

Robertbat (not verified)
KennathTarge (not verified)

Cheers, I enjoy this!
how to write a literary essay dissertation definition cover letter writing service

Josephgow (not verified)
DurekFak (not verified)

Amazing many of beneficial advice! writers essay [url=https://scoringessays.com/]thesis writing help[/url] written dissertation

DurekFak (not verified)

Amazing many of beneficial advice! writers essay [url=https://scoringessays.com/]thesis writing help[/url] written dissertation

DurekFak (not verified)

Amazing many of beneficial advice! writers essay [url=https://scoringessays.com/]thesis writing help[/url] written dissertation

ArthurRox (not verified)

logatomtrade отзывы https://rusvest.ru/news/logatomtrade-otzyvy-naskolko-nadezhnyj-broker/

KennathTarge (not verified)

Good content. Kudos.
college board essay custom paper admission essay writing service

BryaneTwimb (not verified)

Thanks. Terrific stuff.
how to write an intro for an essay custom writing help writing phd proposal
write a personal essay b48mjl c176574

KennathTarge (not verified)

Really tons of helpful facts.
college admission essay prompts writing service hire ghostwriter

BryaneTwimb (not verified)

Many thanks. Very good stuff.
why is it important to go to college essay paper helper online letter writing service
write narrative essay w94vcu 10_cfa4

DurekFak (not verified)

You reported this effectively. personal statement essays for college [url=https://bestessayseducationusa.com/]homework hotline[/url] graduate dissertation

BryaneTwimb (not verified)

Awesome write ups, With thanks!
write a personal essay help in thesis writing websites for essays
college essay writing company p25wbr 896d70c

DurekFak (not verified)

You actually reported it terrifically. how write a good essay [url=https://topswritingservices.com/]undergraduate coursework[/url] coursework help

Shirleybic (not verified)

https://mega-box.com.ua
https://mega-box.com.ua/sitemap.xml
Каждые 4 часа можно бесплатно получить 15 000

KennathTarge (not verified)

Good information. Cheers!
writing compare and contrast essays write papers writing help online

BryaneTwimb (not verified)

Very good write ups. With thanks.
how to write an essay about a quote courses work cheap custom writings
freedom writers essay introduction q918nk 674ec17

Robertbat (not verified)
Vidsigep (not verified)

[url=https://extraint.ru/catalog/servirovochnyj_stol_flat_flix]Потолочный светильник Project X PF20-8[/url] или [url=https://extraint.ru/brand/akssesuary1/mike_shilov]Кресло Slant[/url]

https://extraint.ru/catalog/gr_categ/akssesuary/afd_home/

Vidsigep (not verified)

[url=https://extraint.ru/catalog/servirovochnyj_stol_flat_flix]Потолочный светильник Project X PF20-8[/url] или [url=https://extraint.ru/brand/akssesuary1/mike_shilov]Кресло Slant[/url]

https://extraint.ru/catalog/gr_categ/akssesuary/afd_home/

RicardoAwamb (not verified)

семена газона https://factorygreen.ru

DurekFak (not verified)

You made your point! college pressures essay [url=https://topessayssites.com/]writing services[/url] dissertation structure

Dwayneedine (not verified)

вакансии эскорт краснодар

https://upde.ru/en/domain/rabotavescortsochi.com

BryaneTwimb (not verified)

Perfectly expressed really! !
what to write in an essay about yourself xyz homework doctoral dissertation writing service
how to write an essay intro g60eoi 747f03b

BryaneTwimb (not verified)

Perfectly expressed really! !
what to write in an essay about yourself xyz homework doctoral dissertation writing service
how to write an essay intro g60eoi 747f03b

Dwayneedine (not verified)

эскорт краснодар вакансии

https://mijnwebsitescan.nl/domain/rabotavescortsochi.com

KennathTarge (not verified)

Nicely put. Kudos!
how to write an about me essay paper writer affordable writing services

KennathTarge (not verified)

Nicely put. Kudos!
how to write an about me essay paper writer affordable writing services

Robertbat (not verified)

Add new comment