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

Anonymous (not verified)

I have a hard time describing my thoughts on content, but I really felt I should here. Your article is really great. I like the way you wrote this information. running assessment Glasgow

Anonymous (not verified)

I concede, I have not been on this page in quite a while... on the other hand it was another happiness to see It is such an imperative subject and disregarded by such a large number of, even experts. experts. I thank you to help making individuals more mindful of conceivable issues. http://www.uni-protokolle.de/foren/viewt/320971,0.html

Anonymous (not verified)

You know your activities emerge of the group. There is something exceptional about them. It appears to me every one of them are truly splendid! Exclusive Escorts

Robinjack (not verified)

I think other website owners should take this site as an example , very clean and fantastic user friendly layout. cheap bone conduction headphones review

AsharSeo (not verified)

Thank you for some other informative blog. Where else could I get that type of information written in such an ideal means? I have a mission that I’m just now working on, and I have been at the look out for such information. Dirty Jokes

AsharSeo (not verified)

I am impressed with this internet site , very I am a big fan .

TV-Show Audition
https://auditiondateandplace.com/category/tv-show-audition/

Anonymous (not verified)

While you’re any of the lucky enough choices, it comes evidently, while capture the fancy of the particular coveted by ly folks other useful you you meet may possibly well have hard times this particular problem. pre owned awnings Party Games

Anonymous (not verified)

Thanks for sharing the post.. parents are worlds best person in each lives of individual..they need or must succeed to sustain needs of the family. Borbet felgen

Niharika Singh (not verified)

This content is simply exciting and creative. I have been deciding on an institutional move and this has helped me with one aspect. independent call girls hyderabad

Anonymous (not verified)

This article was written by a real thinking writer. I agree many of the with the solid points made by the writer. I’ll be back. apofraxeis

Anonymous (not verified)

This article gives the light in which we can watch the truth. This is extremely pleasant one and gives indepth data. A debt of gratitude is in order for this pleasant article. Best Romanian Escorts

Anonymous (not verified)

You have a good point here!I totally agree with what you have said!!Thanks for sharing your views...hope more people will read this article!!! http://akne-lecba.info/

Robinjack (not verified)

I prefer this kind of website greatly, It’s any rattling good berth to read and get details. ride 3 tracks

Anonymous (not verified)

This is really the sort of data I have been attempting to discover. Much thanks to you for composing this data. voyance par telephone 20

Anonymous (not verified)

Great artcile, yet it would be better if in future you can share more about this subject. Continue posting. http://danske-artikler.pen.io/

mh (not verified)

Awesome information! I as of late ran over your online journal and have been perusing along. I thought I would leave my first remark. I don't recognize what to say with the exception of that I have. voyance gratuite

Anonymous (not verified)

Amazing, this is truly fascinating perusing. I am happy I discovered this and got the opportunity to peruse it. Awesome employment on this substance. I like it. long island web design company

Anonymous (not verified)

Τα τελευταία χρόνια επεκτείναμε σταδιακά τις δραστηριότητες μας εκτός τις Αποφράξεις αποχετευτικών αγωγών και εκκενώσεις βόθρων, τώρα αναλαμβάνουμε υπεύθυνα απολυμάνσεις κτηρίων και έκδοση κοινοχρήστων, θέλοντας να προσφέρουμε ολοκληρωμένες υπηρεσίες στις κτηριακές ανάγκες αποφραξεις

Anonymous (not verified)

I'm happy to see the immense subtle element here!. Accounting ebooks

dsd (not verified)

Yes i am completely concurred with this article and i simply need say this article is extremely decent and exceptionally enlightening article.I will make a point to be perusing your blog more. You made a decent point yet I can't resist the urge to ponder, shouldn't something be said about the other side? !!!!!!THANKS!!!!!! voyance téléphone

Anonymous (not verified)

This is a really decent site post. Not very numerous individuals would really, the way you simply did. I am truly inspired that there is such a great amount of data about this subject have been revealed and you've put forth a valiant effort, with so much class. In the event that needed to know more about green smoke surveys, than by all methods come in and check our stuff. Leeds Beer Festival

Anonymous (not verified)

hello there was simply checking whether you minded a remark. i like your site and the thme you picked is super. I will be back. voyance paris

amelia (not verified)

I've perused some well done here. Certainly worth bookmarking for returning to. I astound the amount of exertion you put to make such an extraordinary useful site. Anunturi escorte

amelia (not verified)

I take in some new stuff from it as well, a debt of gratitude is in order for sharing your data. CBTF

Anonymous (not verified)

This is such an incredible asset, to the point that you are giving and you give it away for nothing. I cherish seeing blog that comprehend the quality. Im happy to have discovered this post as its such an intriguing one! I am dependably watchful for quality posts and articles so i assume im fortunate to have discovered this! I trust you will be including more later on... شركه نقل عفش بجده

SophiaMiller (not verified)

hello,
this is Sophia Miller.
this is a nice post, thanks for sharing.
Get the best perdisco assignment help in Australia.

Add new comment