/
CartAmountRule.php
57 lines (48 loc) · 1.49 KB
/
CartAmountRule.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
45
46
47
48
49
50
51
52
53
54
55
56
57
<?php declare(strict_types=1);
namespace Shopware\Core\Checkout\Cart\Rule;
use Shopware\Core\Framework\Log\Package;
use Shopware\Core\Framework\Rule\Exception\UnsupportedOperatorException;
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 CartAmountRule extends Rule
{
final public const RULE_NAME = 'cartCartAmount';
protected float $amount;
/**
* @internal
*/
public function __construct(
protected string $operator = self::OPERATOR_EQ,
?float $amount = null
) {
parent::__construct();
$this->amount = (float) $amount;
}
/**
* @throws UnsupportedOperatorException
*/
public function match(RuleScope $scope): bool
{
if (!$scope instanceof CartRuleScope) {
return false;
}
return RuleComparison::numeric($scope->getCart()->getPrice()->getTotalPrice(), $this->amount, $this->operator);
}
public function getConstraints(): array
{
return [
'amount' => RuleConstraints::float(),
'operator' => RuleConstraints::numericOperators(false),
];
}
public function getConfig(): RuleConfig
{
return (new RuleConfig())
->operatorSet(RuleConfig::OPERATOR_SET_NUMBER)
->numberField('amount');
}
}