<pre><code>$order = \craft\commerce\elements\Order::find()-&gt;id($id)-&gt;one();

if($order)
{
  foreach ($order-&gt;getLineItems() as $item) {
   // $item-&gt;description
   // $item-&gt;qty
  }

  $billingAddress = $order-&gt;getBillingAddress();
  $shippingAddress = $order-&gt;getShippingAddress();
}
</code></pre>

CC BY-SA 4.0
2018-08-16T15:21:39.293Z
2018-08-17T10:14:47.393Z
2018-08-17T10:14:47.393Z
Luke Holder
<p>I want to write a plugin which writes all the order data into a CSV file.
Unfortunately the documentation for Commerce 2 isn't quite complete yet so I have difficulties to find out how to do the following with a plugin (so I need to use PHP and not Twig).</p>

<ol>
<li>Get an order by ID.</li>
<li>Get ordered items.</li>
<li>Get the addresses for the order.</li>
</ol>

<p>Every help appreciated.</p>

<plugin-development><plugin-craftcommerce2>
Craft Commerce 2 - get order data for plugin
# Craft Commerce 2 - get order data for plugin

(Asked by: username_0 on 2018-08-16)

I want to write a plugin which writes all the order data into a CSV file.
Unfortunately the documentation for Commerce 2 isn't quite complete yet so I have difficulties to find out how to do the following with a plugin (so I need to use PHP and not Twig).

1. Get an order by ID.
2. Get ordered items.
3. Get the addresses for the order.

Every help appreciated.

**Tags:** plugin-development, plugin-craftcommerce2


---

## 1 answers

### Answer by username_1 on 2018-08-16. Score: 4 | Accepted answer

$order = \craft\commerce\elements\Order::find()->id($id)->one();

if($order)
{
  foreach ($order->getLineItems() as $item) {
   // $item->description
   // $item->qty
  }

  $billingAddress = $order->getBillingAddress();
  $shippingAddress = $order->getShippingAddress();
}


---

