<p>First of all, it's important to know what version(s) of Craft and Commerce you're referring to. The code used in this answer applies to Craft 3, with Commerce 2-beta. Code for Craft 2 and Commerce 1 would look different, but the logic is similar.</p>

<hr>

<p>I would start by looking at Commerce's codebase on Github (or in your plugins folder, if it's Craft 2), and more specifically the <a href="https://github.com/craftcms/commerce/blob/develop/src/controllers/OrdersController.php" rel="nofollow noreferrer">OrdersController</a>, and even more specifically the <code>actionSaveOrder</code> and <code>_setOrderFromPost</code> methods.</p>

<p>It looks to me like you need to do two things:</p>

<h3>Get an Order model:</h3>

<pre><code>// Find an existing order by ID
$order = Commerce::getInstance()-&gt;orders-&gt;getOrderById($orderId);

// ... Or create a new one
$order = new Order; # Don't forget to place use craft\commerce\elements\Order above your class
</code></pre>

<h3>Populate the model (untested)</h3>

<p>If you have an order model, you have to populate all the fields that matter. Status, addresses, I think you'd best start on an individual order page in the admin to see what fields are needed.</p>

<pre><code>$order-&gt;isCompleted = true;
$order-&gt;billingAddressId = 4;
$order-&gt;gatewayId = 7;
... etc (includes creating and adding line items for your order)
</code></pre>

<h3>Save the order and show why it doesn't work magically the first time around ;)</h3>

<p>This is where the fun starts, which might depend on your definition of fun.</p>

<pre><code>if (!Craft::$app-&gt;getElements()-&gt;saveElement($order)) {
    var_dump($order-&gt;errors);
}
</code></pre>

CC BY-SA 3.0
2018-04-12T13:52:21.537Z
Thanks for your reply. I am quite new to craft. So if you can share more detail then it will help me.

Like,
- Adding line items
- Assigning payment method
- Assigning address
user8244
CC BY-SA 3.0
2018-04-12T13:56:16.450Z
It really depends on your use case and needs. I think you need to familiarize yourself with the underlying code (so, take a look at how Commerce does it) before trying anything as potentially complex as programmatically creating orders. This really is all I can provide you with, time wise, without actually building it for you ;)
Paul
CC BY-SA 3.0
2018-04-12T13:18:03.037Z
2018-04-12T13:18:03.037Z
Paul
<p>I want to create order via code in Craft Commerce. How can I achieve that?</p>

<p>Thanks in advance.</p>

<plugin-craftcommerce>
Create order programmatically in Craft Commerce
# Create order programmatically in Craft Commerce

(Asked by: username_0 on 2018-04-12)

I want to create order via code in Craft Commerce. How can I achieve that?

Thanks in advance.

**Tags:** plugin-craftcommerce

---

## 1 answers

### Answer by username_1 on 2018-04-12. Score: 4

First of all, it's important to know what version(s) of Craft and Commerce you're referring to. The code used in this answer applies to Craft 3, with Commerce 2-beta. Code for Craft 2 and Commerce 1 would look different, but the logic is similar.

I would start by looking at Commerce's codebase on Github (or in your plugins folder, if it's Craft 2), and more specifically the OrdersController, and even more specifically the actionSaveOrder and _setOrderFromPost methods.
It looks to me like you need to do two things:
Get an Order model:
// Find an existing order by ID
$order = Commerce::getInstance()->orders->getOrderById($orderId);

// ... Or create a new one
$order = new Order; # Don't forget to place use craft\commerce\elements\Order above your class

Populate the model (untested)
If you have an order model, you have to populate all the fields that matter. Status, addresses, I think you'd best start on an individual order page in the admin to see what fields are needed.
$order->isCompleted = true;
$order->billingAddressId = 4;
$order->gatewayId = 7;
... etc (includes creating and adding line items for your order)

Save the order and show why it doesn't work magically the first time around ;)
This is where the fun starts, which might depend on your definition of fun.
if (!Craft::$app->getElements()->saveElement($order)) {
    var_dump($order->errors);
}

> **Comments:**
> **username_0**: Thanks for your reply. I am quite new to craft. So if you can share more detail then it will help me.  Like, - Adding line items - Assigning payment method - Assigning address  
> **username_1**: It really depends on your use case and needs. I think you need to familiarize yourself with the underlying code (so, take a look at how Commerce does it) before trying anything as potentially complex as programmatically creating orders. This really is all I can provide you with, time wise, without actually building it for you ;)  

---

