Skip to content

Instantly share code, notes, and snippets.

@joshmiller83
Created September 28, 2016 15:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshmiller83/2a091a348b0952db20663ce894677d6b to your computer and use it in GitHub Desktop.
Save joshmiller83/2a091a348b0952db20663ce894677d6b to your computer and use it in GitHub Desktop.
Create an order programmatically for Drupal Commerce 2.x
<?php
$order_type = "default";
$store_id = 1;
$uid = 0;
$entity_type_manager = \Drupal::service('entity_type.manager');
$order_storage = $entity_type_manager->getStorage('commerce_order');
$new_order = $orderStorage->create([
'type' => $order_type,
'store_id' => $store_id,
'uid' => $uid,
'cart' => FALSE,
]);
$new_order->save();
@Tamarpe
Copy link

Tamarpe commented Oct 22, 2019

Thanks!
A small typo - line 8 should be:
$new_order = $order_storage->create([

@renierjohn
Copy link

doesnt work it says
TypeError: Argument 1 passed to Drupal\commerce_tax\TaxRatePercentage::calculateTaxAmount() must be an instance of Drupal\commerce_price\Price, null given, called in C:\Users\ren\Sites\devdesktop\MemorphoseDev2020\web\modules\contrib\commerce\modules\tax\src\Plugin\Commerce\TaxType\LocalTaxTypeBase.php on line 131 in Drupal\commerce_tax\TaxRatePercentage->calculateTaxAmount() (line 131 of modules\contrib\commerce\modules\tax\src\TaxRatePercentage.php).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment