Skip to content

Commit

Permalink
Merge pull request #3787 from craftcms/feature/pt-2323-5x-max-levels-…
Browse files Browse the repository at this point in the history
…are-not-taken-into-account-for-structured

[5.x] Product type’s max levels setting not being respected
  • Loading branch information
nfourtythree authored Nov 29, 2024
2 parents 7e560a6 + b8ce3ba commit 5049483
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ public static function editions(): array
/**
* @inheritDoc
*/
public string $schemaVersion = '5.2.0.5';
public string $schemaVersion = '5.2.7.0';

/**
* @inheritdoc
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

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.
*/
class m241128_174712_fix_maxLevels_structured_productTypes extends Migration
{
/**
* @inheritdoc
*/
public function safeUp(): bool
{
// Get all product types from Commerce project config
$structuredProductTypesWithMaxLevels = (new Query())
->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(CraftTable::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;
}
}
2 changes: 1 addition & 1 deletion src/services/ProductTypes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 5049483

Please sign in to comment.