Skip to content

Commit

Permalink
Merge pull request #3293 from craftcms/bugfix/customer-condition-rule…
Browse files Browse the repository at this point in the history
…-element-select

Update customer condition rule to use element selector
  • Loading branch information
lukeholder authored Oct 17, 2023
2 parents cff0b7b + bdc0e89 commit 451de16
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- Added the `commerce/gateways/webhook-url` command.
- Fixed a bug where the delete button would be shown for users that do not have permission to delete on the Product edit page. ([#3285](https://github.com/craftcms/commerce/issues/3285))
- Fixed a bug where deleted shipping categories were still available for selection. ([#3272](https://github.com/craftcms/commerce/issues/3272))
- Fixed a bug where the customer condition rule wasn’t loading correctly. ([#3291](https://github.com/craftcms/commerce/issues/3291))
- Fixed an error that could occur when rendering a PDF. ([#2633](https://github.com/craftcms/commerce/issues/2633))
- Fixed a bug where duplicate inactive users could be created when using the `commerce/upgrade` command. ([#3286](https://github.com/craftcms/commerce/issues/3286))

Expand Down
30 changes: 27 additions & 3 deletions src/elements/conditions/orders/CustomerConditionRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,17 @@
use craft\elements\conditions\ElementConditionRuleInterface;
use craft\elements\db\ElementQueryInterface;
use craft\elements\User;
use craft\helpers\Cp;
use craft\helpers\Db;
use yii\base\InvalidConfigException;
use yii\db\Expression;

/**
* Customer Condition Rule
*
* @author Pixel & Tonic, Inc. <[email protected]>
* @since 4.2.0
* @TODO change the class that the `CustomerConditionRule` extends
*/
class CustomerConditionRule extends BaseMultiSelectConditionRule implements ElementConditionRuleInterface
{
Expand All @@ -35,6 +39,7 @@ public function getLabel(): string

/**
* @return array
* @deprecated in 4.3.1.
*/
protected function options(): array
{
Expand All @@ -47,6 +52,24 @@ protected function options(): array
->all();
}

/**
* @inheritDoc
*/
protected function inputHtml(): string
{
$users = User::find()->status(null)->limit(null)->id($this->values)->all();

return Cp::elementSelectHtml([
'name' => 'values',
'elements' => $users,
'elementType' => User::class,
'sources' => null,
'criteria' => null,
'condition' => null,
'single' => false,
]);
}

/**
* @inheritdoc
*/
Expand All @@ -63,10 +86,11 @@ public function modifyQuery(ElementQueryInterface $query): void
/** @var OrderQuery $query */
$paramValue = $this->paramValue();
if ($this->operator === self::OPERATOR_NOT_IN) {
$paramValue = ['or', $paramValue, null];
// Account for the fact the querying using a combination of `not` and `in` doesn't match `null` in the column
$query->andWhere(Db::parseParam(new Expression('coalesce([[commerce_orders.customerId]], -1)'), $paramValue));
} else {
$query->customerId($paramValue);
}

$query->customerId($paramValue);
}

/**
Expand Down

0 comments on commit 451de16

Please sign in to comment.