PluginProbe
PostNL for WooCommerce / 4.4.1
PostNL for WooCommerce v4.4.1
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 / OrderSettingsRows.php

OrderSettingsRows.php in PostNL for WooCommerce 4.4.1, at includes/admin/OrderSettingsRows.php

323 lines 13.9 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 namespace PostNL\WooCommerce\Includes\Admin;
6
7 use MyParcelNL\Sdk\src\Adapter\DeliveryOptions\AbstractDeliveryOptionsAdapter;
8 use MyParcelNL\Sdk\src\Model\Consignment\AbstractConsignment;
9 use MyParcelNL\Sdk\src\Model\Consignment\PostNLConsignment;
10 use OrderSettings;
11 use WC_Order;
12 use WCPN_Country_Codes;
13 use WCPN_Data;
14 use WCPN_Export;
15 use WCPN_Settings_Data;
16
17 class OrderSettingsRows
18 {
19 private const HOME_COUNTRY_ONLY_ROWS = [
20 self::OPTION_SHIPMENT_OPTIONS_AGE_CHECK,
21 self::OPTION_SHIPMENT_OPTIONS_ONLY_RECIPIENT,
22 self::OPTION_SHIPMENT_OPTIONS_SIGNATURE,
23 ];
24
25 private const OPTION_CARRIER = "[carrier]";
26 private const OPTION_DELIVERY_TYPE = "[delivery_type]";
27 private const OPTION_EXTRA_OPTIONS_COLLO_AMOUNT = "[extra_options][collo_amount]";
28 private const OPTION_EXTRA_OPTIONS_DIGITAL_STAMP_WEIGHT = "[extra_options][digital_stamp_weight]";
29 private const OPTION_PACKAGE_TYPE = "[package_type]";
30 private const OPTION_SHIPMENT_OPTIONS_INSURED = "[shipment_options][insured]";
31 private const OPTION_SHIPMENT_OPTIONS_INSURED_AMOUNT = "[shipment_options][insured_amount]";
32 private const OPTION_SHIPMENT_OPTIONS_LABEL_DESCRIPTION = "[shipment_options][label_description]";
33 private const OPTION_SHIPMENT_OPTIONS_LARGE_FORMAT = "[shipment_options][large_format]";
34 private const OPTION_SHIPMENT_OPTIONS_ONLY_RECIPIENT = "[shipment_options][only_recipient]";
35 private const OPTION_SHIPMENT_OPTIONS_RETURN_SHIPMENT = "[shipment_options][return_shipment]";
36 private const OPTION_SHIPMENT_OPTIONS_SIGNATURE = "[shipment_options][signature]";
37 private const OPTION_SHIPMENT_OPTIONS_AGE_CHECK = "[shipment_options][age_check]";
38
39 private const CONDITION_CARRIER_DEFAULT = [
40 "parent_name" => self::OPTION_CARRIER,
41 "type" => "show",
42 "parent_value" => WCPN_Data::DEFAULT_CARRIER,
43 "set_value" => WCPN_Settings_Data::DISABLED,
44 ];
45
46 private const CONDITION_DELIVERY_TYPE_DELIVERY = [
47 "parent_name" => self::OPTION_DELIVERY_TYPE,
48 "type" => "show",
49 "parent_value" => [
50 AbstractConsignment::DELIVERY_TYPE_MORNING_NAME,
51 AbstractConsignment::DELIVERY_TYPE_STANDARD_NAME,
52 AbstractConsignment::DELIVERY_TYPE_EVENING_NAME,
53 ],
54 "set_value" => WCPN_Settings_Data::DISABLED,
55 ];
56
57 private const CONDITION_PACKAGE_TYPE_PACKAGE = [
58 "parent_name" => self::OPTION_PACKAGE_TYPE,
59 "type" => "show",
60 "parent_value" => AbstractConsignment::PACKAGE_TYPE_PACKAGE_NAME,
61 ];
62
63 private const CONDITION_FORCE_ENABLED_ON_AGE_CHECK = [
64 "parent_name" => self::OPTION_SHIPMENT_OPTIONS_AGE_CHECK,
65 "type" => "disable",
66 "set_value" => WCPN_Settings_Data::ENABLED,
67 "parent_value" => WCPN_Settings_Data::DISABLED,
68 ];
69
70 /**
71 * @param \MyParcelNL\Sdk\src\Adapter\DeliveryOptions\AbstractDeliveryOptionsAdapter $deliveryOptions
72 * @param \WC_Order $order
73 *
74 * @return array[]
75 * @throws \Exception
76 */
77 public static function getOptionsRows(
78 AbstractDeliveryOptionsAdapter $deliveryOptions,
79 WC_Order $order
80 ): array {
81 $orderSettings = new OrderSettings($order, $deliveryOptions);
82 $shippingCountry = $orderSettings->getShippingCountry();
83 $isEuCountry = WCPN_Country_Codes::isEuCountry($shippingCountry);
84 $isBelgium = AbstractConsignment::CC_BE === $shippingCountry;
85 $isHomeCountry = WCPN_Data::isHomeCountry($shippingCountry);
86 $packageTypeOptions = array_combine(WCPN_Data::getPackageTypes(), WCPN_Data::getPackageTypesHuman());
87
88 // Remove mailbox, because this is not possible for international shipments
89 if (! $isHomeCountry) {
90 unset($packageTypeOptions['mailbox']);
91 }
92
93 $rows = [
94 [
95 "name" => self::OPTION_CARRIER,
96 "label" => __("Carrier", "woocommerce-postnl"),
97 "type" => "select",
98 "options" => WCPN_Data::CARRIERS_HUMAN,
99 "custom_attributes" => ["disabled" => "disabled"],
100 "value" => $deliveryOptions->getCarrier() ?? PostNLConsignment::CARRIER_NAME,
101 ],
102 [
103 "name" => self::OPTION_DELIVERY_TYPE,
104 "label" => __("Delivery type", "woocommerce-postnl"),
105 "type" => "select",
106 "options" => WCPN_Data::getDeliveryTypesHuman(),
107 "custom_attributes" => ["disabled" => "disabled"],
108 "value" => $deliveryOptions->getDeliveryType(),
109 ],
110 [
111 "name" => self::OPTION_PACKAGE_TYPE,
112 "label" => __("Shipment type", "woocommerce-postnl"),
113 "type" => "select",
114 "options" => $packageTypeOptions,
115 "value" => WCPOST()->export->getPackageTypeFromOrder($order, $deliveryOptions),
116 ],
117 [
118 "name" => self::OPTION_EXTRA_OPTIONS_COLLO_AMOUNT,
119 "label" => __("Number of labels", "woocommerce-postnl"),
120 "type" => "number",
121 "value" => $orderSettings->getColloAmount(),
122 "custom_attributes" => [
123 "min" => "1",
124 "max" => "10",
125 ],
126 ],
127 ];
128
129 // Only add extra options and shipment options to home country shipments.
130 if ($isHomeCountry) {
131 $rows = array_merge($rows, self::getAdditionalOptionsRows($orderSettings));
132 }
133
134 if ($isBelgium) {
135 $rows[] = [
136 'name' => self::OPTION_SHIPMENT_OPTIONS_INSURED,
137 'type' => 'toggle',
138 'label' => __('insured', 'woocommerce-postnl'),
139 'value' => $orderSettings->isInsured(),
140 'condition' => [
141 self::CONDITION_PACKAGE_TYPE_PACKAGE,
142 self::CONDITION_DELIVERY_TYPE_DELIVERY,
143 [
144 'parent_name' => self::OPTION_CARRIER,
145 'type' => 'disable',
146 'parent_value' => WCPN_Data::DEFAULT_CARRIER,
147 'set_value' => WCPN_Settings_Data::DISABLED,
148 ],
149 ],
150 ];
151
152 $rows[] = [
153 'name' => self::OPTION_SHIPMENT_OPTIONS_INSURED_AMOUNT,
154 'type' => 'select',
155 'label' => __('insured_amount', 'woocommerce-postnl'),
156 'options' => [$orderSettings::DEFAULT_BELGIAN_INSURANCE => $orderSettings::DEFAULT_BELGIAN_INSURANCE],
157 'value' => $orderSettings->getInsuranceAmount(),
158 'condition' => [
159 self::OPTION_SHIPMENT_OPTIONS_INSURED,
160 ],
161 ];
162 }
163
164 // if ($isEuCountry) {
165 // $rows[] = [
166 // "name" => self::OPTION_SHIPMENT_OPTIONS_LARGE_FORMAT,
167 // "type" => "toggle",
168 // "label" => __("shipment_options_large_format", "woocommerce-postnl"),
169 // "help_text" => __("shipment_options_large_format_help_text", "woocommerce-postnl"),
170 // "value" => $orderSettings->hasLargeFormat(),
171 // "condition" => [
172 // self::CONDITION_PACKAGE_TYPE_PACKAGE,
173 // self::CONDITION_DELIVERY_TYPE_DELIVERY,
174 // self::CONDITION_CARRIER_DEFAULT,
175 // ],
176 // ];
177 // }
178
179 $rows[] = [
180 "name" => self::OPTION_SHIPMENT_OPTIONS_LABEL_DESCRIPTION,
181 "type" => "text",
182 "label" => __("Custom ID (top left on label)", "woocommerce-postnl"),
183 "value" => $orderSettings->getLabelDescription(),
184 ];
185
186 return $rows;
187 }
188
189 /**
190 * Filters out rows that should not be shown if the shipment is sent to the home country.
191 *
192 * @param string $cc
193 * @param array $rows
194 *
195 * @return array
196 */
197 public static function filterRowsByCountry(string $cc, array $rows): array
198 {
199 if (WCPN_Data::DEFAULT_COUNTRY_CODE === $cc) {
200 return $rows;
201 }
202
203 return array_filter(
204 $rows,
205 function ($row) {
206 return ! in_array($row['name'], self::HOME_COUNTRY_ONLY_ROWS);
207 }
208 );
209 }
210
211 /**
212 * @param \OrderSettings $orderSettings
213 *
214 * @return array[]
215 */
216 private static function getAdditionalOptionsRows(OrderSettings $orderSettings): array
217 {
218 return [
219 [
220 "name" => self::OPTION_EXTRA_OPTIONS_DIGITAL_STAMP_WEIGHT,
221 "type" => "select",
222 "label" => __("weight", "woocommerce-postnl"),
223 "description" => sprintf(
224 __("calculated_order_weight", "woocommerce-postnl"),
225 wc_format_weight($orderSettings->getWeight())
226 ),
227 "options" => WCPN_Export::getDigitalStampRangeOptions(),
228 "value" => $orderSettings->getDigitalStampRangeWeight(),
229 "condition" => [
230 [
231 "parent_name" => self::OPTION_CARRIER,
232 "type" => "show",
233 "parent_value" => WCPN_Data::DEFAULT_CARRIER,
234 ],
235 [
236 "parent_name" => self::OPTION_PACKAGE_TYPE,
237 "type" => "show",
238 "parent_value" => AbstractConsignment::PACKAGE_TYPE_DIGITAL_STAMP_NAME,
239 ],
240 ],
241 ],
242 [
243 "name" => self::OPTION_SHIPMENT_OPTIONS_ONLY_RECIPIENT,
244 "type" => "toggle",
245 "label" => __("shipment_options_only_recipient", "woocommerce-postnl"),
246 "help_text" => __("shipment_options_only_recipient_help_text", "woocommerce-postnl"),
247 "value" => $orderSettings->hasOnlyRecipient(),
248 "condition" => [
249 self::CONDITION_PACKAGE_TYPE_PACKAGE,
250 self::CONDITION_DELIVERY_TYPE_DELIVERY,
251 self::CONDITION_CARRIER_DEFAULT,
252 self::CONDITION_FORCE_ENABLED_ON_AGE_CHECK,
253 ],
254 ],
255 [
256 "name" => self::OPTION_SHIPMENT_OPTIONS_SIGNATURE,
257 "type" => "toggle",
258 "label" => __("shipment_options_signature", "woocommerce-postnl"),
259 "help_text" => __("shipment_options_signature_help_text", "woocommerce-postnl"),
260 "value" => $orderSettings->hasSignature(),
261 "condition" => [
262 self::CONDITION_PACKAGE_TYPE_PACKAGE,
263 self::CONDITION_DELIVERY_TYPE_DELIVERY,
264 self::CONDITION_CARRIER_DEFAULT,
265 self::CONDITION_FORCE_ENABLED_ON_AGE_CHECK,
266 ],
267 ],
268 [
269 "name" => self::OPTION_SHIPMENT_OPTIONS_AGE_CHECK,
270 "type" => "toggle",
271 "label" => __("shipment_options_age_check", "woocommerce-postnl"),
272 "help_text" => __("shipment_options_age_check_help_text", "woocommerce-postnl"),
273 "value" => $orderSettings->hasAgeCheck(),
274 "condition" => [
275 self::CONDITION_PACKAGE_TYPE_PACKAGE,
276 self::CONDITION_DELIVERY_TYPE_DELIVERY,
277 self::CONDITION_CARRIER_DEFAULT,
278 ],
279 ],
280 [
281 "name" => self::OPTION_SHIPMENT_OPTIONS_RETURN_SHIPMENT,
282 "type" => "toggle",
283 "label" => __("shipment_options_return", "woocommerce-postnl"),
284 "help_text" => __("shipment_options_return_help_text", "woocommerce-postnl"),
285 "value" => $orderSettings->hasReturnShipment(),
286 "condition" => [
287 self::CONDITION_PACKAGE_TYPE_PACKAGE,
288 self::CONDITION_DELIVERY_TYPE_DELIVERY,
289 self::CONDITION_CARRIER_DEFAULT,
290 ],
291 ],
292 [
293 "name" => self::OPTION_SHIPMENT_OPTIONS_INSURED,
294 "type" => "toggle",
295 "label" => __("insured", "woocommerce-postnl"),
296 "value" => $orderSettings->isInsured(),
297 "condition" => [
298 self::CONDITION_PACKAGE_TYPE_PACKAGE,
299 self::CONDITION_DELIVERY_TYPE_DELIVERY,
300 [
301 "parent_name" => self::OPTION_CARRIER,
302 "type" => "disable",
303 "parent_value" => WCPN_Data::DEFAULT_CARRIER,
304 "set_value" => WCPN_Settings_Data::DISABLED,
305 ],
306 ],
307 ],
308 [
309 "name" => self::OPTION_SHIPMENT_OPTIONS_INSURED_AMOUNT,
310 "type" => "select",
311 "label" => __("insured_amount", "woocommerce-postnl"),
312 "options" => WCPN_Data::getInsuranceAmounts(),
313 "value" => $orderSettings->getInsuranceAmount(),
314 "condition" => [
315 self::CONDITION_PACKAGE_TYPE_PACKAGE,
316 self::CONDITION_DELIVERY_TYPE_DELIVERY,
317 self::OPTION_SHIPMENT_OPTIONS_INSURED,
318 ],
319 ],
320 ];
321 }
322 }
323