<p>The problem was occurring because the order was retrieved through the EVENT_AFTER_ORDER_PAID event. Therefore, the dateOrdered was still not processed (passed through mutations). </p>

<p>What we ended up doing is using <code>$event-&gt;sender</code> only to know what order we are looking for and then retrieve the order anew.</p>

<p>I hope this saves someone's life someday! I'll mark this question as solved in 2 days.</p>

CC BY-SA 4.0
2019-12-18T10:55:31.307Z
2019-12-18T10:55:31.307Z
ves
<p>I am using the Commerce 2 plugin and I am experiencing a very odd issue. Once an order is complete I want to use the dateOrdered property to create a csv which I send through a request endpoint. That worked fine.</p>

<p>BUT! Since recently (somewhere this month), on the first attempt to create and save the .csv, when calling <code>$order-&gt;dateOrdered-&gt;getTimestamp()</code> I get <code>dateOrdered</code> as a <code>string</code>.</p>

<p>On the second attempt to create and save the .csv I get <code>dateOrdered</code> as a <code>DateTime</code>.</p>

<p>The method which I am calling is the same so I think it has something to do wiht order statuses. </p>

<p>I tried to trace the problem in Commerce's <code>Order.php</code> class but I still have no clue why this occurs or what I am doing wrong.</p>

<p>Any help is more than welcome. Thank you in advance!</p>

<craft3><plugin-craftcommerce2>
$order->dateOrdered returns a string value the first time and DateTime after that. [Order got through event]
# $order->dateOrdered returns a string value the first time and DateTime after that. [Order got through event]

(Asked by: username_0 on 2019-12-18)

I am using the Commerce 2 plugin and I am experiencing a very odd issue. Once an order is complete I want to use the dateOrdered property to create a csv which I send through a request endpoint. That worked fine.

BUT! Since recently (somewhere this month), on the first attempt to create and save the .csv, when calling `$order->dateOrdered->getTimestamp()` I get `dateOrdered` as a `string`.

On the second attempt to create and save the .csv I get `dateOrdered` as a `DateTime`.

The method which I am calling is the same so I think it has something to do wiht order statuses.

I tried to trace the problem in Commerce's `Order.php` class but I still have no clue why this occurs or what I am doing wrong.

Any help is more than welcome. Thank you in advance!

**Tags:** craft3, plugin-craftcommerce2

---

## 1 answers

### Answer by username_0 on 2019-12-18. Score: 1 | Accepted answer

The problem was occurring because the order was retrieved through the EVENT\_AFTER\_ORDER\_PAID event. Therefore, the dateOrdered was still not processed (passed through mutations).

What we ended up doing is using `$event->sender` only to know what order we are looking for and then retrieve the order anew.

I hope this saves someone's life someday! I'll mark this question as solved in 2 days.


---

