1
|
|
|
<?php |
2
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
3
|
|
|
exit; |
4
|
|
|
} |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* WooCommerce Shipping Class |
8
|
|
|
* |
9
|
|
|
* Handles shipping and loads shipping methods via hooks. |
10
|
|
|
* |
11
|
|
|
* @class WC_Shipping |
12
|
|
|
* @version 2.6.0 |
13
|
|
|
* @package WooCommerce/Classes/Shipping |
14
|
|
|
* @category Class |
15
|
|
|
* @author WooThemes |
16
|
|
|
*/ |
17
|
|
|
class WC_Shipping { |
18
|
|
|
|
19
|
|
|
/** @var bool True if shipping is enabled. */ |
20
|
|
|
public $enabled = false; |
21
|
|
|
|
22
|
|
|
/** @var array|null Stores methods loaded into woocommerce. */ |
23
|
|
|
public $shipping_methods = null; |
24
|
|
|
|
25
|
|
|
/** @var object|null Stores matching shipping zone. */ |
26
|
|
|
protected $shipping_zone = null; |
27
|
|
|
|
28
|
|
|
/** @var float Stores the cost of shipping */ |
29
|
|
|
public $shipping_total = 0; |
30
|
|
|
|
31
|
|
|
/** @var array Stores an array of shipping taxes. */ |
32
|
|
|
public $shipping_taxes = array(); |
33
|
|
|
|
34
|
|
|
/** @var array Stores the shipping classes. */ |
35
|
|
|
public $shipping_classes = array(); |
36
|
|
|
|
37
|
|
|
/** @var array Stores packages to ship and to get quotes for. */ |
38
|
|
|
public $packages = array(); |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var WC_Shipping The single instance of the class |
42
|
|
|
* @since 2.1 |
43
|
|
|
*/ |
44
|
|
|
protected static $_instance = null; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Main WC_Shipping Instance. |
48
|
|
|
* |
49
|
|
|
* Ensures only one instance of WC_Shipping is loaded or can be loaded. |
50
|
|
|
* |
51
|
|
|
* @since 2.1 |
52
|
|
|
* @static |
53
|
|
|
* @return WC_Shipping Main instance |
54
|
|
|
*/ |
55
|
|
|
public static function instance() { |
56
|
|
|
if ( is_null( self::$_instance ) ) { |
57
|
|
|
self::$_instance = new self(); |
58
|
|
|
} |
59
|
|
|
return self::$_instance; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Cloning is forbidden. |
64
|
|
|
* |
65
|
|
|
* @since 2.1 |
66
|
|
|
*/ |
67
|
|
|
public function __clone() { |
68
|
|
|
_doing_it_wrong( __FUNCTION__, __( 'Cheatin’ huh?', 'woocommerce' ), '2.1' ); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Unserializing instances of this class is forbidden. |
73
|
|
|
* |
74
|
|
|
* @since 2.1 |
75
|
|
|
*/ |
76
|
|
|
public function __wakeup() { |
77
|
|
|
_doing_it_wrong( __FUNCTION__, __( 'Cheatin’ huh?', 'woocommerce' ), '2.1' ); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Initialize shipping. |
82
|
|
|
*/ |
83
|
|
|
public function __construct() { |
84
|
|
|
$this->enabled = wc_shipping_enabled(); |
85
|
|
|
|
86
|
|
|
if ( $this->enabled ) { |
87
|
|
|
$this->init(); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Initialize shipping. |
93
|
|
|
*/ |
94
|
|
|
public function init() { |
95
|
|
|
do_action( 'woocommerce_shipping_init' ); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Shipping methods register themselves by returning their main class name through the woocommerce_shipping_methods filter. |
100
|
|
|
* @return array |
101
|
|
|
*/ |
102
|
|
|
public function get_shipping_method_class_names() { |
103
|
|
|
// Unique Method ID => Method Class name |
104
|
|
|
$shipping_methods = array( |
105
|
|
|
'flat_rate' => 'WC_Shipping_Flat_Rate', |
106
|
|
|
'free_shipping' => 'WC_Shipping_Free_Shipping', |
107
|
|
|
'local_pickup' => 'WC_Shipping_Local_Pickup', |
108
|
|
|
); |
109
|
|
|
|
110
|
|
|
// For backwards compatibility with 2.5.x we load any ENABLED legacy shipping methods here |
111
|
|
|
$maybe_load_legacy_methods = array( 'flat_rate', 'free_shipping', 'international_delivery', 'local_delivery', 'local_pickup' ); |
112
|
|
|
|
113
|
|
|
foreach ( $maybe_load_legacy_methods as $method ) { |
114
|
|
|
$options = get_option( 'woocommerce_' . $method . '_settings' ); |
115
|
|
|
if ( $options && isset( $options['enabled'] ) && 'yes' === $options['enabled'] ) { |
116
|
|
|
$shipping_methods[ 'legacy_' . $method ] = 'WC_Shipping_Legacy_' . $method; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
return apply_filters( 'woocommerce_shipping_methods', $shipping_methods ); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Get matching shipping zone. |
125
|
|
|
* @return object|null |
126
|
|
|
*/ |
127
|
|
|
public function get_shipping_zone() { |
128
|
|
|
return $this->shipping_zone; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Loads all shipping methods which are hooked in. If a $package is passed some methods may add themselves conditionally. |
133
|
|
|
* |
134
|
|
|
* Loads all shipping methods which are hooked in. |
135
|
|
|
* If a $package is passed some methods may add themselves conditionally and zones will be used. |
136
|
|
|
* |
137
|
|
|
* @param array $package |
138
|
|
|
* @return array |
139
|
|
|
*/ |
140
|
|
|
public function load_shipping_methods( $package = array() ) { |
141
|
|
|
if ( ! empty( $package ) ) { |
142
|
|
|
$this->shipping_zone = WC_Shipping_Zones::get_zone_matching_package( $package ); |
|
|
|
|
143
|
|
|
$this->shipping_methods = $this->shipping_zone->get_shipping_methods( true ); |
144
|
|
|
} else { |
145
|
|
|
$this->shipping_zone = null; |
146
|
|
|
$this->shipping_methods = array(); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
// For the settings in the backend, and for non-shipping zone methods, we still need to load any registered classes here. |
150
|
|
|
foreach ( $this->get_shipping_method_class_names() as $method_id => $method_class ) { |
151
|
|
|
$this->register_shipping_method( $method_class ); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
// Methods can register themselves manually through this hook if necessary. |
155
|
|
|
do_action( 'woocommerce_load_shipping_methods', $package ); |
156
|
|
|
|
157
|
|
|
// Return loaded methods |
158
|
|
|
return $this->get_shipping_methods(); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Register a shipping method. |
163
|
|
|
* |
164
|
|
|
* @param object|string $method Either the name of the method's class, or an instance of the method's class. |
165
|
|
|
*/ |
166
|
|
|
public function register_shipping_method( $method ) { |
167
|
|
|
if ( ! is_object( $method ) ) { |
168
|
|
|
if ( ! class_exists( $method ) ) { |
169
|
|
|
return false; |
170
|
|
|
} |
171
|
|
|
$method = new $method(); |
172
|
|
|
} |
173
|
|
|
if ( is_null( $this->shipping_methods ) ) { |
174
|
|
|
$this->shipping_methods = array(); |
175
|
|
|
} |
176
|
|
|
$this->shipping_methods[ $method->id ] = $method; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Unregister shipping methods. |
181
|
|
|
*/ |
182
|
|
|
public function unregister_shipping_methods() { |
183
|
|
|
$this->shipping_methods = null; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Returns all registered shipping methods for usage. |
188
|
|
|
* |
189
|
|
|
* @access public |
190
|
|
|
* @return array |
191
|
|
|
*/ |
192
|
|
|
public function get_shipping_methods() { |
193
|
|
|
if ( is_null( $this->shipping_methods ) ) { |
194
|
|
|
$this->load_shipping_methods(); |
195
|
|
|
} |
196
|
|
|
return $this->shipping_methods; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Get an array of shipping classes. |
201
|
|
|
* |
202
|
|
|
* @access public |
203
|
|
|
* @return array |
204
|
|
|
*/ |
205
|
|
|
public function get_shipping_classes() { |
206
|
|
|
if ( empty( $this->shipping_classes ) ) { |
207
|
|
|
$classes = get_terms( 'product_shipping_class', array( 'hide_empty' => '0', 'orderby' => 'name' ) ); |
208
|
|
|
$this->shipping_classes = ! is_wp_error( $classes ) ? $classes : array(); |
209
|
|
|
} |
210
|
|
|
return apply_filters( 'woocommerce_get_shipping_classes', $this->shipping_classes ); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Calculate shipping for (multiple) packages of cart items. |
215
|
|
|
* |
216
|
|
|
* @param array $packages multi-dimensional array of cart items to calc shipping for. |
217
|
|
|
* @return array Array of calculated packages. |
218
|
|
|
*/ |
219
|
|
|
public function calculate_shipping( $packages = array() ) { |
220
|
|
|
$this->packages = array(); |
221
|
|
|
|
222
|
|
|
if ( ! $this->enabled || empty( $packages ) ) { |
223
|
|
|
return array(); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
// Calculate costs for passed packages |
227
|
|
|
foreach ( $packages as $package_key => $package ) { |
228
|
|
|
$this->packages[ $package_key ] = $this->calculate_shipping_for_package( $package, $package_key ); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* Allow packages to be reorganized after calculate the shipping. |
233
|
|
|
* |
234
|
|
|
* This filter can be used to apply some extra manipulation after the shipping costs are calculated for the packages |
235
|
|
|
* but before Woocommerce does anything with them. A good example of usage is to merge the shipping methods for multiple |
236
|
|
|
* packages for marketplaces. |
237
|
|
|
* |
238
|
|
|
* @since 2.6.0 |
239
|
|
|
* |
240
|
|
|
* @param array $packages The array of packages after shipping costs are calculated. |
241
|
|
|
*/ |
242
|
|
|
$this->packages = array_filter( (array) apply_filters( 'woocommerce_shipping_packages', $this->packages ) ); |
243
|
|
|
|
244
|
|
|
return $this->packages; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* See if package is shippable. |
249
|
|
|
* @param array $package |
250
|
|
|
* @return boolean |
251
|
|
|
*/ |
252
|
|
|
protected function is_package_shippable( $package ) { |
253
|
|
|
$allowed = array_keys( WC()->countries->get_shipping_countries() ); |
254
|
|
|
return in_array( $package['destination']['country'], $allowed ); |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* Calculate shipping rates for a package, |
259
|
|
|
* |
260
|
|
|
* Calculates each shipping methods cost. Rates are stored in the session based on the package hash to avoid re-calculation every page load. |
261
|
|
|
* |
262
|
|
|
* @param array $package cart items |
263
|
|
|
* @param int $package_key Index of the package being calculated. Used to cache multiple package rates. |
264
|
|
|
* @return array |
265
|
|
|
*/ |
266
|
|
|
public function calculate_shipping_for_package( $package = array(), $package_key = 0 ) { |
267
|
|
|
if ( ! $this->enabled || empty( $package ) || ! $this->is_package_shippable( $package ) ) { |
268
|
|
|
return false; |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
// Check if we need to recalculate shipping for this package |
272
|
|
|
$package_to_hash = $package; |
273
|
|
|
|
274
|
|
|
// Remove data objects so hashes are consistent |
275
|
|
|
foreach ( $package_to_hash['contents'] as $item_id => $item ) { |
276
|
|
|
unset( $package_to_hash['contents'][ $item_id ]['data'] ); |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
$package_hash = 'wc_ship_' . md5( json_encode( $package_to_hash ) . WC_Cache_Helper::get_transient_version( 'shipping' ) ); |
280
|
|
|
$session_key = 'shipping_for_package_' . $package_key; |
281
|
|
|
$stored_rates = WC()->session->get( $session_key ); |
282
|
|
|
|
283
|
|
|
if ( ! is_array( $stored_rates ) || $package_hash !== $stored_rates['package_hash'] || 'yes' === get_option( 'woocommerce_shipping_debug_mode', 'no' ) ) { |
284
|
|
|
// Calculate shipping method rates |
285
|
|
|
$package['rates'] = array(); |
286
|
|
|
|
287
|
|
|
foreach ( $this->load_shipping_methods( $package ) as $shipping_method ) { |
|
|
|
|
288
|
|
|
// Shipping instances need an ID |
289
|
|
|
if ( ! $shipping_method->supports( 'shipping-zones' ) || $shipping_method->get_instance_id() ) { |
290
|
|
|
$package['rates'] = $package['rates'] + $shipping_method->get_rates_for_package( $package ); // + instead of array_merge maintains numeric keys |
291
|
|
|
} |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
// Filter the calculated rates |
295
|
|
|
$package['rates'] = apply_filters( 'woocommerce_package_rates', $package['rates'], $package ); |
296
|
|
|
|
297
|
|
|
// Store in session to avoid recalculation |
298
|
|
|
WC()->session->set( $session_key, array( |
299
|
|
|
'package_hash' => $package_hash, |
300
|
|
|
'rates' => $package['rates'], |
301
|
|
|
) ); |
302
|
|
|
} else { |
303
|
|
|
$package['rates'] = $stored_rates['rates']; |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
return $package; |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
/** |
310
|
|
|
* Get packages. |
311
|
|
|
* @return array |
312
|
|
|
*/ |
313
|
|
|
public function get_packages() { |
314
|
|
|
return $this->packages; |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
/** |
318
|
|
|
* Reset shipping. |
319
|
|
|
* |
320
|
|
|
* Reset the totals for shipping as a whole. |
321
|
|
|
*/ |
322
|
|
|
public function reset_shipping() { |
323
|
|
|
unset( WC()->session->chosen_shipping_methods ); |
324
|
|
|
$this->shipping_total = null; |
325
|
|
|
$this->shipping_taxes = array(); |
326
|
|
|
$this->packages = array(); |
327
|
|
|
} |
328
|
|
|
} |
329
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: