/
LineItemsInCartCountRule.php
44 lines (35 loc) · 1.18 KB
/
LineItemsInCartCountRule.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<?php declare(strict_types=1);
namespace Shopware\Core\Checkout\Cart\Rule;
use Shopware\Core\Framework\Log\Package;
use Shopware\Core\Framework\Rule\Rule;
use Shopware\Core\Framework\Rule\RuleComparison;
use Shopware\Core\Framework\Rule\RuleConfig;
use Shopware\Core\Framework\Rule\RuleConstraints;
use Shopware\Core\Framework\Rule\RuleScope;
#[Package('services-settings')]
class LineItemsInCartCountRule extends Rule
{
final public const RULE_NAME = 'cartLineItemsInCartCount';
protected int $count;
protected string $operator;
public function match(RuleScope $scope): bool
{
if (!$scope instanceof CartRuleScope) {
return false;
}
return RuleComparison::numeric((float) $scope->getCart()->getLineItems()->count(), (float) $this->count, $this->operator);
}
public function getConstraints(): array
{
return [
'count' => RuleConstraints::int(),
'operator' => RuleConstraints::numericOperators(false),
];
}
public function getConfig(): RuleConfig
{
return (new RuleConfig())
->operatorSet(RuleConfig::OPERATOR_SET_NUMBER)
->intField('count');
}
}