Skip to content

Commit 40b73b4

Browse files
authored
Merge pull request #4159 from craftcms/nathaniel/com-493-5x-product-type-settings
[5.5] Add "Product type settings" action menu item to edit product screens
2 parents 3ab8945 + 1597f1c commit 40b73b4

5 files changed

Lines changed: 73 additions & 36 deletions

File tree

CHANGELOG-WIP.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
- Added preview targets for products. ([#4128](https://github.com/craftcms/commerce/pull/4128))
99
- Added slug translation options to product types. ([#4088](https://github.com/craftcms/commerce/pull/4088))
1010
- Gateway condition rules now allow multiple gateways to be selected. ([#4112](https://github.com/craftcms/commerce/issues/4112))
11+
- Product action menus now have “Product type settings” action, for admin users on environments that allow admin changes. ([#4157](https://github.com/craftcms/commerce/issues/4157))
1112

1213
### Development
1314
- Orders now have a `dateFirstPaid` property that records the date and time when the order was first paid in full.

src/controllers/ProductTypesController.php

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -67,46 +67,55 @@ public function actionEditProductType(int $productTypeId = null, ProductType $pr
6767
if (!empty($variables['productTypeId'])) {
6868
$variables['title'] = $variables['productType']->name;
6969
} else {
70-
$variables['title'] = Craft::t('commerce', 'Create a Product Type');
70+
$variables['title'] = Craft::t('commerce', 'Create a new product type');
7171
}
7272

7373
DebugPanel::prependOrAppendModelTab(model: $variables['productType'], prepend: true);
7474

75-
$tabs = [
76-
'productTypeSettings' => [
77-
'label' => Craft::t('commerce', 'Settings'),
78-
'url' => '#product-type-settings',
79-
],
80-
'taxAndShipping' => [
81-
'label' => Craft::t('commerce', 'Tax & Shipping'),
82-
'url' => '#tax-and-shipping',
83-
],
84-
'productFields' => [
85-
'label' => Craft::t('commerce', 'Product Fields'),
86-
'url' => '#product-fields',
87-
],
88-
'variantFields' => [
89-
'label' => Craft::t('commerce', 'Variant Fields'),
90-
'url' => '#variant-fields',
91-
],
92-
];
93-
94-
$variables['tabs'] = $tabs;
9575
$variables['selectedTab'] = 'productTypeSettings';
9676

9777
$this->getView()->registerAssetBundle(EditSectionAsset::class);
9878

9979
$variables['readOnly'] = $this->isReadOnlyScreen();
10080

101-
return $this->renderTemplate('commerce/settings/producttypes/_edit', $variables);
81+
return $this->asCpScreen()
82+
->title($variables['title'])
83+
->crumbs([
84+
['label' => Craft::t('commerce', 'Commerce'), 'url' => 'commerce'],
85+
['label' => Craft::t('app', 'Settings'), 'url' => 'commerce/settings', 'ariaLabel' => Craft::t('commerce', 'Commerce Settings')],
86+
['label' => Craft::t('commerce', 'Product Types'), 'url' => 'commerce/settings/producttypes'],
87+
])
88+
->tabs([
89+
'productTypeSettings' => [
90+
'label' => Craft::t('commerce', 'Settings'),
91+
'url' => '#product-type-settings',
92+
],
93+
'taxAndShipping' => [
94+
'label' => Craft::t('commerce', 'Tax & Shipping'),
95+
'url' => '#tax-and-shipping',
96+
],
97+
'productFields' => [
98+
'label' => Craft::t('commerce', 'Product Fields'),
99+
'url' => '#product-fields',
100+
],
101+
'variantFields' => [
102+
'label' => Craft::t('commerce', 'Variant Fields'),
103+
'url' => '#variant-fields',
104+
],
105+
])
106+
->selectedSubnavItem('settings')
107+
->action('commerce/product-types/save-product-type')
108+
->submitButtonLabel(Craft::t('app', 'Save'))
109+
->redirectUrl('commerce/settings/producttypes')
110+
->contentTemplate('commerce/settings/producttypes/_edit', $variables);
102111
}
103112

104113
/**
105114
* @throws HttpException
106115
* @throws Throwable
107116
* @throws BadRequestHttpException
108117
*/
109-
public function actionSaveProductType(): void
118+
public function actionSaveProductType(): ?Response
110119
{
111120
$currentUser = Craft::$app->getUser()->getIdentity();
112121

@@ -199,16 +208,15 @@ public function actionSaveProductType(): void
199208

200209
// Save it
201210
if (Plugin::getInstance()->getProductTypes()->saveProductType($productType)) {
202-
$this->setSuccessFlash(Craft::t('commerce', 'Product type saved.'));
203-
$this->redirectToPostedUrl($productType);
204-
} else {
205-
$this->setFailFlash(Craft::t('commerce', 'Couldn’t save product type.'));
211+
return $this->asSuccess(Craft::t('commerce', 'Product type saved.'), redirect: $this->getPostedRedirectUrl($productType));
206212
}
207213

208214
// Send the productType back to the template
209215
Craft::$app->getUrlManager()->setRouteParams([
210216
'productType' => $productType,
211217
]);
218+
219+
return $this->asModelFailure($productType, Craft::t('commerce', 'Couldn’t save product type.'), 'productType');
212220
}
213221

214222
/**

src/elements/Product.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,42 @@ protected static function defineActions(string $source = null): array
463463
return $actions;
464464
}
465465

466+
/**
467+
* @inheritdoc
468+
*/
469+
protected function safeActionMenuItems(): array
470+
{
471+
$actions = parent::safeActionMenuItems();
472+
473+
if (
474+
Craft::$app->getUser()->getIsAdmin() &&
475+
Craft::$app->getConfig()->getGeneral()->allowAdminChanges
476+
) {
477+
// Product type settings
478+
$productTypeEditId = sprintf('edit-product-type-%s', mt_rand());
479+
$actions[] = [
480+
'id' => $productTypeEditId,
481+
'icon' => 'gear',
482+
'label' => Craft::t('commerce', 'Product type settings'),
483+
];
484+
485+
$view = Craft::$app->getView();
486+
$view->registerJsWithVars(fn($id, $params) => <<<JS
487+
(() => {
488+
$('#' + $id).on('activate', function() {
489+
const params = $params;
490+
new Craft.CpScreenSlideout('commerce/product-types/edit-product-type', {params});
491+
});
492+
})();
493+
JS, [
494+
$view->namespaceInputId($productTypeEditId),
495+
['productTypeId' => $this->typeId],
496+
]);
497+
}
498+
499+
return $actions;
500+
}
501+
466502
/**
467503
* @inheritdoc
468504
*/

src/templates/settings/producttypes/_edit.twig

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,3 @@
1-
{% extends "commerce/_layouts/cp" %}
2-
{% set title = productType.id ? productType.name : 'Create a new product type'|t('commerce') %}
3-
4-
{% set crumbs = [
5-
{ label: 'Commerce'|t('commerce'), url: url('commerce') },
6-
{ label: 'Settings'|t('app'), url: url('commerce/settings'), ariaLabel: 'Commerce Settings'|t('commerce') },
7-
{ label: "Product Types"|t('commerce'), url: url('commerce/settings/producttypes') },
8-
] %}
9-
101
{% set selectedSubnavItem = 'settings' %}
112

123
{% set headlessMode = craft.app.config.general.headlessMode %}

src/translations/en/commerce.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -883,6 +883,7 @@
883883
'Product Variant' => 'Product Variant',
884884
'Product Variants' => 'Product Variants',
885885
'Product type saved.' => 'Product type saved.',
886+
'Product type settings' => 'Product type settings',
886887
'Product' => 'Product',
887888
'Products and Variants deleted.' => 'Products and Variants deleted.',
888889
'Products not restored.' => 'Products not restored.',

0 commit comments

Comments
 (0)