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

Support —withFields on resave commands #3775

Merged
merged 4 commits into from
Nov 20, 2024
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

- 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
Loading