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 / settings / class-wcpn-settings-data.php

class-wcpn-settings-data.php in PostNL for WooCommerce 4.0.0, at includes/admin/settings/class-wcpn-settings-data.php

956 lines 40.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 use MyParcelNL\Sdk\src\Model\Consignment\PostNLConsignment;
4 use WPO\WC\PostNL\Entity\SettingsFieldArguments;
5
6 if (! defined('ABSPATH')) {
7 exit;
8 }
9
10 if (class_exists('WCPN_Settings_Data')) {
11 return new WCPN_Settings_Data();
12 }
13
14 /**
15 * This class contains all data for the admin settings screens created by the plugin.
16 */
17 class WCPN_Settings_Data
18 {
19 public const ENABLED = "1";
20 public const DISABLED = "0";
21
22 public const DISPLAY_FOR_SELECTED_METHODS = "selected_methods";
23 public const DISPLAY_FOR_ALL_METHODS = "all_methods";
24
25
26 /**
27 * @var WCPN_Settings_Callbacks
28 */
29 private $callbacks;
30
31 public function __construct()
32 {
33 $this->callbacks = require 'class-wcpn-settings-callbacks.php';
34
35 // Create the PostNL settings with the admin_init hook.
36 add_action("admin_init", [$this, "create_all_settings"]);
37 }
38
39 /**
40 * Create all settings sections.
41 *
42 * @throws \Exception
43 */
44 public function create_all_settings(): void
45 {
46 $this->generate_settings(
47 $this->get_sections_general(),
48 WCPOST_Settings::SETTINGS_GENERAL
49 );
50
51 $this->generate_settings(
52 $this->get_sections_export_defaults(),
53 WCPOST_Settings::SETTINGS_EXPORT_DEFAULTS
54 );
55
56 $this->generate_settings(
57 $this->get_sections_checkout(),
58 WCPOST_Settings::SETTINGS_CHECKOUT
59 );
60
61 $this->generate_settings(
62 $this->get_sections_carrier_postnl(),
63 WCPOST_Settings::SETTINGS_POSTNL,
64 true
65 );
66 }
67
68 /**
69 * @return array
70 */
71 public static function getTabs(): array
72 {
73 $array = [
74 WCPOST_Settings::SETTINGS_GENERAL => __("General", "woocommerce-postnl"),
75 WCPOST_Settings::SETTINGS_EXPORT_DEFAULTS => __("Default export settings", "woocommerce-postnl"),
76 WCPOST_Settings::SETTINGS_CHECKOUT => __("Checkout settings", "woocommerce-postnl"),
77 ];
78
79 $array[WCPOST_Settings::SETTINGS_POSTNL] = __("PostNL", "woocommerce-postnl");
80
81 return $array;
82 }
83
84 /**
85 * Generate settings sections and fields by the given $settingsArray.
86 *
87 * @param array $settingsArray - Array of settings to loop through.
88 * @param string $optionName - Name to use in the identifier.
89 * @param bool $prefix - Add the key of the top level settings as prefix before every setting or not.
90 *
91 * @throws \Exception
92 */
93 private function generate_settings(array $settingsArray, string $optionName, bool $prefix = false): void
94 {
95 $optionIdentifier = WCPOST_Settings::getOptionId($optionName);
96 $defaults = [];
97
98 // Register settings.
99 register_setting($optionIdentifier, $optionIdentifier, [$this->callbacks, 'validate']);
100
101 foreach ($settingsArray as $name => $array) {
102 foreach ($array as $section) {
103 $sectionName = "{$name}_{$section["name"]}";
104
105 add_settings_section(
106 $sectionName,
107 $section["label"],
108 /**
109 * Allows a description to be shown with a section title.
110 */
111 static function() use ($section) {
112 WCPN_Settings_Callbacks::renderSection($section);
113 },
114 $optionIdentifier
115 );
116
117 foreach ($section["settings"] as $setting) {
118 $namePrefix = $prefix ? "{$name}_" : '';
119 $setting["id"] = $prefix ? "{$name}_{$setting["name"]}" : $setting["name"];
120 $setting["option_id"] = $optionIdentifier;
121
122 $class = new SettingsFieldArguments($setting, "{$optionIdentifier}[{$namePrefix}", ']');
123
124 // Add the setting's default value to the defaults array.
125 $defaults[$setting["id"]] = $class->getDefault();
126
127 if (isset(get_option($optionIdentifier)[$class->getId()])) {
128 $class->setValue(get_option($optionIdentifier)[$class->getId()]);
129 }
130
131 // Default callback
132 $callback = static function() use ($class) {
133 WCPN_Settings_Callbacks::renderField($class);
134 };
135
136 // Pass the class to custom callbacks as well.
137 if (isset($setting['callback'])) {
138 $callback = static function () use ($setting, $class) {
139 call_user_func($setting["callback"], $class);
140 };
141 }
142
143 add_settings_field(
144 $setting["id"],
145 $setting["label"],
146 $callback,
147 $optionIdentifier,
148 $sectionName,
149 // If a custom callback is used, send the $setting as arguments. Otherwise use the created
150 // arguments from the class.
151 isset($setting["callback"]) ? $setting : $class->getArguments()
152 );
153 }
154 }
155 }
156
157 // Create option in wp_options with default settings if the option doesn't exist yet.
158 if (false === get_option($optionIdentifier)) {
159 add_option($optionIdentifier, $defaults);
160 }
161
162 // Merge any missing values into the settings
163 update_option(
164 $optionIdentifier,
165 array_replace_recursive(
166 $defaults,
167 get_option($optionIdentifier)
168 )
169 );
170 }
171
172 /**
173 * @return array
174 */
175 private function get_sections_general()
176 {
177 return [
178 WCPOST_Settings::SETTINGS_GENERAL => [
179 [
180 "name" => "api",
181 "label" => __("API settings", "woocommerce-postnl"),
182 "settings" => $this->get_section_general_api(),
183 ],
184 [
185 "name" => "general",
186 "label" => __("General settings", "woocommerce-postnl"),
187 "settings" => $this->get_section_general_general(),
188 ],
189 [
190 "name" => "diagnostics",
191 "label" => __("Diagnostic tools", "woocommerce-postnl"),
192 "settings" => $this->get_section_general_diagnostics(),
193 ],
194 ],
195 ];
196 }
197
198 /**
199 * @return array
200 */
201 private function get_sections_export_defaults()
202 {
203 return [
204 WCPOST_Settings::SETTINGS_EXPORT_DEFAULTS => [
205 [
206 "name" => "main",
207 "label" => __("Default export settings", "woocommerce-postnl"),
208 "settings" => $this->get_section_export_defaults_main(),
209 ],
210 ],
211 ];
212 }
213
214 private function get_sections_checkout()
215 {
216 return [
217 WCPOST_Settings::SETTINGS_CHECKOUT => [
218 [
219 "name" => "main",
220 "label" => __("Checkout settings", "woocommerce-postnl"),
221 "settings" => $this->get_section_checkout_main(),
222 ],
223 [
224 "name" => "strings",
225 "label" => __("Titles", "woocommerce-postnl"),
226 "condition" => WCPOST_Settings::SETTING_DELIVERY_OPTIONS_ENABLED,
227 "settings" => $this->get_section_checkout_strings(),
228 ],
229 ],
230 ];
231 }
232
233 /**
234 * Get the array of PostNL sections and their settings to be added to WordPress.
235 *
236 * @return array
237 */
238 private function get_sections_carrier_postnl()
239 {
240 return [
241 PostNLConsignment::CARRIER_NAME => [
242 [
243 "name" => "export_defaults",
244 "label" => __("PostNL export settings", "woocommerce-postnl"),
245 "description" => __(
246 "These settings will be applied to PostNL shipments you create in the backend.",
247 "woocommerce-postnl"
248 ),
249 "settings" => $this->get_section_carrier_postnl_export_defaults(),
250 ],
251 [
252 "name" => "delivery_options",
253 "label" => __("PostNL delivery options", "woocommerce-postnl"),
254 "settings" => $this->get_section_carrier_postnl_delivery_options(),
255 ],
256 [
257 "name" => "pickup_options",
258 "label" => __("PostNL pickup options", "woocommerce-postnl"),
259 "settings" => $this->get_section_carrier_postnl_pickup_options(),
260 ],
261 ],
262 ];
263 }
264
265 /**
266 * @return array
267 */
268 private function get_section_general_api(): array
269 {
270 return [
271 [
272 "name" => WCPOST_Settings::SETTING_API_KEY,
273 "label" => __("Key", "woocommerce-postnl"),
274 "help_text" => __("api key", "woocommerce-postnl"),
275 ],
276 ];
277 }
278
279 /**
280 * @return array
281 */
282 private function get_section_general_general(): array
283 {
284 return [
285 [
286 "name" => WCPOST_Settings::SETTING_DOWNLOAD_DISPLAY,
287 "label" => __("Label display", "woocommerce-postnl"),
288 "type" => "select",
289 "options" => [
290 "download" => __("Download PDF", "woocommerce-postnl"),
291 "display" => __("Open the PDF in a new tab", "woocommerce-postnl"),
292 ],
293 ],
294 [
295 "name" => WCPOST_Settings::SETTING_LABEL_FORMAT,
296 "label" => __("Label format", "woocommerce-postnl"),
297 "type" => "select",
298 "options" => [
299 "A4" => __("Standard printer (A4)", "woocommerce-postnl"),
300 "A6" => __("Label Printer (A6)", "woocommerce-postnl"),
301 ],
302 ],
303 [
304 "name" => WCPOST_Settings::SETTING_ASK_FOR_PRINT_POSITION,
305 "label" => __("Ask for print start position", "woocommerce-postnl"),
306 "condition" => [
307 "parent_name" => WCPOST_Settings::SETTING_LABEL_FORMAT,
308 "type" => "disable",
309 "parent_value" => "A4",
310 "set_value" => self::DISABLED,
311 ],
312 "type" => "toggle",
313 "help_text" => __(
314 "This option enables you to continue printing where you left off last time",
315 "woocommerce-postnl"
316 ),
317 ],
318 [
319 "name" => WCPOST_Settings::SETTING_TRACK_TRACE_EMAIL,
320 "label" => __("Track & Trace in email", "woocommerce-postnl"),
321 "type" => "toggle",
322 "help_text" => __(
323 "Add the Track & Trace code to emails to the customer.<br/><strong>Note!</strong> When you select this option.",
324 "woocommerce-postnl"
325 ),
326 ],
327 [
328 "name" => WCPOST_Settings::SETTING_TRACK_TRACE_MY_ACCOUNT,
329 "label" => __("Track & Trace in My Account", "woocommerce-postnl"),
330 "type" => "toggle",
331 "help_text" => __("Show Track & Trace trace code and link in My Account.", "woocommerce-postnl"),
332 ],
333 [
334 "name" => WCPOST_Settings::SETTING_PROCESS_DIRECTLY,
335 "label" => __("Process shipments directly", "woocommerce-postnl"),
336 "type" => "toggle",
337 "help_text" => __(
338 "When you enable this option, shipments will be directly processed when sent to PostNL.",
339 "woocommerce-postnl"
340 ),
341 ],
342 [
343 "name" => WCPOST_Settings::SETTING_ORDER_STATUS_AUTOMATION,
344 "label" => __("Order status automation", "woocommerce-postnl"),
345 "type" => "toggle",
346 "help_text" => __(
347 "Automatically set order status to a predefined status after successful PostNL export.<br/>Make sure <strong>Process shipments directly</strong> is enabled when you use this option together with the <strong>Track & Trace in email</strong> option, otherwise the Track & Trace code will not be included in the customer email.",
348 "woocommerce-postnl"
349 ),
350 ],
351 [
352 "name" => WCPOST_Settings::SETTING_AUTOMATIC_ORDER_STATUS,
353 "condition" => WCPOST_Settings::SETTING_ORDER_STATUS_AUTOMATION,
354 "class" => ["wcpn__child"],
355 "label" => __("Automatic order status", "woocommerce-postnl"),
356 "type" => "select",
357 "options" => WCPN_Settings_Callbacks::get_order_status_options(),
358 ],
359 [
360 "name" => WCPOST_Settings::SETTING_BARCODE_IN_NOTE,
361 "label" => __("Place barcode inside note", "woocommerce-postnl"),
362 "type" => "toggle",
363 "help_text" => __("Place the barcode inside a note of the order", "woocommerce-postnl"),
364 ],
365 [
366 "name" => WCPOST_Settings::SETTING_BARCODE_IN_NOTE_TITLE,
367 "condition" => WCPOST_Settings::SETTING_BARCODE_IN_NOTE,
368 "class" => ["wcpn__child"],
369 "label" => __("Title before the barcode", "woocommerce-postnl"),
370 "default" => __("Track & trace code:", "woocommerce-postnl"),
371 "help_text" => __(
372 "You can change the text before the barcode inside an note",
373 "woocommerce-postnl"
374 ),
375 ],
376 ];
377 }
378
379 /**
380 * @return array
381 */
382 private function get_section_general_diagnostics(): array
383 {
384 return [
385 [
386 "name" => WCPOST_Settings::SETTING_ERROR_LOGGING,
387 "label" => __("Log API communication", "woocommerce-postnl"),
388 "type" => "toggle",
389 "description" => '<a href="' . esc_url_raw(
390 admin_url("admin.php?page=wc-status&tab=logs")
391 ) . '" target="_blank">' . __("View logs", "woocommerce-postnl") . "</a> (wc-postnl)",
392 ],
393 ];
394 }
395
396 /**
397 * Export defaults specifically for postnl.
398 *
399 * @return array
400 */
401 private function get_section_carrier_postnl_export_defaults(): array
402 {
403 return [
404 [
405 "name" => WCPOST_Settings::SETTING_CARRIER_DEFAULT_EXPORT_ONLY_RECIPIENT,
406 "label" => __("Home address only", "woocommerce-postnl"),
407 "type" => "toggle",
408 "help_text" => __(
409 "If you don't want the parcel to be delivered at the neighbours, choose this option.",
410 "woocommerce-postnl"
411 ),
412 ],
413 [
414 "name" => WCPOST_Settings::SETTING_CARRIER_DEFAULT_EXPORT_SIGNATURE,
415 "label" => __("Signature on delivery", "woocommerce-postnl"),
416 "type" => "toggle",
417 "help_text" => __(
418 "The parcel will be offered at the delivery address. If the recipient is not at home, the parcel will be delivered to the neighbours. In both cases, a signature will be required.",
419 "woocommerce-postnl"
420 ),
421 ],
422 [
423 "name" => WCPOST_Settings::SETTING_CARRIER_DEFAULT_EXPORT_AGE_CHECK,
424 "label" => __("Age check 18+", "woocommerce-postnl"),
425 "type" => "toggle",
426 "help_text" => __(
427 "The age check is intended for parcel shipments for which the recipient must show 18+ by means of a proof of identity. With this shipping option Signature for receipt and Delivery only at recipient are included. The age 18+ is further excluded from the delivery options morning and evening delivery.",
428 "woocommerce-postnl"
429 ),
430 ],
431 [
432 "name" => WCPOST_Settings::SETTING_CARRIER_DEFAULT_EXPORT_RETURN,
433 "label" => __("Return if no answer", "woocommerce-postnl"),
434 "type" => "toggle",
435 "help_text" => __(
436 "By default, a parcel will be offered twice. After two unsuccessful delivery attempts, the parcel will be available at the nearest pickup point for two weeks. There it can be picked up by the recipient with the note that was left by the courier. If you want to receive the parcel back directly and NOT forward it to the pickup point, enable this option.",
437 "woocommerce-postnl"
438 ),
439 ],
440 [
441 "name" => WCPOST_Settings::SETTING_CARRIER_DEFAULT_EXPORT_INSURED,
442 "label" => __("Insured shipment", "woocommerce-postnl"),
443 "type" => "toggle",
444 "help_text" => __(
445 "By default, there is no insurance on the shipments. If you still want to insure the shipment, you can do that. We insure the purchase value of the shipment, with a maximum insured value of € 5.000. Insured parcels always contain the options 'Home address only' en 'Signature for delivery'",
446 "woocommerce-postnl"
447 ),
448 ],
449 [
450 "name" => WCPOST_Settings::SETTING_CARRIER_DEFAULT_EXPORT_INSURED_FROM_PRICE,
451 "condition" => WCPOST_Settings::SETTING_CARRIER_DEFAULT_EXPORT_INSURED,
452 "label" => __("Insure from price", "woocommerce-postnl"),
453 "type" => "number",
454 "help_text" => __(
455 "Insure all orders that exceed this price point.",
456 "woocommerce-postnl"
457 ),
458 ],
459 [
460 "name" => WCPOST_Settings::SETTING_CARRIER_DEFAULT_EXPORT_INSURED_AMOUNT,
461 "condition" => WCPOST_Settings::SETTING_CARRIER_DEFAULT_EXPORT_INSURED,
462 "label" => __("Max insured amount", "woocommerce-postnl"),
463 "type" => "select",
464 "options" => WCPN_Data::getInsuranceAmounts(),
465 "help_text" => __(
466 "Insure all parcels up to the selected amount.",
467 "woocommerce-postnl"
468 ),
469 ],
470 ];
471 }
472
473 /**
474 * These are the unprefixed settings for postnl.
475 * After the settings are generated every name will be prefixed with "postnl_"
476 * Example: delivery_enabled => postnl_delivery_enabled
477 *
478 * @return array
479 */
480 private function get_section_carrier_postnl_delivery_options(): array
481 {
482 return [
483 [
484 "name" => WCPOST_Settings::SETTING_CARRIER_DELIVERY_ENABLED,
485 "label" => __("Enable PostNL delivery", "woocommerce-postnl"),
486 "type" => "toggle",
487 ],
488 [
489 "name" => WCPOST_Settings::SETTING_CARRIER_DROP_OFF_DAYS,
490 "condition" => WCPOST_Settings::SETTING_CARRIER_DELIVERY_ENABLED,
491 "label" => __("Drop-off days", "woocommerce-postnl"),
492 "callback" => [WCPN_Settings_Callbacks::class, "enhanced_select"],
493 "options" => $this->getWeekdays(null),
494 "default" => [2],
495 "help_text" => __("Days of the week on which you hand over parcels to PostNL", "woocommerce-postnl"),
496 ],
497 [
498 "name" => WCPOST_Settings::SETTING_CARRIER_CUTOFF_TIME,
499 "condition" => WCPOST_Settings::SETTING_CARRIER_DELIVERY_ENABLED,
500 "label" => __("Cut-off time", "woocommerce-postnl"),
501 "help_text" => __(
502 "Time at which you stop processing orders for the day (format: hh:mm)",
503 "woocommerce-postnl"
504 ),
505 "default" => "17:00",
506 ],
507 [
508 "name" => WCPOST_Settings::SETTING_CARRIER_DROP_OFF_DELAY,
509 "condition" => WCPOST_Settings::SETTING_CARRIER_DELIVERY_ENABLED,
510 "label" => __("Drop-off delay", "woocommerce-postnl"),
511 "type" => "number",
512 "max" => 14,
513 "help_text" => __("Number of days you need to process an order.", "woocommerce-postnl"),
514 ],
515 [
516 "name" => WCPOST_Settings::SETTING_CARRIER_DELIVERY_DAYS_WINDOW,
517 "condition" => WCPOST_Settings::SETTING_CARRIER_DELIVERY_ENABLED,
518 "label" => __("Delivery days window", "woocommerce-postnl"),
519 "type" => "number",
520 "max" => 14,
521 "default" => self::ENABLED,
522 "help_text" => __(
523 "Amount of days a customer can postpone a shipment. Default is 0 days with a maximum value of 14 days.",
524 "woocommerce-postnl"
525 ),
526 ],
527 [
528 "name" => WCPOST_Settings::SETTING_CARRIER_DELIVERY_MORNING_ENABLED,
529 "condition" => WCPOST_Settings::SETTING_CARRIER_DELIVERY_ENABLED,
530 "label" => __("Morning delivery", "woocommerce-postnl"),
531 "type" => "toggle",
532 ],
533 self::getFeeField(
534 WCPOST_Settings::SETTING_CARRIER_DELIVERY_MORNING_FEE,
535 [
536 WCPOST_Settings::SETTING_CARRIER_DELIVERY_ENABLED,
537 WCPOST_Settings::SETTING_CARRIER_DELIVERY_MORNING_ENABLED,
538 ]
539 ),
540 [
541 "name" => WCPOST_Settings::SETTING_CARRIER_DELIVERY_EVENING_ENABLED,
542 "condition" => WCPOST_Settings::SETTING_CARRIER_DELIVERY_ENABLED,
543 "label" => __("Evening delivery", "woocommerce-postnl"),
544 "type" => "toggle",
545 ],
546 self::getFeeField(
547 WCPOST_Settings::SETTING_CARRIER_DELIVERY_EVENING_FEE,
548 [
549 WCPOST_Settings::SETTING_CARRIER_DELIVERY_ENABLED,
550 WCPOST_Settings::SETTING_CARRIER_DELIVERY_EVENING_ENABLED,
551 ]
552 ),
553 [
554 "name" => WCPOST_Settings::SETTING_CARRIER_ONLY_RECIPIENT_ENABLED,
555 "condition" => WCPOST_Settings::SETTING_CARRIER_DELIVERY_ENABLED,
556 "label" => __("Home address only", "woocommerce-postnl"),
557 "type" => "toggle",
558 ],
559 self::getFeeField(
560 WCPOST_Settings::SETTING_CARRIER_ONLY_RECIPIENT_FEE,
561 [
562 WCPOST_Settings::SETTING_CARRIER_DELIVERY_ENABLED,
563 WCPOST_Settings::SETTING_CARRIER_ONLY_RECIPIENT_ENABLED,
564 ]
565 ),
566 [
567 "name" => WCPOST_Settings::SETTING_CARRIER_SIGNATURE_ENABLED,
568 "condition" => WCPOST_Settings::SETTING_CARRIER_DELIVERY_ENABLED,
569 "label" => __("Signature on delivery", "woocommerce-postnl"),
570 "type" => "toggle",
571 "help_text" => __(
572 "Enter an amount that is either positive or negative. For example, do you want to give a discount for using this function or do you want to charge extra for this delivery option.",
573 "woocommerce-postnl"
574 ),
575 ],
576 self::getFeeField(
577 WCPOST_Settings::SETTING_CARRIER_SIGNATURE_FEE,
578 [
579 WCPOST_Settings::SETTING_CARRIER_DELIVERY_ENABLED,
580 WCPOST_Settings::SETTING_CARRIER_SIGNATURE_ENABLED,
581 ]
582 ),
583 [
584 "name" => WCPOST_Settings::SETTING_CARRIER_MONDAY_DELIVERY_ENABLED,
585 "condition" => WCPOST_Settings::SETTING_CARRIER_DELIVERY_ENABLED,
586 "label" => __("Monday delivery", "woocommerce-postnl"),
587 "type" => "toggle",
588 ],
589 [
590 "name" => WCPOST_Settings::SETTING_CARRIER_SATURDAY_CUTOFF_TIME,
591 "condition" => [
592 WCPOST_Settings::SETTING_CARRIER_DELIVERY_ENABLED,
593 WCPOST_Settings::SETTING_CARRIER_MONDAY_DELIVERY_ENABLED,
594 ],
595 "class" => ["wcpn__child"],
596 "label" => __("Cut-off time on Saturday", "woocommerce-postnl"),
597 "placeholder" => "14:30",
598 "default" => "15:00",
599 "help_text" => __(
600 "Time at which you stop processing orders for the day (format: hh:mm)",
601 "woocommerce-postnl"
602 ),
603 ],
604 ];
605 }
606
607 /**
608 * @return array
609 */
610 private function get_section_carrier_postnl_pickup_options(): array
611 {
612 return [
613 [
614 "name" => WCPOST_Settings::SETTING_CARRIER_PICKUP_ENABLED,
615 "label" => __("Enable PostNL pickup", "woocommerce-postnl"),
616 "type" => "toggle",
617 ],
618 self::getFeeField(
619 WCPOST_Settings::SETTING_CARRIER_PICKUP_FEE,
620 [
621 WCPOST_Settings::SETTING_CARRIER_PICKUP_ENABLED,
622 ]
623 ),
624 ];
625 }
626
627 /**
628 * @return array
629 */
630 private function get_section_export_defaults_main()
631 {
632 return [
633 [
634 "name" => WCPOST_Settings::SETTING_SHIPPING_METHODS_PACKAGE_TYPES,
635 "label" => __("Package types", "woocommerce-postnl"),
636 "callback" => [WCPN_Settings_Callbacks::class, "enhanced_select"],
637 "loop" => WCPN_Data::getPackageTypesHuman(),
638 "options" => (new WCPN_Shipping_Methods())->getShippingMethods(),
639 "default" => [],
640 "help_text" => __(
641 "Select one or more shipping methods for each PostNL package type",
642 "woocommerce-postnl"
643 ),
644 ],
645 [
646 "name" => WCPOST_Settings::SETTING_CONNECT_PHONE,
647 "label" => __("Connect customer phone", "woocommerce-postnl"),
648 "type" => "toggle",
649 "help_text" => __(
650 "When you connect the customer's phone number, the courier can use this for the delivery of the parcel. This greatly increases the delivery success rate for foreign shipments.",
651 "woocommerce-postnl"
652 ),
653 ],
654 [
655 "name" => WCPOST_Settings::SETTING_LABEL_DESCRIPTION,
656 "label" => __("Label description", "woocommerce-postnl"),
657 "help_text" => __(
658 "With this option you can add a description to the shipment. This will be printed on the top left of the label. Because of limited space on the label which varies per package type, we recommend that you keep the label description as short as possible.",
659 "woocommerce-postnl"
660 ),
661 "append" => $this->getLabelDescriptionAddition(),
662 ],
663 [
664 "name" => WCPOST_Settings::SETTING_EMPTY_PARCEL_WEIGHT,
665 "label" => __("Empty parcel weight (grams)", "woocommerce-postnl"),
666 "help_text" => __(
667 "Default weight of your empty parcel, rounded to grams.",
668 "woocommerce-postnl"
669 ),
670 ],
671 [
672 "name" => WCPOST_Settings::SETTING_HS_CODE,
673 "label" => __("Default HS Code", "woocommerce-postnl"),
674 "help_text" => __(
675 "HS Codes are used for PostNL world shipments, you can find the appropriate code on the site of the Dutch Customs.",
676 "woocommerce-postnl"
677 ),
678 ],
679 [
680 "name" => WCPOST_Settings::SETTING_PACKAGE_CONTENT,
681 "label" => __("Customs shipment type", "woocommerce-postnl"),
682 "type" => "select",
683 "options" => [
684 1 => __("Commercial goods", "woocommerce-postnl"),
685 2 => __("Commercial samples", "woocommerce-postnl"),
686 3 => __("Documents", "woocommerce-postnl"),
687 4 => __("Gifts", "woocommerce-postnl"),
688 5 => __("Return shipment", "woocommerce-postnl"),
689 ],
690 ],
691 [
692 "name" => WCPOST_Settings::SETTING_COUNTRY_OF_ORIGIN,
693 "label" => __("Default country of origin", "woocommerce-postnl"),
694 "type" => "select",
695 "options" => (new WC_Countries())->get_countries(),
696 "help-text" => __(
697 "Country of origin is required for world shipments. Defaults to shop base or NL. Example: 'NL', 'BE', 'DE'", "woocommerce-postnl"
698 ),
699 ],
700 ];
701 }
702
703 /**
704 * @return array
705 */
706 private function get_section_checkout_main(): array
707 {
708 return [
709 [
710 "name" => WCPOST_Settings::SETTING_USE_SPLIT_ADDRESS_FIELDS,
711 "label" => __("PostNL address fields", "woocommerce-postnl"),
712 "type" => "toggle",
713 "help_text" => __(
714 "When enabled the checkout will use the PostNL address fields. This means there will be three separate fields for street name, number and suffix. Want to use the WooCommerce default fields? Leave this option unchecked.",
715 "woocommerce-postnl"
716 ),
717 ],
718 [
719 "name" => WCPOST_Settings::SETTING_SHOW_DELIVERY_DAY,
720 "label" => __("Show delivery day", "woocommerce-postnl"),
721 "type" => "toggle",
722 "help_text" => __(
723 "Show delivery day options allow your customers to see the delivery day in order confirmation and My Account.",
724 "woocommerce-postnl"
725 ),
726 ],
727 [
728 "name" => WCPOST_Settings::SETTING_DELIVERY_OPTIONS_ENABLED,
729 "label" => __("Enable PostNL delivery options", "woocommerce-postnl"),
730 "type" => "toggle",
731 "help_text" => __(
732 "The PostNL delivery options allow your customers to select whether they want their parcel delivered at home or to a pickup point. Depending on the settings you can allow them to select a date, time and even options like requiring a signature on delivery.",
733 "woocommerce-postnl"
734 ),
735 ],
736 [
737 "name" => WCPOST_Settings::SETTING_DELIVERY_OPTIONS_DISPLAY,
738 "condition" => WCPOST_Settings::SETTING_DELIVERY_OPTIONS_ENABLED,
739 "label" => __("Display for", "woocommerce-postnl"),
740 "type" => "select",
741 "help_text" => __(
742 "You can link the delivery options to specific shipping methods by adding them to the package types under \"Standard export settings\". The delivery options are not visible at foreign addresses.",
743 "woocommerce-postnl"
744 ),
745 "options" => [
746 self::DISPLAY_FOR_SELECTED_METHODS => __(
747 "Shipping methods associated with Parcels",
748 "woocommerce-postnl"
749 ),
750 self::DISPLAY_FOR_ALL_METHODS => __("All shipping methods", "woocommerce-postnl"),
751 ],
752 ],
753 [
754 "name" => WCPOST_Settings::SETTING_DELIVERY_OPTIONS_POSITION,
755 "condition" => WCPOST_Settings::SETTING_DELIVERY_OPTIONS_ENABLED,
756 "label" => __("Checkout position", "woocommerce-postnl"),
757 "type" => "select",
758 "default" => "woocommerce_after_checkout_billing_form",
759 "options" => [
760 "woocommerce_after_checkout_billing_form" => __(
761 "Show after billing details",
762 "woocommerce-postnl"
763 ),
764 "woocommerce_after_checkout_shipping_form" => __(
765 "Show after shipping details",
766 "woocommerce-postnl"
767 ),
768 "woocommerce_checkout_after_customer_details" => __(
769 "Show after customer details",
770 "woocommerce-postnl"
771 ),
772 "woocommerce_after_order_notes" => __(
773 "Show after notes",
774 "woocommerce-postnl"
775 ),
776 "woocommerce_review_order_before_payment" => __(
777 "Show after subtotal",
778 "woocommerce-postnl"
779 ),
780 ],
781 "help_text" => __(
782 "You can change the place of the delivery options on the checkout page. By default it will be placed after shipping details.",
783 "woocommerce-postnl"
784 ),
785 ],
786 [
787 "name" => WCPOST_Settings::SETTING_DELIVERY_OPTIONS_CUSTOM_CSS,
788 "condition" => WCPOST_Settings::SETTING_DELIVERY_OPTIONS_ENABLED,
789 "label" => __("Custom styles", "woocommerce-postnl"),
790 "type" => "textarea",
791 "append" => $this->getCustomCssAddition(),
792 "custom_attributes" => [
793 "style" => "font-family: monospace;",
794 "rows" => "8",
795 "cols" => "12",
796 ],
797 ],
798 ];
799 }
800
801 /**
802 * Get the weekdays from WP_Locale and remove any entries. Sunday is removed by default unless `null` is passed.
803 *
804 * @param int|null ...$remove
805 *
806 * @return array
807 */
808 private function getWeekdays(...$remove): array
809 {
810 $weekdays = (new WP_Locale())->weekday;
811
812 if ($remove !== null) {
813 $remove = count($remove) ? $remove : [0];
814 foreach ($remove as $index) {
815 unset($weekdays[$index]);
816 }
817 }
818
819 return $weekdays;
820 }
821
822 private function get_section_checkout_strings(): array
823 {
824 return [
825 [
826 "name" => WCPOST_Settings::SETTING_HEADER_DELIVERY_OPTIONS_TITLE,
827 "condition" => WCPOST_Settings::SETTING_DELIVERY_OPTIONS_ENABLED,
828 "label" => __("Delivery options title", "woocommerce-postnl"),
829 "title" => "Delivery options title",
830 "help_text" => __(
831 "You can place a delivery title above the PostNL options. When there is no title, it will not be visible.",
832 "woocommerce-postnl"
833 ),
834 ],
835 [
836 "name" => WCPOST_Settings::SETTING_DELIVERY_TITLE,
837 "condition" => WCPOST_Settings::SETTING_DELIVERY_OPTIONS_ENABLED,
838 "label" => __("Delivery title", "woocommerce-postnl"),
839 "default" => __("Delivered at home or at work", "woocommerce-postnl"),
840 ],
841 [
842 "name" => WCPOST_Settings::SETTING_MORNING_DELIVERY_TITLE,
843 "condition" => WCPOST_Settings::SETTING_DELIVERY_OPTIONS_ENABLED,
844 "label" => __("Morning delivery title", "woocommerce-postnl"),
845 "default" => __("Morning delivery", "woocommerce-postnl"),
846 ],
847 [
848 "name" => WCPOST_Settings::SETTING_STANDARD_TITLE,
849 "condition" => WCPOST_Settings::SETTING_DELIVERY_OPTIONS_ENABLED,
850 "label" => __("Standard delivery title", "woocommerce-postnl"),
851 "help_text" => __(
852 "When there is no title, the delivery time will automatically be visible.",
853 "woocommerce-postnl"
854 ),
855 "default" => __("Standard delivery", "woocommerce-postnl"),
856 ],
857 [
858 "name" => WCPOST_Settings::SETTING_EVENING_DELIVERY_TITLE,
859 "condition" => WCPOST_Settings::SETTING_DELIVERY_OPTIONS_ENABLED,
860 "label" => __("Evening delivery title", "woocommerce-postnl"),
861 "default" => __("Evening delivery", "woocommerce-postnl"),
862 ],
863 [
864 "name" => WCPOST_Settings::SETTING_ONLY_RECIPIENT_TITLE,
865 "condition" => WCPOST_Settings::SETTING_DELIVERY_OPTIONS_ENABLED,
866 "label" => __("Home address only title", "woocommerce-postnl"),
867 "default" => __("Home address only", "woocommerce-postnl"),
868 ],
869 [
870 "name" => WCPOST_Settings::SETTING_SIGNATURE_TITLE,
871 "condition" => WCPOST_Settings::SETTING_DELIVERY_OPTIONS_ENABLED,
872 "label" => __("Signature on delivery title", "woocommerce-postnl"),
873 "default" => __("Signature on delivery", "woocommerce-postnl"),
874 ],
875 [
876 "name" => WCPOST_Settings::SETTING_PICKUP_TITLE,
877 "condition" => WCPOST_Settings::SETTING_DELIVERY_OPTIONS_ENABLED,
878 "label" => __("Pickup title", "woocommerce-postnl"),
879 "default" => __("Pickup", "woocommerce-postnl"),
880 ],
881 ];
882 }
883
884 /**
885 * Get the html string to render after the custom css select.
886 *
887 * @return string
888 */
889 private function getCustomCssAddition(): string
890 {
891 $currentTheme = wp_get_theme();
892
893 $preset = sanitize_title($currentTheme);
894 $cssPath = WCPOST()->plugin_path() . "/assets/css/delivery-options/delivery-options-preset-$preset.css";
895
896 if (! file_exists($cssPath)) {
897 return "";
898 }
899
900 return sprintf(
901 '<p>%s <a class="" href="#" onclick="document.querySelector(`#delivery_options_custom_css`).value = `%s`">%s</a></p>',
902 sprintf(__("Theme \"%s\" detected.", "woocommerce-postnl"), $currentTheme),
903 file_get_contents($cssPath),
904 __("Apply preset.", "woocommerce-postnl")
905 );
906 }
907
908 /**
909 * Created html for clickable hints for the variables that can be used in the label description.
910 *
911 * @return string
912 */
913 private function getLabelDescriptionAddition(): string
914 {
915 $output = '';
916 $variables = [
917 '[DELIVERY_DATE]' => __('Delivery date', 'woocommerce-postnl'),
918 '[ORDER_NR]' => __('Order number', 'woocommerce-postnl'),
919 '[PRODUCT_ID]' => __('Product id', 'woocommerce-postnl'),
920 '[PRODUCT_NAME]' => __('Product name', 'woocommerce-postnl'),
921 '[PRODUCT_QTY]' => __('Product quantity', 'woocommerce-postnl'),
922 '[PRODUCT_SKU]' => __('Product SKU', 'woocommerce-postnl'),
923 '[CUSTOMER_NOTE]' => __('Customer note', 'woocommerce-postnl'),
924 ];
925
926 foreach ($variables as $variable => $description) {
927 $output .= "<br><a onclick=\"var el = document.querySelector('#label_description_field input');el.value += '$variable';el.focus();\">$variable</a>: $description";
928 }
929
930 return sprintf("<div class=\"label-description-variables\"><p>Available variables: %s</p>", $output);
931 }
932
933 /**
934 * @param string $name
935 * @param string|array $conditions
936 *
937 * @return array
938 */
939 private static function getFeeField(string $name, array $conditions): array
940 {
941 return [
942 "name" => $name,
943 "condition" => $conditions,
944 "class" => ["wcpn__child"],
945 "label" => __("Fee (optional)", "woocommerce-postnl"),
946 "type" => "currency",
947 "help_text" => __(
948 "Enter an amount that is either positive or negative. For example, do you want to give a discount for using this function or do you want to charge extra for this delivery option.",
949 "woocommerce-postnl"
950 ),
951 ];
952 }
953 }
954
955 new WCPN_Settings_Data();
956