-
Notifications
You must be signed in to change notification settings - Fork 170
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
3 changed files
with
80 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
src/elements/conditions/products/ProductTypeConditionRule.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 GitHub Actions / ci / Code Quality / PHPStan / PHPStan
|
||
return ArrayHelper::firstWhere($productTypes, 'uid', $value)?->handle; | ||
})); | ||
} | ||
|
||
/** | ||
* @throws InvalidConfigException | ||
*/ | ||
public function matchElement(ElementInterface $element): bool | ||
{ | ||
/** @var Product $element */ | ||
return $this->matchValue((string)$element->getType()); | ||
} | ||
} |