Skip to content

Commit

Permalink
Merge pull request #3775 from craftcms/feature/support-with-fields-pa…
Browse files Browse the repository at this point in the history
…ram-on-resave-action

Support `—withFields` on resave commands
  • Loading branch information
lukeholder authored Nov 20, 2024
2 parents 03cebf1 + f63fc97 commit b00299a
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 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

- Added the `--with-fields` option to all Commerce `resave/*` commands.
- Fixed a SQL error that could occur when updating. ([#3778](https://github.com/craftcms/commerce/issues/3778))

## 5.2.4 - 2024-11-14
Expand Down
47 changes: 44 additions & 3 deletions src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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.',
Expand All @@ -1159,23 +1182,41 @@ 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.',
];

$e->actions['carts'] = [
'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.',
];
});
Expand Down

0 comments on commit b00299a

Please sign in to comment.