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

class-wcpn-shipping-methods.php in PostNL for WooCommerce 4.4.1, at includes/admin/settings/class-wcpn-shipping-methods.php

222 lines 7.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if (! defined('ABSPATH')) {
4 exit;
5 } // Exit if accessed directly
6
7 /**
8 * Creates the array of available shipping methods in the checkout.
9 */
10 class WCPN_Shipping_Methods
11 {
12 public const FLAT_RATE = 'flat_rate';
13 public const FLEXIBLE_SHIPPING = 'flexible_shipping';
14 public const FLEXIBLE_SHIPPING_INFO = 'flexible_shipping_info';
15 public const FREE_SHIPPING = 'free_shipping';
16 public const LEGACY_FLAT_RATE = 'legacy_flat_rate';
17 public const LOCAL_PICKUP = 'local_pickup';
18 public const TABLE_RATES_BOLDER_ELEMENTS = 'betrs_shipping';
19 public const TABLE_RATES_WOOCOMMERCE = 'table_rate';
20
21 private const SHIPPING_METHOD_CLASS_WOOCOMMERCE = 'WC_Shipping_Table_Rate';
22 private const SHIPPING_METHOD_CLASS_BOLDER_ELEMENTS = 'BE_Table_Rate_Method';
23 private const SHIPPING_METHOD_CLASS_WP_DESK_FLEXIBLE = 'WPDesk_Flexible_Shipping';
24
25 /**
26 * Items in this array will not be added to the shipping methods array. Useful for table rates because the base
27 * method "table_rate" for example, can't be selected. Only the suffixed ones "table_rate:1:3" can, so it
28 * shouldn't be in this array.
29 */
30 private const DISALLOWED_SHIPPING_METHODS = [
31 self::FLEXIBLE_SHIPPING,
32 self::FLEXIBLE_SHIPPING_INFO,
33 self::TABLE_RATES_BOLDER_ELEMENTS,
34 self::TABLE_RATES_WOOCOMMERCE,
35 ];
36
37 /**
38 * @var array
39 */
40 private $shippingMethods = [];
41
42 public function __construct()
43 {
44 $this->gatherShippingMethods();
45 }
46
47 /**
48 * @return array
49 */
50 public function getShippingMethods(): array
51 {
52 return $this->shippingMethods;
53 }
54
55 private function gatherShippingMethods(): void
56 {
57 $wooCommerceShippingMethods = WC()->shipping()->get_shipping_methods();
58
59 if (! $wooCommerceShippingMethods) {
60 return;
61 }
62
63 foreach ($wooCommerceShippingMethods as $shippingMethodId => $zoneShippingMethod) {
64 $method = $zoneShippingMethod;
65 $methodTitle = $method->method_title ?? $method->title;
66
67 $this->addShippingMethod($shippingMethodId, $methodTitle);
68 $this->addFlatRateShippingMethods($shippingMethodId, $methodTitle);
69 $this->addShippingMethodsFromShippingZones();
70 }
71 }
72
73 /**
74 * Add a shipping method. Skips disallowed entries.
75 *
76 * @param string $key
77 * @param string $value
78 */
79 private function addShippingMethod(string $key, string $value): void
80 {
81 if (in_array($key, self::DISALLOWED_SHIPPING_METHODS)) {
82 return;
83 }
84
85 $this->shippingMethods[$key] = $value;
86 }
87
88 private function addShippingMethodsFromShippingZones(): void
89 {
90 $shippingZones = WC_Shipping_Zones::get_zones();
91
92 foreach ($shippingZones as $shippingZone) {
93 $zoneId = $shippingZone['id'] ?? $shippingZone['zone_id'];
94
95 if (! $zoneId) {
96 continue;
97 }
98
99 $zone = WC_Shipping_Zones::get_zone($zoneId);
100 /* @var WC_Shipping_Method[] $zoneShippingMethods */
101 $zoneShippingMethods = $zone->get_shipping_methods();
102
103 foreach ($zoneShippingMethods as $zoneShippingMethod) {
104 $this->addZoneShippingMethodRates($zone, $zoneShippingMethod);
105 }
106 }
107 }
108
109 /**
110 * @param string $shippingMethodId
111 * @param string $methodTitle
112 */
113 private function addFlatRateShippingMethods(string $shippingMethodId, string $methodTitle): void
114 {
115 $isFlatRate = in_array($shippingMethodId, [self::FLAT_RATE, self::LEGACY_FLAT_RATE]);
116 $versionAbove2_4 = version_compare(WOOCOMMERCE_VERSION, '2.4', '>=');
117
118 // split flat rate by shipping class
119 if (! $isFlatRate || ! $versionAbove2_4) {
120 return;
121 }
122
123 /** @var WP_Term[] $shippingClasses */
124 $shippingClasses = WC()->shipping()->get_shipping_classes();
125
126 foreach ($shippingClasses as $shippingClass) {
127 if (! isset($shippingClass->term_id)) {
128 continue;
129 }
130
131 $this->addShippingMethod(
132 esc_attr($shippingMethodId) . ":" . $shippingClass->term_id,
133 esc_html("{$methodTitle} - {$shippingClass->name}")
134 );
135 }
136 }
137
138 /**
139 * @param \WC_Shipping_Zone $zone
140 * @param \WC_Shipping_Method $zoneShippingMethod
141 */
142 private function addZoneShippingMethodRates(WC_Shipping_Zone $zone, WC_Shipping_Method $zoneShippingMethod): void
143 {
144 switch (get_class($zoneShippingMethod)) {
145 case self::SHIPPING_METHOD_CLASS_WOOCOMMERCE:
146 $this->addWooCommerceZoneShippingMethodRates($zone, $zoneShippingMethod);
147 break;
148 case self::SHIPPING_METHOD_CLASS_BOLDER_ELEMENTS:
149 $this->addBolderElementsZoneShippingMethodRates($zone, $zoneShippingMethod);
150 break;
151 case self::SHIPPING_METHOD_CLASS_WP_DESK_FLEXIBLE:
152 $this->addWPDeskFlexibleZoneShippingMethodRates($zoneShippingMethod);
153 break;
154 }
155 }
156
157 /**
158 * @param WC_Shipping_Zone $zone
159 * @param WC_Shipping_Method $zoneShippingMethod
160 */
161 private function addWooCommerceZoneShippingMethodRates(
162 WC_Shipping_Zone $zone,
163 WC_Shipping_Method $zoneShippingMethod
164 ): void {
165 foreach ($zoneShippingMethod->get_shipping_rates() as $zoneShippingRate) {
166 $label = $zoneShippingRate->rate_label ?? "{$zoneShippingMethod->title} ({$zoneShippingRate->rate_id})";
167
168 $this->addShippingMethod(
169 self::TABLE_RATES_WOOCOMMERCE . ":{$zoneShippingMethod->instance_id}:{$zoneShippingRate->rate_id}",
170 "{$zone->get_zone_name()} - {$label}"
171 );
172 }
173 }
174
175 /**
176 * @param WC_Shipping_Zone $zone
177 * @param WC_Shipping_Method $zoneShippingMethod
178 */
179 private function addBolderElementsZoneShippingMethodRates(
180 WC_Shipping_Zone $zone,
181 WC_Shipping_Method $zoneShippingMethod
182 ): void {
183 $shippingMethodOption = get_option($zoneShippingMethod->id . '_options-' . $zoneShippingMethod->instance_id);
184
185 if (! isset($shippingMethodOption['settings'])) {
186 return;
187 }
188
189 foreach ($shippingMethodOption['settings'] as $zoneShippingRate) {
190 $optionId = $zoneShippingRate['option_id'];
191 $label = $zoneShippingRate['title'] ?? "{$zoneShippingMethod->title} ({$optionId})";
192
193 /*
194 * It appears that after version 4.0.0 the separator changed from "_" to ":". We're adding both variants
195 * here for compatibility.
196 */
197 foreach ([':', '_'] as $separator) {
198 $this->addShippingMethod(
199 self::TABLE_RATES_BOLDER_ELEMENTS . "$separator{$zoneShippingMethod->instance_id}-{$optionId}",
200 "{$zone->get_zone_name()} - {$label}"
201 );
202 }
203 }
204 }
205
206 /**
207 * @param \WC_Shipping_Method $zoneShippingMethod
208 */
209 private function addWPDeskFlexibleZoneShippingMethodRates(WC_Shipping_Method $zoneShippingMethod): void
210 {
211 $shippingMethodOption = get_option($zoneShippingMethod->shipping_methods_option);
212
213 if (! $shippingMethodOption) {
214 return;
215 }
216
217 foreach ($shippingMethodOption as $item) {
218 $this->addShippingMethod($item['id_for_shipping'], $item['method_title']);
219 }
220 }
221 }
222