Skip to content

Commit

Permalink
Merge pull request #3301 from craftcms/bugfix/3298-4x-sale-discount-s…
Browse files Browse the repository at this point in the history
…et-dateupdated-+-datecreated-after-save

Fixed #3298 population of audit columns on Sales and Discounts after saving
  • Loading branch information
lukeholder authored Oct 17, 2023
2 parents 451de16 + 92e90d1 commit 4a74cd0
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions src/services/Discounts.php
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand Down Expand Up @@ -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]);

Expand Down
11 changes: 11 additions & 0 deletions src/services/Sales.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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([
Expand All @@ -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]);
Expand Down

0 comments on commit 4a74cd0

Please sign in to comment.