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

[5.x] Product type’s max levels setting not being respected #3787

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 @@ -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';
nfourtythree marked this conversation as resolved.
Show resolved Hide resolved
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
Loading