Skip to content

Commit

Permalink
Merge pull request #3318 from craftcms/bugfix/3253-line-item-snapshot…
Browse files Browse the repository at this point in the history
…-unpacking

Consistent decoding of line item snapshot
  • Loading branch information
lukeholder authored Oct 31, 2023
2 parents 4496ad1 + f20b503 commit 1045fed
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- Product GQL queries now support `promotable`, `availableForPurchase`, `freeShipping`, `defaultSku`, `defaultPrice`, `defaultVariant`, `defaultHeight`, `defaultLength`, `defaultWidth`, and `defaultWeight` params. ([#3307](https://github.com/craftcms/commerce/pull/3307))
- Fixed a PHP error that occurred when viewing trashed orders. ([#3308](https://github.com/craftcms/commerce/issues/3308))
- Fixed an incorrect validation error that occurred when saving a Shipping Zone. ([#3317](https://github.com/craftcms/commerce/issues/3317))
- Fixed a bug where a line item’s snapshot wasn’t being decoded. ([#3253](https://github.com/craftcms/commerce/issues/3253))

## 4.3.1 - 2023-10-18

Expand Down
1 change: 1 addition & 0 deletions src/models/LineItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ class LineItem extends Model

/**
* @var mixed Snapshot
* @TODO add get and set methods in 5.0.0. Strict typing will be enforced.
*/
public mixed $snapshot = null;

Expand Down
5 changes: 5 additions & 0 deletions src/services/LineItems.php
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,11 @@ public function getLineItemById(int $id): ?LineItem
->where(['id' => $id])
->one();

if ($result) {
// Unpack the snapshot
$result['snapshot'] = Json::decodeIfJson($result['snapshot']);
}

return $result ? new LineItem($result) : null;
}

Expand Down
12 changes: 12 additions & 0 deletions tests/unit/services/LineItemsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,18 @@ public function testGetLineItemById(): void
self::assertEquals($lineItems[0]->qty, $lineItem->qty);
}

public function testSnapshotUnpacking(): void
{
/** @var Order $order */
$order = $this->fixtureData->getElement('completed-new');
$lineItemById = $this->service->getLineItemById($order->getLineItems()[0]->id);
$lineItemFromAll = collect($this->service->getAllLineItemsByOrderId($order->id))->firstWhere('id', $lineItemById->id);

self::assertIsArray($lineItemById->snapshot);
self::assertIsArray($lineItemFromAll->snapshot);
self::assertEquals($lineItemById->snapshot, $lineItemFromAll->snapshot);
}

public function testCreateLineItem(): void
{
/** @var Order $order */
Expand Down

0 comments on commit 1045fed

Please sign in to comment.