Skip to content

Commit

Permalink
Fix error with parsing order completed and paid date from timestamp (#19
Browse files Browse the repository at this point in the history
)
  • Loading branch information
Dartui committed Oct 30, 2020
1 parent 93ef94b commit 0bd74c6
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 4 deletions.
36 changes: 34 additions & 2 deletions src/Model/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,23 @@ protected function getDateCompletedAttribute(): ?Carbon
{
$value = $this->getMeta('_date_completed');

return $value ? Carbon::parse($value) : null;
if ($value !== null) {
return Carbon::createFromTimestamp($value);
}

/**
* WooCommerce in version 2.6.x has stored completed date in
* "_completed_date" meta field in MySQL datetime format.
*/
$value = $this->getMeta('_completed_date');

if ($value !== null) {
$datetime = Carbon::createFromFormat('Y-m-d H:i:s', $value);

return $datetime !== false ? $datetime : null;
}

return null;
}

/**
Expand All @@ -157,7 +173,23 @@ public function getDatePaidAttribute(): ?Carbon
{
$value = $this->getMeta('_date_paid');

return $value ? Carbon::parse($value) : null;
if ($value !== null) {
return Carbon::createFromTimestamp($value);
}

/**
* WooCommerce in version 2.6.x has stored paid date in "_paid_date"
* meta field in MySQL datetime format.
*/
$value = $this->getMeta('_paid_date');

if ($value !== null) {
$datetime = Carbon::createFromFormat('Y-m-d H:i:s', $value);

return $datetime !== false ? $datetime : null;
}

return null;
}

/**
Expand Down
17 changes: 15 additions & 2 deletions tests/Unit/Model/OrderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function testStatusProperty(): void
public function testDateCompletedProperty(): void
{
$order = $this->createOrder();
$order->createMeta('_date_completed', '2020-01-01 00:00:00');
$order->createMeta('_date_completed', 1577836800); // '2020-01-01 00:00:00'

$this->assertInstanceOf(Carbon::class, $order->date_completed);
$this->assertSame('2020-01-01 00:00:00', $order->date_completed->format('Y-m-d H:i:s')); // @phpstan-ignore-line
Expand All @@ -75,7 +75,20 @@ public function testDateCompletedProperty(): void
public function testDatePaidProperty(): void
{
$order = $this->createOrder();
$order->createMeta('_date_paid', '2020-01-01 01:00:00');
$order->createMeta('_date_paid', 1577840400); // '2020-01-01 01:00:00'

$this->assertInstanceOf(Carbon::class, $order->date_paid);
$this->assertSame('2020-01-01 01:00:00', $order->date_paid->format('Y-m-d H:i:s')); // @phpstan-ignore-line
}

public function testDeprecatedDateFormats(): void
{
$order = $this->createOrder();
$order->createMeta('_completed_date', '2020-01-01 00:00:00');
$order->createMeta('_paid_date', '2020-01-01 01:00:00');

$this->assertInstanceOf(Carbon::class, $order->date_completed);
$this->assertSame('2020-01-01 00:00:00', $order->date_completed->format('Y-m-d H:i:s')); // @phpstan-ignore-line

$this->assertInstanceOf(Carbon::class, $order->date_paid);
$this->assertSame('2020-01-01 01:00:00', $order->date_paid->format('Y-m-d H:i:s')); // @phpstan-ignore-line
Expand Down

0 comments on commit 0bd74c6

Please sign in to comment.