From 1b98df2f12b3e1a077fc4c25950a3b5fca50dada Mon Sep 17 00:00:00 2001 From: Luke Holder Date: Tue, 11 Jul 2023 07:44:57 +0800 Subject: [PATCH] Add product type condition rule Fixed #3209 --- CHANGELOG.md | 1 + .../conditions/products/ProductCondition.php | 1 + .../products/ProductTypeConditionRule.php | 78 +++++++++++++++++++ 3 files changed, 80 insertions(+) create mode 100644 src/elements/conditions/products/ProductTypeConditionRule.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 940741a549..f76fca2e94 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Unreleased +- Added the “Product Type” product condition rule. ([#3209](https://github.com/craftcms/commerce/issues/3209)) - Fixed a bug where `hasMatchingAddresses()` was incorrectly returning `false`. ([#3183](https://github.com/craftcms/commerce/issues/3183)) - Fixed a bug where changing a user’s email would cause extra user elements to be created. ([#3138](https://github.com/craftcms/commerce/issues/3138)) - Fixed a bug where related sales were showing when creating a new product. diff --git a/src/elements/conditions/products/ProductCondition.php b/src/elements/conditions/products/ProductCondition.php index d59d16b2ca..017ffe9976 100644 --- a/src/elements/conditions/products/ProductCondition.php +++ b/src/elements/conditions/products/ProductCondition.php @@ -18,6 +18,7 @@ class ProductCondition extends ElementCondition protected function conditionRuleTypes(): array { return array_merge(parent::conditionRuleTypes(), [ + ProductTypeConditionRule::class, ]); } } diff --git a/src/elements/conditions/products/ProductTypeConditionRule.php b/src/elements/conditions/products/ProductTypeConditionRule.php new file mode 100644 index 0000000000..481fbf252f --- /dev/null +++ b/src/elements/conditions/products/ProductTypeConditionRule.php @@ -0,0 +1,78 @@ + + * @since 4.3.0 + */ +class ProductTypeConditionRule extends BaseMultiSelectConditionRule implements ElementConditionRuleInterface +{ + /** + * @inheritdoc + */ + public function getLabel(): string + { + return Craft::t('commerce', 'Product Type'); + } + + /** + * @return array + */ + protected function options(): array + { + return collect(Plugin::getInstance()->getProductTypes()->getAllProductTypes()) + ->map(fn(ProductType $productType) => ['value' => $productType->uid , 'label' => $productType->name]) + ->all(); + } + + /** + * @inheritdoc + */ + public function getExclusiveQueryParams(): array + { + return ['type']; + } + + /** + * @throws InvalidConfigException + */ + public function modifyQuery(ElementQueryInterface $query): void + { + /** @var ProductQuery $query */ + $productTypes = Plugin::getInstance()->getProductTypes()->getAllProductTypes(); + + /** @var ProductQuery $query */ + $query->type($this->paramValue(function(string $value) use ($productTypes) { + return ArrayHelper::firstWhere($productTypes, 'uid', $value)?->handle; + })); + } + + /** + * @throws InvalidConfigException + */ + public function matchElement(ElementInterface $element): bool + { + /** @var Product $element */ + return $this->matchValue((string)$element->getType()); + } +}