Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update customer condition rule to use element selector #3293

Merged
merged 9 commits into from
Oct 17, 2023
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