| 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 FREE_SHIPPING = 'free_shipping'; |
| 14 |
public const LOCAL_PICKUP = 'local_pickup'; |
| 15 |
public const TABLE_RATES_BOLDER_ELEMENTS = 'betrs_shipping'; |
| 16 |
public const TABLE_RATES_WOOCOMMERCE = 'table_rate'; |
| 17 |
public const LEGACY_FLAT_RATE = 'legacy_flat_rate'; |
| 18 |
|
| 19 |
private const SHIPPING_METHOD_CLASS_BETRS = 'BE_Table_Rate_Method'; |
| 20 |
private const SHIPPING_METHOD_CLASS_WC = 'WC_Shipping_Table_Rate'; |
| 21 |
|
| 22 |
/** |
| 23 |
* Items in this array will not be added to the shipping methods array. Useful for table rates because the base |
| 24 |
* method "table_rate" for example, can't be selected. Only the suffixed ones "table_rate:1:3" can, so it |
| 25 |
* shouldn't be in this array. |
| 26 |
*/ |
| 27 |
private const DISALLOWED_SHIPPING_METHODS = [ |
| 28 |
self::TABLE_RATES_BOLDER_ELEMENTS, |
| 29 |
self::TABLE_RATES_WOOCOMMERCE, |
| 30 |
]; |
| 31 |
|
| 32 |
/** |
| 33 |
* @var array |
| 34 |
*/ |
| 35 |
private $shippingMethods = []; |
| 36 |
|
| 37 |
public function __construct() |
| 38 |
{ |
| 39 |
$this->gatherShippingMethods(); |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* @return array |
| 44 |
*/ |
| 45 |
public function getShippingMethods(): array |
| 46 |
{ |
| 47 |
return $this->shippingMethods; |
| 48 |
} |
| 49 |
|
| 50 |
private function gatherShippingMethods(): void |
| 51 |
{ |
| 52 |
$wooCommerceShippingMethods = WC()->shipping()->get_shipping_methods(); |
| 53 |
|
| 54 |
if (! $wooCommerceShippingMethods) { |
| 55 |
return; |
| 56 |
} |
| 57 |
|
| 58 |
foreach ($wooCommerceShippingMethods as $shippingMethodId => $zoneShippingMethod) { |
| 59 |
$method = $wooCommerceShippingMethods[$shippingMethodId]; |
| 60 |
$methodTitle = $method->method_title ?? $method->title; |
| 61 |
|
| 62 |
$this->addShippingMethod($shippingMethodId, $methodTitle); |
| 63 |
$this->addFlatRateShippingMethods($shippingMethodId, $methodTitle); |
| 64 |
|
| 65 |
$this->addShippingMethodsFromShippingZones(); |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Add a shipping method. Skips disallowed entries. |
| 71 |
* |
| 72 |
* @param string $key |
| 73 |
* @param string $value |
| 74 |
*/ |
| 75 |
private function addShippingMethod(string $key, string $value): void |
| 76 |
{ |
| 77 |
if (in_array($key, self::DISALLOWED_SHIPPING_METHODS)) { |
| 78 |
return; |
| 79 |
} |
| 80 |
|
| 81 |
$this->shippingMethods[$key] = $value; |
| 82 |
} |
| 83 |
|
| 84 |
private function addShippingMethodsFromShippingZones() |
| 85 |
{ |
| 86 |
$shippingZones = WC_Shipping_Zones::get_zones(); |
| 87 |
|
| 88 |
foreach ($shippingZones as $shippingZone) { |
| 89 |
$zoneId = $shippingZone['id'] ?? $shippingZone['zone_id']; |
| 90 |
|
| 91 |
if (! $zoneId) { |
| 92 |
continue; |
| 93 |
} |
| 94 |
|
| 95 |
$zone = WC_Shipping_Zones::get_zone($zoneId); |
| 96 |
/* @var WC_Shipping_Method[] $zoneShippingMethods */ |
| 97 |
$zoneShippingMethods = $zone->get_shipping_methods(false); |
| 98 |
|
| 99 |
foreach ($zoneShippingMethods as $zoneShippingMethod) { |
| 100 |
$this->addWooCommerceZoneShippingMethods($zone, $zoneShippingMethod); |
| 101 |
$this->addBetrsZoneShippingMethods($zone, $zoneShippingMethod); |
| 102 |
} |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* @param string $shippingMethodId |
| 108 |
* @param string $methodTitle |
| 109 |
*/ |
| 110 |
private function addFlatRateShippingMethods(string $shippingMethodId, string $methodTitle): void |
| 111 |
{ |
| 112 |
$isFlatRate = in_array($shippingMethodId, [self::FLAT_RATE, self::LEGACY_FLAT_RATE]); |
| 113 |
$versionAbove2_4 = version_compare(WOOCOMMERCE_VERSION, '2.4', '>='); |
| 114 |
|
| 115 |
// split flat rate by shipping class |
| 116 |
if (! $isFlatRate || ! $versionAbove2_4) { |
| 117 |
return; |
| 118 |
} |
| 119 |
|
| 120 |
/** @var WP_Term[] $shippingClasses */ |
| 121 |
$shippingClasses = WC()->shipping()->get_shipping_classes(); |
| 122 |
|
| 123 |
foreach ($shippingClasses as $shippingClass) { |
| 124 |
if (! isset($shippingClass->term_id)) { |
| 125 |
continue; |
| 126 |
} |
| 127 |
|
| 128 |
$this->addShippingMethod( |
| 129 |
esc_attr($shippingMethodId) . ":" . $shippingClass->term_id, |
| 130 |
esc_html("{$methodTitle} - {$shippingClass->name}") |
| 131 |
); |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* @param WC_Shipping_Zone $zone |
| 137 |
* @param WC_Shipping_Method $zoneShippingMethod |
| 138 |
*/ |
| 139 |
private function addWooCommerceZoneShippingMethods(WC_Shipping_Zone $zone, WC_Shipping_Method $zoneShippingMethod) |
| 140 |
{ |
| 141 |
if (! is_a($zoneShippingMethod, self::SHIPPING_METHOD_CLASS_WC)) { |
| 142 |
return; |
| 143 |
} |
| 144 |
|
| 145 |
foreach ($zoneShippingMethod->get_shipping_rates() as $zoneShippingRate) { |
| 146 |
$label = $zoneShippingRate->rate_label ?? "{$zoneShippingMethod->title} ({$zoneShippingRate->rate_id})"; |
| 147 |
|
| 148 |
$this->addShippingMethod( |
| 149 |
self::TABLE_RATES_WOOCOMMERCE . ":{$zoneShippingMethod->instance_id}:{$zoneShippingRate->rate_id}", |
| 150 |
"{$zone->get_zone_name()} - {$label}" |
| 151 |
); |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* @param WC_Shipping_Zone $zone |
| 157 |
* @param WC_Shipping_Method $zoneShippingMethod |
| 158 |
*/ |
| 159 |
private function addBetrsZoneShippingMethods(WC_Shipping_Zone $zone, WC_Shipping_Method $zoneShippingMethod) |
| 160 |
{ |
| 161 |
if (! is_a($zoneShippingMethod, self::SHIPPING_METHOD_CLASS_BETRS)) { |
| 162 |
return; |
| 163 |
} |
| 164 |
|
| 165 |
$shippingMethodOption = get_option($zoneShippingMethod->id . '_options-' . $zoneShippingMethod->instance_id); |
| 166 |
|
| 167 |
if (! isset($shippingMethodOption['settings'])) { |
| 168 |
return; |
| 169 |
} |
| 170 |
|
| 171 |
foreach ($shippingMethodOption['settings'] as $zoneShippingRate) { |
| 172 |
$optionId = $zoneShippingRate['option_id']; |
| 173 |
$label = $zoneShippingRate['title'] ?? "{$zoneShippingMethod->title} ({$optionId})"; |
| 174 |
|
| 175 |
/* |
| 176 |
* It appears that after version 4.0.0 the separator changed from "_" to ":". We're adding both variants |
| 177 |
* here for compatibility. |
| 178 |
*/ |
| 179 |
foreach ([':', '_'] as $separator) { |
| 180 |
$this->addShippingMethod( |
| 181 |
self::TABLE_RATES_BOLDER_ELEMENTS . "$separator{$zoneShippingMethod->instance_id}-{$optionId}", |
| 182 |
"{$zone->get_zone_name()} - {$label}" |
| 183 |
); |
| 184 |
} |
| 185 |
} |
| 186 |
} |
| 187 |
} |
| 188 |
|