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

juliadave (not verified)

I merely hit upon this web site and desired to enunciate that I've definitely delighted in reckoning your blog articles or blog posts. Rest assured I'll subsist https://www.assignmentcloud.co.uk/write-my-assignment pledging to your RSS and I wish you write-up after much more shortly

dave (not verified)

thanks for sharing it to each one of us. Looking forward to more information and you have continually intrigued me. Thank you for creating an https://www.uhjackets.com/blade-runner-2-ryan-gosling-leather-fur-coat
article that has amazing substance and is well formed and even I am revived by your composed style as well.

Barrett (not verified)

Drupal 8 has made so many things easier for us to do, man. As someone who works at the essay service uk site, it's certainly the best software to use right now. If you have any problems with that statement, I'd like to know why.

wondergirl (not verified)

I starting late impelled a site on PHP instructional activities yet I starting at now had a YouTube channel covering relative stuff. So I was scanning for a way to deal with introduce a video at the base end of my substance instructional exercises. help with assignment Anyway, an obligation of appreciation is all together to post this!

Robinjack (not verified)

Oh my goodness! an excellent post dude. Thanks a ton Nevertheless We’re experiencing trouble with ur rss . Do not know why Unable to register for it. Will there be everyone getting identical rss difficulty? Anybody who knows kindly respond. Thnkx affordable insurance in tn

Thick Filter Papers (not verified)

Wow what a Great Information about World Day its exceptionally pleasant educational post. a debt of gratitude is in order for the post. Thick Filter Papers

Rooibos Tea (not verified)

Well, this got me thinking what other workouts are good for those of us who find ourselves on the road or have limited equipment options. Rooibos Tea

Raisins (not verified)

I feel really happy to have seen your webpage and look forward to so many more entertaining times reading here. Thanks once more for all the details. Raisins

Kiwano (not verified)

I wanted to leave a little comment to support you and wish you a good continuation. Wishing you the best of luck for all your blogging efforts. Kiwano

Head Lice (not verified)

I have read your excellent post. This is a great job. I have enjoyed reading your post first time. I want to say thanks for this post. Thank you... Head Lice

Burn Fat (not verified)

wow, great, I was wondering how to cure acne naturally. and found your site by google, learned a lot, now i’m a bit clear. I’ve bookmark your site and also add rss. keep us updated. Burn Fat

Detox Water (not verified)

With so many books and articles coming up to give gateway to make-money-online field and confusing reader even more on the actual way of earning money, Detox Water

Copper (not verified)

It was a very good post indeed. I thoroughly enjoyed reading it in my lunch time. Will surely come and visit this blog more often. Thanks for sharing. Copper

Lycopene (not verified)

This post is good enough to make somebody understand this amazing thing, and I’m sure everyone will appreciate this interesting things. Lycopene

Anonymous (not verified)

Remember to create backlinks with websites or blogs which have high rankings. This is because of the fact that high ranking websites or blogs will have more visitors than those which fall back on SERPs (Search Engine Result Pages). what is cbd

Anonymous (not verified)

discover sluurpy and start browsing the original restaurant menu, select the best restaurants around you with the new "sluurpometer" algorithm. sluurpy.it

robinjack (not verified)

There a few intriguing points in time in this post but I don’t know if I see these people center to heart. You can find some validity but Let me take hold opinion until I investigate it further. Great write-up , thanks and now we want far more! Put into FeedBurner likewise voyance couple gratuite

Anonymous (not verified)

If it's not too much trouble share more like that. Costumes rock 60s costumes

Anonymous (not verified)

Thankyou for this fantastic post, I am glad I noticed this internet site on yahoo. Free Online Seo Tools

Anonymous (not verified)

Naturally I like your web-site, however you need to check the spelling on several of your posts. Many of them are rife with spelling problems and I find it very silly to inform you. On the other hand I will certainly come again again! car service miami fl

amelia (not verified)

I high value this post. It's elusive the great from the awful in some cases, yet I think you've nailed it! would you brain redesigning your website with more data? digital forensics corporation

chalsea (not verified)

Simply respecting your work and thinking about how you dealt with this website so well. It's remarkable to the point that I can't stand to not experience this important data at whatever point I surf the web! kush canada

Add new comment