From 2d4ae2ecaf75f41f3c15d192cdcede21a0e33a3f Mon Sep 17 00:00:00 2001 From: Nathaniel Hammond Date: Thu, 28 Nov 2024 17:57:16 +0000 Subject: [PATCH 1/3] =?UTF-8?q?#3785=20Product=20type=E2=80=99s=20max=20le?= =?UTF-8?q?vels=20setting=20not=20being=20respected?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 1 + src/Plugin.php | 2 +- ..._fix_maxLevels_structured_productTypes.php | 42 +++++++++++++++++++ src/services/ProductTypes.php | 2 +- 4 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 src/migrations/m241128_174712_fix_maxLevels_structured_productTypes.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 0f492d723f..0a27faa719 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Unreleased +- Fixed a bug where a structured product type’s “Max Levels” setting wasn’t being respected. ([#3785](https://github.com/craftcms/commerce/issues/3785)) - Fixed a bug where users could access the Commerce user screen when the current user didn’t have permission. ## 5.2.6 - 2024-11-26 diff --git a/src/Plugin.php b/src/Plugin.php index e8764107b6..831b393d94 100755 --- a/src/Plugin.php +++ b/src/Plugin.php @@ -257,7 +257,7 @@ public static function editions(): array /** * @inheritDoc */ - public string $schemaVersion = '5.2.0.5'; + public string $schemaVersion = '5.2.0.6'; /** * @inheritdoc diff --git a/src/migrations/m241128_174712_fix_maxLevels_structured_productTypes.php b/src/migrations/m241128_174712_fix_maxLevels_structured_productTypes.php new file mode 100644 index 0000000000..3483329e0f --- /dev/null +++ b/src/migrations/m241128_174712_fix_maxLevels_structured_productTypes.php @@ -0,0 +1,42 @@ +from('{{%commerce_producttypes}}') + ->where(['isStructure' => 1]) + ->andWhere(['not', ['maxLevels' => null]]) + ->collect(); + + // Loop through and update the `maxLevels` column in the `structures` table + $structuredProductTypesWithMaxLevels->each(function($productType) { + $this->update('{{%structures}}', ['maxLevels' => $productType['maxLevels']], ['id' => $productType['structureId']]); + }); + + return true; + } + + /** + * @inheritdoc + */ + public function safeDown(): bool + { + echo "m241128_174712_fix_maxLevels_structured_productTypes cannot be reverted.\n"; + return false; + } +} diff --git a/src/services/ProductTypes.php b/src/services/ProductTypes.php index ec5cd855da..d771561364 100755 --- a/src/services/ProductTypes.php +++ b/src/services/ProductTypes.php @@ -475,7 +475,7 @@ public function handleChangedProductType(ConfigEvent $event): void $structureUid = $data['structure']['uid']; $structure = Craft::$app->getStructures()->getStructureByUid($structureUid, true) ?? new Structure(['uid' => $structureUid]); $isNewStructure = empty($structure->id); - $structure->maxLevels = $data['structure']['maxLevels'] ?? null; + $structure->maxLevels = $data['maxLevels'] ?? null; Craft::$app->getStructures()->saveStructure($structure); $productTypeRecord->structureId = $structure->id; } else { From 5d0de8fc0d1b65c3dd5cc7b9769192bb31513251 Mon Sep 17 00:00:00 2001 From: Nathaniel Hammond Date: Thu, 28 Nov 2024 17:59:48 +0000 Subject: [PATCH 2/3] fix cs --- .../m241128_174712_fix_maxLevels_structured_productTypes.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/migrations/m241128_174712_fix_maxLevels_structured_productTypes.php b/src/migrations/m241128_174712_fix_maxLevels_structured_productTypes.php index 3483329e0f..4ead158992 100644 --- a/src/migrations/m241128_174712_fix_maxLevels_structured_productTypes.php +++ b/src/migrations/m241128_174712_fix_maxLevels_structured_productTypes.php @@ -2,7 +2,6 @@ namespace craft\commerce\migrations; -use Craft; use craft\db\Migration; use craft\db\Query; From b8ce3ba6f51f7374ebf53f5bd77c4bb8dd8dfe40 Mon Sep 17 00:00:00 2001 From: Nathaniel Hammond Date: Fri, 29 Nov 2024 15:10:06 +0000 Subject: [PATCH 3/3] Tweaks --- src/Plugin.php | 2 +- ...41128_174712_fix_maxLevels_structured_productTypes.php | 8 +++++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/Plugin.php b/src/Plugin.php index 831b393d94..8fef95aa8d 100755 --- a/src/Plugin.php +++ b/src/Plugin.php @@ -257,7 +257,7 @@ public static function editions(): array /** * @inheritDoc */ - public string $schemaVersion = '5.2.0.6'; + public string $schemaVersion = '5.2.7.0'; /** * @inheritdoc diff --git a/src/migrations/m241128_174712_fix_maxLevels_structured_productTypes.php b/src/migrations/m241128_174712_fix_maxLevels_structured_productTypes.php index 4ead158992..f07f69385d 100644 --- a/src/migrations/m241128_174712_fix_maxLevels_structured_productTypes.php +++ b/src/migrations/m241128_174712_fix_maxLevels_structured_productTypes.php @@ -2,8 +2,10 @@ namespace craft\commerce\migrations; +use craft\commerce\db\Table; use craft\db\Migration; use craft\db\Query; +use craft\db\Table as CraftTable; /** * m241128_174712_fix_maxLevels_structured_productTypes migration. @@ -17,14 +19,14 @@ public function safeUp(): bool { // Get all product types from Commerce project config $structuredProductTypesWithMaxLevels = (new Query()) - ->from('{{%commerce_producttypes}}') - ->where(['isStructure' => 1]) + ->from(Table::PRODUCTTYPES) + ->where(['isStructure' => true]) ->andWhere(['not', ['maxLevels' => null]]) ->collect(); // Loop through and update the `maxLevels` column in the `structures` table $structuredProductTypesWithMaxLevels->each(function($productType) { - $this->update('{{%structures}}', ['maxLevels' => $productType['maxLevels']], ['id' => $productType['structureId']]); + $this->update(CraftTable::STRUCTURES, ['maxLevels' => $productType['maxLevels']], ['id' => $productType['structureId']]); }); return true;