From 2d0a70ba634b1184eb7b0e0ca40e209e3485196c Mon Sep 17 00:00:00 2001 From: Nathaniel Hammond Date: Mon, 30 Oct 2023 09:21:27 +0000 Subject: [PATCH 1/2] Fixed #3253 consistent decoding of line item snapshot --- CHANGELOG.md | 1 + src/services/LineItems.php | 5 +++++ tests/unit/services/LineItemsTest.php | 12 ++++++++++++ 3 files changed, 18 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 59bc358cba..1b26d83df6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ - Fixed a PHP error that occurred when sending an email with a missing PDF filename format. ([#3309](https://github.com/craftcms/commerce/issues/3309)) - 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 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 diff --git a/src/services/LineItems.php b/src/services/LineItems.php index 3522690a52..e0f8c78262 100644 --- a/src/services/LineItems.php +++ b/src/services/LineItems.php @@ -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; } diff --git a/tests/unit/services/LineItemsTest.php b/tests/unit/services/LineItemsTest.php index 89fcf78273..6e41849433 100644 --- a/tests/unit/services/LineItemsTest.php +++ b/tests/unit/services/LineItemsTest.php @@ -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 */ From f20b5034f0ab933e2ae9300273a450d085b92377 Mon Sep 17 00:00:00 2001 From: Nathaniel Hammond Date: Tue, 31 Oct 2023 08:10:08 +0000 Subject: [PATCH 2/2] Add todo --- src/models/LineItem.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/models/LineItem.php b/src/models/LineItem.php index fd7581abad..ce88684423 100644 --- a/src/models/LineItem.php +++ b/src/models/LineItem.php @@ -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;