Skip to content

Commit

Permalink
Add product type condition rule
Browse files Browse the repository at this point in the history
Fixed #3209
  • Loading branch information
lukeholder committed Jul 10, 2023
1 parent 082cc2f commit 1b98df2
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 0 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 “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.
Expand Down
1 change: 1 addition & 0 deletions src/elements/conditions/products/ProductCondition.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class ProductCondition extends ElementCondition
protected function conditionRuleTypes(): array
{
return array_merge(parent::conditionRuleTypes(), [
ProductTypeConditionRule::class,
]);
}
}
78 changes: 78 additions & 0 deletions src/elements/conditions/products/ProductTypeConditionRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/

namespace craft\commerce\elements\conditions\products;

use Craft;
use craft\base\conditions\BaseMultiSelectConditionRule;
use craft\base\ElementInterface;
use craft\commerce\elements\db\ProductQuery;
use craft\commerce\elements\Product;
use craft\commerce\models\ProductType;
use craft\commerce\Plugin;
use craft\elements\conditions\ElementConditionRuleInterface;
use craft\elements\db\ElementQueryInterface;
use craft\helpers\ArrayHelper;
use yii\base\InvalidConfigException;

/**
* Customer Condition Rule
*
* @author Pixel & Tonic, Inc. <support@pixelandtonic.com>
* @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) {

Check failure on line 65 in src/elements/conditions/products/ProductTypeConditionRule.php

View workflow job for this annotation

GitHub Actions / ci / Code Quality / PHPStan / PHPStan

Parameter #1 $value of method craft\commerce\elements\db\ProductQuery::type() expects (craft\commerce\models\ProductType&iterable<string>)|string|null, array|null given.
return ArrayHelper::firstWhere($productTypes, 'uid', $value)?->handle;
}));
}

/**
* @throws InvalidConfigException
*/
public function matchElement(ElementInterface $element): bool
{
/** @var Product $element */
return $this->matchValue((string)$element->getType());
}
}

0 comments on commit 1b98df2

Please sign in to comment.