diff --git a/CHANGELOG.md b/CHANGELOG.md index ea467fbb79..dbb8ba53be 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ - Fixed a bug where deleted shipping categories were still available for selection. ([#3272](https://github.com/craftcms/commerce/issues/3272)) - Fixed a bug where the customer condition rule wasn’t loading correctly. ([#3291](https://github.com/craftcms/commerce/issues/3291)) - 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)) - Fixed a bug where duplicate inactive users could be created when using the `commerce/upgrade` command. ([#3286](https://github.com/craftcms/commerce/issues/3286)) ## 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]);