From 02ed12ade345143e4c860d22a43f7bf7d9697428 Mon Sep 17 00:00:00 2001 From: Nathaniel Hammond Date: Fri, 13 Oct 2023 12:20:51 +0100 Subject: [PATCH] Fixed #3298 population of audit columns on Sales and Discounts after saving --- CHANGELOG.md | 1 + src/services/Discounts.php | 10 ++++++++++ src/services/Sales.php | 11 +++++++++++ 3 files changed, 22 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 276e6ac29a..43de4aa859 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ - Fixed a bug where the delete button would be shown for users that do not have permission to delete on the Product edit page. ([#3285](https://github.com/craftcms/commerce/issues/3285)) - Fixed a bug where deleted shipping categories were still available for selection. ([#3272](https://github.com/craftcms/commerce/issues/3272)) - Fixed an error that could occur when rendering a PDF. ([#2633](https://github.com/craftcms/commerce/issues/2633)) +- Fixed a bug where Sale’s and Discount’s date audit columns weren’t populated after saving. ([#3298](https://github.com/craftcms/commerce/issues/3298)) ## 4.3.0 - 2023-09-13 diff --git a/src/services/Discounts.php b/src/services/Discounts.php index 4dbb874998..545678877b 100644 --- a/src/services/Discounts.php +++ b/src/services/Discounts.php @@ -693,6 +693,12 @@ public function saveDiscount(Discount $model, bool $runValidation = true): bool $record = new DiscountRecord(); } + // Make sure the datetime attributes are populated before firing the event + if (!$isNew) { + $model->dateCreated = DateTimeHelper::toDateTime($record->dateCreated); + $model->dateUpdated = DateTimeHelper::toDateTime($record->dateUpdated); + } + // Raise the beforeSaveDiscount event if ($this->hasEventHandlers(self::EVENT_BEFORE_SAVE_DISCOUNT)) { $this->trigger(self::EVENT_BEFORE_SAVE_DISCOUNT, new DiscountEvent([ @@ -753,6 +759,10 @@ public function saveDiscount(Discount $model, bool $runValidation = true): bool $record->save(false); $model->id = $record->id; + // Update datetime attributes after save + $model->dateCreated = DateTimeHelper::toDateTime($record->dateCreated); + $model->dateUpdated = DateTimeHelper::toDateTime($record->dateUpdated); + DiscountPurchasableRecord::deleteAll(['discountId' => $model->id]); DiscountCategoryRecord::deleteAll(['discountId' => $model->id]); diff --git a/src/services/Sales.php b/src/services/Sales.php index 2a7d34f23a..858c491524 100644 --- a/src/services/Sales.php +++ b/src/services/Sales.php @@ -24,6 +24,7 @@ use craft\elements\Category; use craft\elements\Entry; use craft\helpers\ArrayHelper; +use craft\helpers\DateTimeHelper; use DateTime; use yii\base\Component; use yii\base\Exception; @@ -531,6 +532,12 @@ public function saveSale(Sale $model, bool $runValidation = true): bool $model->setPurchasableIds([]); } + // Make sure `dateCreated` and `dateUpdated` are set on the model + if (!$isNewSale) { + $model->dateCreated = DateTimeHelper::toDateTime($record->dateCreated); + $model->dateUpdated = DateTimeHelper::toDateTime($record->dateUpdated); + } + // Fire an 'beforeSaveSection' event if ($this->hasEventHandlers(self::EVENT_BEFORE_SAVE_SALE)) { $this->trigger(self::EVENT_BEFORE_SAVE_SALE, new SaleEvent([ @@ -546,6 +553,10 @@ public function saveSale(Sale $model, bool $runValidation = true): bool $record->save(false); $model->id = $record->id; + // Update datetime attributes + $model->dateCreated = DateTimeHelper::toDateTime($record->dateCreated); + $model->dateUpdated = DateTimeHelper::toDateTime($record->dateUpdated); + SaleUserGroupRecord::deleteAll(['saleId' => $model->id]); SalePurchasableRecord::deleteAll(['saleId' => $model->id]); SaleCategoryRecord::deleteAll(['saleId' => $model->id]);