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

Josh Matthew (not verified)

Sports which are of huge event like football, cricket, NBA Soccer, Base Ball, UFC and many more can bring together people with different opinions together. we like to watch sports live

Anonymous (not verified)

I haven't any word to welcome this post.....Really i am inspired from this post....the individual who make this post it was an incredible human..thanks for imparted this to us. Reiki

chalsea (not verified)

I know your aptitude on this. I must say we ought to have an online discourse on this. Composing just remarks will close the exchange straight away! Furthermore, will confine the advantages from this data. water heater installation Fuqua varina

Anonymous (not verified)

The data you have posted is extremely helpful. The destinations you have alluded was great. A debt of gratitude is in order for sharing.. click to buy

Anonymous (not verified)

I truly like you're composing style, awesome data, thankyou for posting. legitimate online pharmacies

Anonymous (not verified)

Just try to specify ones substance can be as mind blowing. This clarity with your post is radiant and that i may believe you're a master for this issue. High caliber alongside your agree grant me to grab your present provide for keep changed by utilizing drawing nearer blog entry. You rock several alongside you ought to go on the pleasurable take care of business. Bellevue locksmith

Anonymous (not verified)

I truly like you're composing style, awesome data, thankyou for posting. Berita Saham

Anonymous (not verified)

808 Pictures. Professional Maui photographers specializing in family portrait photography, wedding photography, engagement photography, couples photography, maternity photography, fashion photography. Award-winning photographers on Maui with the highest in five-star Google reviews and the best prices for photography packages in Hawaii. Maui photographers

Anonymous (not verified)

This is an incredible rousing article.I am basically satisfied with your great work.You put truly exceptionally accommodating data... best cinnamon for health

Anonymous (not verified)

That is the excellent mindset, nonetheless is just not help to make every sence whatsoever preaching about that mather. Virtually any method many thanks in addition to i had endeavor to promote your own article in to delicius nevertheless it is apparently a dilemma using your information sites can you please recheck the idea. thanks once more. click here

seo (not verified)

I haven’t checked in here for some time since I thought it was getting boring, but the last handful of posts are really good quality so I guess I will add you back to my everyday bloglist. You deserve it my friend. logan paul instagram stats

Anonymous (not verified)

Superb article. Extremely fascinating to peruse. I truly love to peruse such a pleasant article. Much obliged! continue shaking. avenue south residence showflat

Anonymous (not verified)

I truly like you're composing style, awesome data, thankyou for posting. http://www.pingmyurl.com/site-stats/show.php?url=https%3A%2F%2Fodsherredsefterskole.dk

Anonymous (not verified)

Wow what a Great Information about World Day its exceptionally pleasant instructive post. a debt of gratitude is in order for the post. شركة كشف تسربات المياه بالدمام

Anonymous (not verified)

A debt of gratitude is in order for setting aside an ideal opportunity to examine this, I feel emphatically about it and affection adapting more on this theme. On the off chance that conceivable, as you pick up skill, would you psyche overhauling your online journal with more data? It is to a great degree supportive for me. Hill Country Breaking News

amelia (not verified)

Incredible Information sharing .. I am exceptionally glad to peruse this article .. a debt of gratitude is in order for giving us experience info.Fantastic pleasant. I welcome this post. https://www.rinkratron.com/

Anonymous (not verified)

Im no master, however I trust you simply made a brilliant point. You absolutely completely comprehend what youre talking about, and I can genuinely get behind that. http://www.banyuleandnillumbikweekly.com.au/

Anonymous (not verified)

Wow what a Great Information about World Day its exceptionally pleasant instructive post. a debt of gratitude is in order for the post. https://kinja.com/dk-newz/posts

Anonymous (not verified)

Much obliged for setting aside an ideal opportunity to examine this, I feel firmly about it and affection adapting more on this subject. view

Anonymous (not verified)

808 Pictures. Maui weather, a live weather report from real-time webcams from Maui on the true Maui weather forecast. See Maui weather live from 808 Pictures. Our photography services rely on the weather and we make sure our clients are in safe hands when it comes to having their family portrait, wedding or engagement photo-shoot on the beaches with 808 Pictures. See live Maui weather reports and plan your photo-shoot for morning or sunset. Weather on Maui

Anonymous (not verified)

Hey what a splendid post I have run over and trust me I have been hunting out down this comparative sort of post for recent week and barely ran over this. Much thanks and will search for more postings from you. 먹튀검증

Anonymous (not verified)

good post, i surely enjoy this website, continue on it. 메이저사이트

Anonymous (not verified)

Many of the Koreans are enjoying Sportstototo(스포츠토토) betting. The best sites among the many Totosites(토토사이트) are called major sites.The sites that provide information on popular sports games are called Toto sites  스포츠토토(sportstoto)

Anonymous (not verified)

I truly appreciated perusing this post, huge fan. Keep doing awesome me when would you be able to distribute more articles or where would I be able to peruse more on the subject? https://techarim.com/

Anonymous (not verified)

Your website is really cool and this is a great inspiring article. Thank you so much. Olife come acquistare

Anonymous (not verified)

i was simply skimming along and happened upon your web journal. simply needed to say great web journal and this article truly helped me. Corporate Social Responsibility

Anonymous (not verified)

You are very brave focuses in this article. I would have never thought to be any of these on the off chance that I didn't go over this. Much appreciated!. CBD Gummies

Anonymous (not verified)

I was perusing your article and thought about whether you had considered making a digital book on this subject. You're composing would offer it quick. You have a great deal of composing ability. epoxy concrete floor coating

Anonymous (not verified)

I needed to thank you for this phenomenal read!! I unquestionably adored each and every piece of it. I have you bookmarked your site to look at the new stuff you post. Stiiizy

Anonymous (not verified)

808 Pictures. We have specialized in Maui family photography since 2004. Our Maui family photographers have hundreds of Google five star reviews and our award-winning friendly team of professional Maui photographers has been published in magazines around the world. We know what it takes to get the family excited about having their family portrait taken on Maui on the most beautiful beaches in the world. Maui portrait photography is now affordable and fun with 808 Pictures. Maui family photographers

Anonymous (not verified)

This is such an awesome asset, to the point that you are giving and you give it away for nothing. I adore seeing blog that comprehend the benefit of giving a quality asset to free. sky everton showflat

Anonymous (not verified)

Wow what a Great Information about World Day its exceptionally pleasant instructive post. a debt of gratitude is in order for the post. midtown bay price

Anonymous (not verified)

Much thanks for this incredible post. Situs Judi Casino

Anonymous (not verified)

Hello, Neat post. There’s a problem with your website in internet explorer, would check this… IE nonetheless is the marketplace leader and a huge portion of people will omit your great writing due to this problem. 我要借錢

Anonymous (not verified)

Good website! I truly love how it is easy on my eyes it is. I am wondering how I might be notified whenever a new post has been made. I have subscribed to your RSS which may do the trick? Have a great day! Cara deposit dan withdraw sbypoker

Anonymous (not verified)

Marvelous web journal. I appreciated perusing your articles. This is really an incredible read for me. I have bookmarked it and I am anticipating perusing new articles. Keep doing awesome! watch free movies online streaming movgotv

Anonymous (not verified)

Is it alright to post some portion of this on my site essentially present a hyperlink on this site page? 여유증수술

Anonymous (not verified)

This is truly a decent and instructive, containing all data furthermore greatly affects the new innovation. A debt of gratitude is in order for sharing it, voyance en ligne

Add new comment