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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\SalesRule\Model\Rule\Condition;
/**
* Address rule condition data model.
*/
class Address extends \Magento\Rule\Model\Condition\AbstractCondition
{
/**
* @var \Magento\Directory\Model\Config\Source\Country
*/
protected $_directoryCountry;
/**
* @var \Magento\Directory\Model\Config\Source\Allregion
*/
protected $_directoryAllregion;
/**
* @var \Magento\Shipping\Model\Config\Source\Allmethods
*/
protected $_shippingAllmethods;
/**
* @var \Magento\Payment\Model\Config\Source\Allmethods
*/
protected $_paymentAllmethods;
/**
* @param \Magento\Rule\Model\Condition\Context $context
* @param \Magento\Directory\Model\Config\Source\Country $directoryCountry
* @param \Magento\Directory\Model\Config\Source\Allregion $directoryAllregion
* @param \Magento\Shipping\Model\Config\Source\Allmethods $shippingAllmethods
* @param \Magento\Payment\Model\Config\Source\Allmethods $paymentAllmethods
* @param array $data
*/
public function __construct(
\Magento\Rule\Model\Condition\Context $context,
\Magento\Directory\Model\Config\Source\Country $directoryCountry,
\Magento\Directory\Model\Config\Source\Allregion $directoryAllregion,
\Magento\Shipping\Model\Config\Source\Allmethods $shippingAllmethods,
\Magento\Payment\Model\Config\Source\Allmethods $paymentAllmethods,
array $data = []
) {
parent::__construct($context, $data);
$this->_directoryCountry = $directoryCountry;
$this->_directoryAllregion = $directoryAllregion;
$this->_shippingAllmethods = $shippingAllmethods;
$this->_paymentAllmethods = $paymentAllmethods;
}
/**
* Load attribute options
*
* @return $this
*/
public function loadAttributeOptions()
{
$attributes = [
'base_subtotal' => __('Subtotal'),
'total_qty' => __('Total Items Quantity'),
'weight' => __('Total Weight'),
'payment_method' => __('Payment Method'),
'shipping_method' => __('Shipping Method'),
'postcode' => __('Shipping Postcode'),
'region' => __('Shipping Region'),
'region_id' => __('Shipping State/Province'),
'country_id' => __('Shipping Country'),
];
$this->setAttributeOption($attributes);
return $this;
}
/**
* Get attribute element
*
* @return $this
*/
public function getAttributeElement()
{
$element = parent::getAttributeElement();
$element->setShowAsText(true);
return $element;
}
/**
* Get input type
*
* @return string
*/
public function getInputType()
{
switch ($this->getAttribute()) {
case 'base_subtotal':
case 'weight':
case 'total_qty':
return 'numeric';
case 'shipping_method':
case 'payment_method':
case 'country_id':
case 'region_id':
return 'select';
}
return 'string';
}
/**
* Get value element type
*
* @return string
*/
public function getValueElementType()
{
switch ($this->getAttribute()) {
case 'shipping_method':
case 'payment_method':
case 'country_id':
case 'region_id':
return 'select';
}
return 'text';
}
/**
* Get value select options
*
* @return array|mixed
*/
public function getValueSelectOptions()
{
if (!$this->hasData('value_select_options')) {
switch ($this->getAttribute()) {
case 'country_id':
$options = $this->_directoryCountry->toOptionArray();
break;
case 'region_id':
$options = $this->_directoryAllregion->toOptionArray();
break;
case 'shipping_method':
$options = $this->_shippingAllmethods->toOptionArray();
break;
case 'payment_method':
$options = $this->_paymentAllmethods->toOptionArray();
break;
default:
$options = [];
}
$this->setData('value_select_options', $options);
}
return $this->getData('value_select_options');
}
/**
* Validate Address Rule Condition
*
* @param \Magento\Framework\Model\AbstractModel $model
* @return bool
*/
public function validate(\Magento\Framework\Model\AbstractModel $model)
{
$address = $model;
if (!$address instanceof \Magento\Quote\Model\Quote\Address) {
if ($model->getQuote()->isVirtual()) {
$address = $model->getQuote()->getBillingAddress();
} else {
$address = $model->getQuote()->getShippingAddress();
}
}
if ('payment_method' == $this->getAttribute() && !$address->hasPaymentMethod()) {
$address->setPaymentMethod($model->getQuote()->getPayment()->getMethod());
}
return parent::validate($address);
}
}