From 75f4a0a8557aab3ae2cf5abd810a093ff65ee152 Mon Sep 17 00:00:00 2001 From: Luke Holder Date: Tue, 17 Oct 2023 17:08:22 +0800 Subject: [PATCH] Fixes #3297 --- CHANGELOG.md | 1 + src/elements/Variant.php | 9 ++++++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9e63eb71b4..c9cf19e41e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ - 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)) - The order param is now included when triggering the `craft\commerce\services\Purchasables::EVENT_PURCHASABLE_SHIPPABLE` event. ([#3279](https://github.com/craftcms/commerce/pull/3279)) +- The auto-generated variant titles and SKUs are now generated when before saving a product in addition to before validation. ((#3297)[https://github.com/craftcms/commerce/pull/3297]) ## 4.3.0 - 2023-09-13 diff --git a/src/elements/Variant.php b/src/elements/Variant.php index a097087167..4f45813467 100644 --- a/src/elements/Variant.php +++ b/src/elements/Variant.php @@ -1127,6 +1127,7 @@ public static function isLocalized(): bool */ public function beforeValidate(): bool { + /** @var Product $product */ $product = $this->getProduct(); $this->updateTitle($product); @@ -1159,8 +1160,14 @@ public function beforeValidate(): bool */ public function beforeSave(bool $isNew): bool { + /** @var Product $product */ + $product = $this->getProduct(); + + $this->updateTitle($product); + $this->updateSku($product); + // Set the field layout - $productType = $this->getProduct()->getType(); + $productType = $product->getType(); $this->fieldLayoutId = $productType->variantFieldLayoutId; return parent::beforeSave($isNew);