Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed #3298 population of audit columns on Sales and Discounts after saving #3301

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading