diff --git a/CHANGELOG.md b/CHANGELOG.md index a29109fa59..2c77fc6bc1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Release Notes for Craft Commerce +## Unreleased + +- Added the `--with-fields` option to all Commerce `resave/*` commands. + ## 5.2.4 - 2024-11-14 - Improved the performance of `craft\commerce\elements\Product::getVariants()`. ([#3578](https://github.com/craftcms/commerce/issues/3758)) diff --git a/src/Plugin.php b/src/Plugin.php index 3e95e4d3c6..4e5522ebaa 100755 --- a/src/Plugin.php +++ b/src/Plugin.php @@ -51,6 +51,7 @@ use craft\commerce\helpers\ProjectConfigData; use craft\commerce\linktypes\Product as ProductLinkType; use craft\commerce\migrations\Install; +use craft\commerce\models\ProductType; use craft\commerce\models\Settings; use craft\commerce\plugin\Routes; use craft\commerce\plugin\Services as CommerceServices; @@ -165,7 +166,9 @@ use craft\web\Application; use craft\web\twig\variables\CraftVariable; use Exception; +use Illuminate\Support\Collection; use yii\base\Event; +use yii\console\ExitCode; use yii\web\User; /** @@ -1143,12 +1146,32 @@ private function _defineResaveCommand(): void /** @var ResaveController $controller */ $controller = Craft::$app->controller; $criteria = []; + if ($controller->type !== null) { $criteria['type'] = explode(',', $controller->type); } + + // @TODO Remove this check when Commerce requires Craft 5.5 + if (version_compare(Craft::$app->getInfo()->version, '5.5.0', '>=') && !empty($controller->withFields)) { + $handles = Collection::make(self::getInstance()->getProductTypes()->getAllProductTypes()) + ->filter(fn(ProductType $productType) => $controller->hasTheFields($productType->getFieldLayout())) + ->map(fn(ProductType $productType) => $productType->handle) + ->all(); + if (isset($criteria['type'])) { + $criteria['type'] = array_intersect($criteria['type'], $handles); + } else { + $criteria['type'] = $handles; + } + + if (empty($criteria['type'])) { + $controller->output($controller->markdownToAnsi('No product types satisfy `--with-fields`.')); + return ExitCode::UNSPECIFIED_ERROR; + } + } + return $controller->resaveElements(Product::class, $criteria); }, - 'options' => ['type'], + 'options' => array_filter(['type', (property_exists(ResaveController::class, 'withFields') ? 'withFields' : null)]), 'helpSummary' => 'Re-saves Commerce products.', 'optionsHelp' => [ 'type' => 'The product type handle(s) of the products to resave.', @@ -1159,11 +1182,20 @@ private function _defineResaveCommand(): void 'action' => function(): int { /** @var ResaveController $controller */ $controller = Craft::$app->controller; + // @TODO Remove this check when Commerce requires Craft 5.5 + if (version_compare(Craft::$app->getInfo()->version, '5.5.0', '>=') && !empty($controller->withFields)) { + $fieldLayout = Craft::$app->getFields()->getLayoutByType(Order::class); + if (!$controller->hasTheFields($fieldLayout)) { + $controller->output($controller->markdownToAnsi('The order field layout doesn’t satisfy `--with-fields`.')); + return ExitCode::UNSPECIFIED_ERROR; + } + } + return $controller->resaveElements(Order::class, [ 'isCompleted' => true, ]); }, - 'options' => [], + 'options' => array_filter([(property_exists(ResaveController::class, 'withFields') ? 'withFields' : null)]), 'helpSummary' => 'Re-saves completed Commerce orders.', ]; @@ -1171,11 +1203,20 @@ private function _defineResaveCommand(): void 'action' => function(): int { /** @var ResaveController $controller */ $controller = Craft::$app->controller; + // @TODO Remove this check when Commerce requires Craft 5.5 + if (version_compare(Craft::$app->getInfo()->version, '5.5.0', '>=') && !empty($controller->withFields)) { + $fieldLayout = Craft::$app->getFields()->getLayoutByType(Order::class); + if (!$controller->hasTheFields($fieldLayout)) { + $controller->output($controller->markdownToAnsi('The order field layout doesn’t satisfy `--with-fields`.')); + return ExitCode::UNSPECIFIED_ERROR; + } + } + return $controller->resaveElements(Order::class, [ 'isCompleted' => false, ]); }, - 'options' => [], + 'options' => array_filter([(property_exists(ResaveController::class, 'withFields') ? 'withFields' : null)]), 'helpSummary' => 'Re-saves Commerce carts.', ]; });