PluginProbe
PostNL for WooCommerce / 4.0.0
PostNL for WooCommerce v4.0.0
5.9.12 5.9.11 5.9.10 5.9.9 5.9.8 5.9.7 5.9.6 trunk 2.5.0 2.5.1 2.5.2 2.5.3 2.5.4 2.5.5 3.1.4 3.1.5 3.1.6 3.1.7 4.0.0 4.0.1 4.0.2 4.3.2 4.3.3 4.4.0 4.4.1 All 72 releases
woo-postnl / includes / admin / OrderSettings.php

OrderSettings.php in PostNL for WooCommerce 4.0.0, at includes/admin/OrderSettings.php

343 lines 7.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 declare(strict_types=1);
4
5 use MyParcelNL\Sdk\src\Adapter\DeliveryOptions\AbstractDeliveryOptionsAdapter;
6 use MyParcelNL\Sdk\src\Model\Consignment\AbstractConsignment;
7 use WPO\WC\PostNL\Compatibility\Order as WCX_Order;
8
9 class OrderSettings
10 {
11 /**
12 * @var \MyParcelNL\Sdk\src\Adapter\DeliveryOptions\AbstractDeliveryOptionsAdapter
13 */
14 private $deliveryOptions;
15
16 /**
17 * @var \WC_Order
18 */
19 private $order;
20
21 /**
22 * @var string|null
23 */
24 private $carrier;
25
26 /**
27 * @var \MyParcelNL\Sdk\src\Adapter\DeliveryOptions\AbstractShipmentOptionsAdapter|null
28 */
29 private $shipmentOptions;
30
31 /**
32 * @var float
33 */
34 private $weight;
35 /**
36 * @var bool
37 */
38 private $ageCheck;
39
40 /**
41 * @var bool
42 */
43 private $insured;
44
45 /**
46 * @var int
47 */
48 private $insuranceAmount;
49
50 /**
51 * @var string
52 */
53 private $labelDescription;
54
55 /**
56 * @var bool
57 */
58 private $largeFormat;
59
60 /**
61 * @var bool
62 */
63 private $onlyRecipient;
64
65 /**
66 * @var string
67 */
68 private $packageType;
69
70 /**
71 * @var bool
72 */
73 private $returnShipment;
74
75 /**
76 * @var bool
77 */
78 private $signature;
79
80 /**
81 * @var int
82 */
83 private $colloAmount;
84
85 /**
86 * @var array
87 */
88 private $extraOptionsMeta;
89
90 /**
91 * @param \MyParcelNL\Sdk\src\Adapter\DeliveryOptions\AbstractDeliveryOptionsAdapter $deliveryOptions
92 * @param WC_Order $order
93 *
94 * @throws \Exception
95 */
96 public function __construct(
97 AbstractDeliveryOptionsAdapter $deliveryOptions,
98 WC_Order $order
99 ) {
100 $this->order = $order;
101 $this->deliveryOptions = $deliveryOptions;
102 $this->carrier = $deliveryOptions->getCarrier() ?? WCPN_Data::DEFAULT_CARRIER;
103 $this->shipmentOptions = $deliveryOptions->getShipmentOptions();
104
105 $this->extraOptionsMeta = WCX_Order::get_meta($this->order, WCPOST_Admin::META_SHIPMENT_OPTIONS_EXTRA);
106
107 $this->setAllData();
108 }
109
110 /**
111 * @return float
112 */
113 public function getWeight(): float
114 {
115 return $this->weight;
116 }
117
118
119 /**
120 * @return bool
121 */
122 public function hasAgeCheck(): bool
123 {
124 return $this->ageCheck;
125 }
126
127 /**
128 * @return int
129 */
130 public function getColloAmount(): int
131 {
132 return $this->colloAmount;
133 }
134
135 /**
136 * @return bool
137 */
138 public function isInsured(): bool
139 {
140 return $this->insured;
141 }
142
143 /**
144 * @return int
145 */
146 public function getInsuranceAmount(): int
147 {
148 return $this->insuranceAmount;
149 }
150
151 /**
152 * @return mixed|string
153 */
154 public function getLabelDescription(): string
155 {
156 return $this->labelDescription;
157 }
158
159 /**
160 * @return bool
161 */
162 public function hasLargeFormat(): bool
163 {
164 return $this->largeFormat;
165 }
166
167 /**
168 * @return bool
169 */
170 public function hasOnlyRecipient(): bool
171 {
172 return $this->onlyRecipient;
173 }
174
175 /**
176 * @return string
177 */
178 public function getPackageType(): string
179 {
180 return $this->packageType;
181 }
182
183 /**
184 * @return bool
185 */
186 public function hasReturnShipment(): bool
187 {
188 return $this->returnShipment;
189 }
190
191 /**
192 * @return bool
193 */
194 public function hasSignature(): bool
195 {
196 return $this->signature;
197 }
198
199 /**
200 * @throws \Exception
201 */
202 private function setAllData(): void
203 {
204 $this->setPackageType();
205 $this->setColloAmount();
206 $this->setLabelDescription();
207
208 $this->setAgeCheck();
209 $this->setOnlyRecipient();
210 $this->setReturnShipment();
211 $this->setSignature();
212
213 $this->setInsuranceData();
214
215 $this->setWeight();
216 }
217
218 /**
219 * @return void
220 */
221 private function setWeight(): void
222 {
223 $orderWeight = $this->order->get_meta(WCPOST_Admin::META_ORDER_WEIGHT);
224
225 $this->weight = (float) $orderWeight;
226 }
227
228 /**
229 * @return void
230 */
231 private function setAgeCheck(): void
232 {
233 $this->ageCheck = (bool) WCPN_Export::getChosenOrDefaultShipmentOption(
234 $this->shipmentOptions->hasAgeCheck(),
235 "{$this->carrier}_" . WCPOST_Settings::SETTING_CARRIER_DEFAULT_EXPORT_AGE_CHECK
236 );
237 }
238
239 /**
240 * @return void
241 */
242 private function setColloAmount(): void
243 {
244 $this->colloAmount = (int) ($this->extraOptionsMeta['collo_amount'] ?? 1);
245 }
246
247 /**
248 * Sets insured and insuranceAmount.
249 *
250 * @return void
251 */
252 private function setInsuranceData(): void
253 {
254 $isInsured = false;
255 $insuranceAmount = 0;
256
257 $isDefaultInsured = $this->getCarrierSetting(
258 WCPOST_Settings::SETTING_CARRIER_DEFAULT_EXPORT_INSURED
259 );
260 $isDefaultInsuredFromPrice = $this->getCarrierSetting(
261 WCPOST_Settings::SETTING_CARRIER_DEFAULT_EXPORT_INSURED_FROM_PRICE
262 );
263 $orderTotalExceedsInsuredFromPrice = $this->order->get_total() >= $isDefaultInsuredFromPrice;
264 $insuranceFromDeliveryOptions = $this->shipmentOptions->getInsurance();
265
266 if ($insuranceFromDeliveryOptions) {
267 $isInsured = (bool) $insuranceFromDeliveryOptions;
268 $insuranceAmount = $insuranceFromDeliveryOptions;
269 } elseif ($isDefaultInsured && $orderTotalExceedsInsuredFromPrice) {
270 $isInsured = true;
271 $insuranceAmount = $this->getCarrierSetting(WCPOST_Settings::SETTING_CARRIER_DEFAULT_EXPORT_INSURED_AMOUNT);
272 }
273
274 $this->insured = $isInsured;
275 $this->insuranceAmount = (int) $insuranceAmount;
276 }
277
278 /**
279 * @return void
280 */
281 private function setLabelDescription(): void
282 {
283 $defaultValue = "Order: " . $this->order->get_id();
284 $valueFromSetting = WCPOST()->setting_collection->getByName(WCPOST_Settings::SETTING_LABEL_DESCRIPTION);
285 $valueFromOrder = $this->shipmentOptions->getLabelDescription();
286
287 $this->labelDescription = (string) ($valueFromOrder ?? $valueFromSetting ?? $defaultValue);
288 }
289
290 /**
291 * @return void
292 */
293 private function setOnlyRecipient(): void
294 {
295 $this->onlyRecipient = (bool) WCPN_Export::getChosenOrDefaultShipmentOption(
296 $this->shipmentOptions->hasOnlyRecipient(),
297 "{$this->carrier}_" . WCPOST_Settings::SETTING_CARRIER_DEFAULT_EXPORT_ONLY_RECIPIENT
298 );
299 }
300
301 /**
302 * @return void
303 * @throws \Exception
304 */
305 private function setPackageType(): void
306 {
307 $packageType = WCPOST()->export->getPackageTypeFromOrder($this->order, $this->deliveryOptions);
308 $this->packageType = $packageType;
309 }
310
311 /**
312 * @return void
313 */
314 private function setReturnShipment(): void
315 {
316 $this->returnShipment = (bool) WCPN_Export::getChosenOrDefaultShipmentOption(
317 $this->shipmentOptions->isReturn(),
318 "{$this->carrier}_" . WCPOST_Settings::SETTING_CARRIER_DEFAULT_EXPORT_RETURN
319 );
320 }
321
322 /**
323 * @return void
324 */
325 private function setSignature(): void
326 {
327 $this->signature = (bool) WCPN_Export::getChosenOrDefaultShipmentOption(
328 $this->shipmentOptions->hasSignature(),
329 "{$this->carrier}_" . WCPOST_Settings::SETTING_CARRIER_DEFAULT_EXPORT_SIGNATURE
330 );
331 }
332
333 /**
334 * @param string $settingName
335 *
336 * @return mixed
337 */
338 private function getCarrierSetting(string $settingName)
339 {
340 return WCPOST()->setting_collection->getByName("{$this->carrier}_" . $settingName);
341 }
342 }
343