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)

Incredible post I might want to thank you for the endeavors you have made in composing this fascinating and learned article. Leeds Festival tickets

ali (not verified)

I'm truly enjoying the design and layout of your site. It's a very easy on the eyes which makes it much more pleasant for me to come here and visit more often. Did you hire out a designer to create your theme? Superb work! Good Friday Quotes Saying

ali (not verified)

I'm truly enjoying the design and layout of your site. It's a very easy on the eyes which makes it much more pleasant for me to come here and visit more often. Did you hire out a designer to create your theme? Superb work! Happy Mother’s Day Messages For A Friend

ali (not verified)

I'm truly enjoying the design and layout of your site. It's a very easy on the eyes which makes it much more pleasant for me to come here and visit more often. Did you hire out a designer to create your theme? Superb work! Memorial Day Decorations

houston website... (not verified)

we are in fact thankful in your weblog declare. you will locate a lot of approaches after journeying your screen. i was exactly attempting to find. thanks for such say and make smile shop it taking region. incredible take steps.
web design

Happy Ramadan M... (not verified)
Mothers Day Cards (not verified)

Very Attractive Article and very awesome.
It really helped me a ton. Please to be continued and posts like these.
Mothers Day Is coming I will prefer you to check out my collection and wish others.
Mothers Day Cards
Regards,
Amelia

Happy Valentines Day (not verified)
Anonymous (not verified)

Thanks for every other informative site. The place else may just I get that kind of information written in such an ideal means? I have a venture that I’m just now operating on, and I have been on the look out for such information. https://bornholmsefterskole.contently.com/

Anonymous (not verified)

Incredible articles and extraordinary design. Your blog entry merits the greater part of the positive input it's been getting. laser tattoo removal glasgow

Anonymous (not verified)

Great Information sharing .. I am very happy to read this article .. thanks for giving us go through info.Fantastic nice. I appreciate this post. http://sniksnak.qsite.dk/

c (not verified)

Precisely, you're exceptionally sort of us about remark!. https://dk-newz.kinja.com

Anonymous (not verified)

I recently found many useful information in your website especially this blog page. Among the lots of comments on your articles. Thanks for sharing. presentation skills course

Anonymous (not verified)

I truly like your thoughts on the issue. I now have an unmistakable thought on what this matter is about.. Men Counselling Singapore

Anonymous (not verified)

This is the type of information I’ve long been trying to find. Thank you for writing this information. best pre wedding photographer singapore

Anonymous (not verified)

Much thanks for this extraordinary post. gummy bear singapore

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! bedroom furniture vancouver

Anonymous (not verified)

Interesting and amazing how your post is! It Is Useful and helpful for me That I like it very much, and I am looking forward to Hearing from your next.. https://www.instapaper.com/p/anbefalinger

Anonymous (not verified)

A debt of gratitude is in order for the post and incredible tips..even I likewise feel that diligent work is the most critical part of getting achievement.. http://anbefalinger.pen.io/

chalsea (not verified)

Extraordinary substance material and awesome design. Your site merits the majority of the positive input it's been getting. Pet Supplies

Anonymous (not verified)

I can't read articles online all the time, however I'm happy I did today. This is extremely elegantly composed and your focuses are very much communicated. Kindly, absolutely never quit composing. bonne voyante gratuite

Anonymous (not verified)

Somebody Sometimes with visits your web journal consistently and prescribed it I would say to peruse also. The method for composing is phenomenal furthermore the substance is first rate. A debt of gratitude is in order for that understanding you give the perusers! Fix administration for Kenmore Refrigerator in Orange County

Anonymous (not verified)

Your work is great and I value you and bouncing for some more educational posts. Much obliged to you for sharing incredible data to us. Köp Nikken

Anonymous (not verified)

That is the great attitude, in any case is simply not make each sence at all proclaiming about that mather. For all intents and purposes any strategy an abundance of thanks notwithstanding i had try to advance your own particular article into delicius in any case it is evidently a situation utilizing your data destinations would you be able to please recheck the thought. much obliged yet again. 해외서버

Anonymous (not verified)

This is an awesome motivating article.I am practically satisfied with your great work.You put truly exceptionally accommodating data. Keep it up. Continue blogging. Hoping to perusing your next post. Sweet Cart

Anonymous (not verified)

Extraordinary overview, I'm certain you're getting an awesome reaction. 성기확대

Add new comment