1
|
|
|
<?php |
|
|
|
|
2
|
|
|
|
3
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
4
|
|
|
exit; |
5
|
|
|
} |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* WooCommerce Shipping Method Class. |
9
|
|
|
* |
10
|
|
|
* Extended by shipping methods to handle shipping calculations etc. |
11
|
|
|
* |
12
|
|
|
* @class WC_Shipping_Method |
13
|
|
|
* @version 2.6.0 |
14
|
|
|
* @package WooCommerce/Abstracts |
15
|
|
|
* @category Abstract Class |
16
|
|
|
* @author WooThemes |
17
|
|
|
*/ |
18
|
|
|
abstract class WC_Shipping_Method extends WC_Settings_API { |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Features this method supports. Possible features used by core: |
22
|
|
|
* - shipping-zones Shipping zone functionality + instances |
23
|
|
|
* - instance-settings Instance settings screens. |
24
|
|
|
* - settings Non-instance settings screens. Enabled by default for BW compatibility with methods before instances existed. |
25
|
|
|
* @var array |
26
|
|
|
*/ |
27
|
|
|
public $supports = array( 'settings' ); |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Unique ID for the shipping method - must be set. |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
public $id = ''; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Method title. |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
public $method_title = ''; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Method description. |
43
|
|
|
* @var string |
44
|
|
|
*/ |
45
|
|
|
public $method_description = ''; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* yes or no based on whether the method is enabled. |
49
|
|
|
* @var string |
50
|
|
|
*/ |
51
|
|
|
public $enabled = 'yes'; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Shipping method title for the frontend. |
55
|
|
|
* @var string |
56
|
|
|
*/ |
57
|
|
|
public $title; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* This is an array of rates - methods must populate this array to register shipping costs. |
61
|
|
|
* @var array |
62
|
|
|
*/ |
63
|
|
|
public $rates = array(); |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* If 'taxable' tax will be charged for this method (if applicable). |
67
|
|
|
* @var string |
68
|
|
|
*/ |
69
|
|
|
public $tax_status = 'taxable'; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Fee for the method (if applicable). |
73
|
|
|
* @var string |
74
|
|
|
*/ |
75
|
|
|
public $fee = null; |
76
|
|
|
|
77
|
|
|
/** @var float Minimum fee for the method */ |
78
|
|
|
/** |
79
|
|
|
* Minimum fee for the method (if applicable). |
80
|
|
|
* @var string |
81
|
|
|
*/ |
82
|
|
|
public $minimum_fee = null; |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Instance ID if used. |
86
|
|
|
* @var int |
87
|
|
|
*/ |
88
|
|
|
public $instance_id = 0; |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Instance form fields. |
92
|
|
|
* @var array |
93
|
|
|
*/ |
94
|
|
|
public $instance_form_fields = array(); |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Instance settings. |
98
|
|
|
* @var array |
99
|
|
|
*/ |
100
|
|
|
public $instance_settings = array(); |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Availability - legacy. Used for method Availability. |
104
|
|
|
* No longer useful for instance based shipping methods. |
105
|
|
|
* @deprecated 2.6.0 |
106
|
|
|
* @var string |
107
|
|
|
*/ |
108
|
|
|
public $availability; |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Availability countries - legacy. Used for method Availability. |
112
|
|
|
* No longer useful for instance based shipping methods. |
113
|
|
|
* @deprecated 2.6.0 |
114
|
|
|
* @var string |
115
|
|
|
*/ |
116
|
|
|
public $countries = array(); |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Constructor. |
120
|
|
|
* @param int $instance_id |
121
|
|
|
*/ |
122
|
|
|
public function __construct( $instance_id = 0 ) { |
123
|
|
|
$this->instance_id = absint( $instance_id ); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Check if a shipping method supports a given feature. |
128
|
|
|
* |
129
|
|
|
* Methods should override this to declare support (or lack of support) for a feature. |
130
|
|
|
* |
131
|
|
|
* @param $feature string The name of a feature to test support for. |
132
|
|
|
* @return bool True if the shipping method supports the feature, false otherwise. |
133
|
|
|
*/ |
134
|
|
|
public function supports( $feature ) { |
135
|
|
|
return apply_filters( 'woocommerce_shipping_method_supports', in_array( $feature, $this->supports ), $feature, $this ); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Called to calculate shipping rates for this method. Rates can be added using the add_rate() method. |
140
|
|
|
*/ |
141
|
|
|
public function calculate_shipping() {} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Whether or not we need to calculate tax on top of the shipping rate. |
145
|
|
|
* @return boolean |
146
|
|
|
*/ |
147
|
|
|
public function is_taxable() { |
148
|
|
|
return wc_tax_enabled() && 'taxable' === $this->tax_status && ! WC()->customer->is_vat_exempt(); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Whether or not this method is enabled in settings. |
153
|
|
|
* @since 2.6.0 |
154
|
|
|
* @return boolean |
155
|
|
|
*/ |
156
|
|
|
public function is_enabled() { |
157
|
|
|
return 'yes' === $this->enabled; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Return the shipping method instance ID. |
162
|
|
|
* @since 2.6.0 |
163
|
|
|
* @return int |
164
|
|
|
*/ |
165
|
|
|
public function get_instance_id() { |
166
|
|
|
return $this->instance_id; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Return the shipping method title. |
171
|
|
|
* @since 2.6.0 |
172
|
|
|
* @return string |
173
|
|
|
*/ |
174
|
|
|
public function get_method_title() { |
175
|
|
|
return apply_filters( 'woocommerce_shipping_method_title', $this->method_title, $this ); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Return the shipping method description. |
180
|
|
|
* @since 2.6.0 |
181
|
|
|
* @return string |
182
|
|
|
*/ |
183
|
|
|
public function get_method_description() { |
184
|
|
|
return apply_filters( 'woocommerce_shipping_method_description', $this->method_description, $this ); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Return the shipping title which is user set. |
189
|
|
|
* |
190
|
|
|
* @return string |
191
|
|
|
*/ |
192
|
|
|
public function get_title() { |
193
|
|
|
return apply_filters( 'woocommerce_shipping_method_title', $this->title, $this->id ); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Return calculated rates for a package. |
198
|
|
|
* @since 2.6.0 |
199
|
|
|
* @param object $package |
200
|
|
|
* @return array |
201
|
|
|
*/ |
202
|
|
|
public function get_rates_for_package( $package ) { |
203
|
|
|
$this->rates = array(); |
204
|
|
|
if ( $this->is_available( $package ) && ( empty( $package['ship_via'] ) || in_array( $this->id, $package['ship_via'] ) ) ) { |
|
|
|
|
205
|
|
|
$this->calculate_shipping( $package ); |
|
|
|
|
206
|
|
|
} |
207
|
|
|
return $this->rates; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Add a shipping rate. If taxes are not set they will be calculated based on cost. |
212
|
|
|
* @param array $args (default: array()) |
213
|
|
|
*/ |
214
|
|
|
public function add_rate( $args = array() ) { |
215
|
|
|
$args = wp_parse_args( $args, array( |
216
|
|
|
'id' => '', // ID for the rate |
217
|
|
|
'label' => '', // Label for the rate |
218
|
|
|
'cost' => '0', // Amount or array of costs (per item shipping) |
219
|
|
|
'taxes' => '', // Pass taxes, nothing to have it calculated for you, or 'false' to calc no tax |
220
|
|
|
'calc_tax' => 'per_order' // Calc tax per_order or per_item. Per item needs an array of costs |
221
|
|
|
) ); |
222
|
|
|
|
223
|
|
|
// ID and label are required |
224
|
|
|
if ( ! $args['id'] || ! $args['label'] ) { |
225
|
|
|
return; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
// Handle cost |
229
|
|
|
$total_cost = is_array( $args['cost'] ) ? array_sum( $args['cost'] ) : $args['cost']; |
230
|
|
|
$taxes = $args['taxes']; |
231
|
|
|
|
232
|
|
|
// Taxes - if not an array and not set to false, calc tax based on cost and passed calc_tax variable. This saves shipping methods having to do complex tax calculations. |
233
|
|
|
if ( ! is_array( $taxes ) && $taxes !== false && $total_cost > 0 && $this->is_taxable() ) { |
234
|
|
|
$taxes = 'per_item' === $args['calc_tax'] ? $this->get_taxes_per_item( $args['cost'] ) : WC_Tax::calc_shipping_tax( $total_cost, WC_Tax::get_shipping_tax_rates() ); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
$this->rates[ $args['id'] ] = new WC_Shipping_Rate( $args['id'], $args['label'], $total_cost, $taxes, $this->id ); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* Calc taxes per item being shipping in costs array. |
242
|
|
|
* @since 2.6.0 |
243
|
|
|
* @access protected |
244
|
|
|
* @param array $costs |
245
|
|
|
* @return array of taxes |
246
|
|
|
*/ |
247
|
|
|
protected function get_taxes_per_item( $costs ) { |
248
|
|
|
$taxes = array(); |
249
|
|
|
|
250
|
|
|
// If we have an array of costs we can look up each items tax class and add tax accordingly |
251
|
|
|
if ( is_array( $costs ) ) { |
252
|
|
|
|
253
|
|
|
$cart = WC()->cart->get_cart(); |
254
|
|
|
|
255
|
|
|
foreach ( $costs as $cost_key => $amount ) { |
256
|
|
|
if ( ! isset( $cart[ $cost_key ] ) ) { |
257
|
|
|
continue; |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
$item_taxes = WC_Tax::calc_shipping_tax( $amount, WC_Tax::get_shipping_tax_rates( $cart[ $cost_key ]['data']->get_tax_class() ) ); |
261
|
|
|
|
262
|
|
|
// Sum the item taxes |
263
|
|
|
foreach ( array_keys( $taxes + $item_taxes ) as $key ) { |
264
|
|
|
$taxes[ $key ] = ( isset( $item_taxes[ $key ] ) ? $item_taxes[ $key ] : 0 ) + ( isset( $taxes[ $key ] ) ? $taxes[ $key ] : 0 ); |
265
|
|
|
} |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
// Add any cost for the order - order costs are in the key 'order' |
269
|
|
|
if ( isset( $costs['order'] ) ) { |
270
|
|
|
$item_taxes = WC_Tax::calc_shipping_tax( $costs['order'], WC_Tax::get_shipping_tax_rates() ); |
271
|
|
|
|
272
|
|
|
// Sum the item taxes |
273
|
|
|
foreach ( array_keys( $taxes + $item_taxes ) as $key ) { |
274
|
|
|
$taxes[ $key ] = ( isset( $item_taxes[ $key ] ) ? $item_taxes[ $key ] : 0 ) + ( isset( $taxes[ $key ] ) ? $taxes[ $key ] : 0 ); |
275
|
|
|
} |
276
|
|
|
} |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
return $taxes; |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* Is this method available? |
284
|
|
|
* @param array $package |
285
|
|
|
* @return bool |
286
|
|
|
*/ |
287
|
|
|
public function is_available( $package ) { |
288
|
|
|
$available = $this->is_enabled(); |
289
|
|
|
|
290
|
|
|
// Country availability (legacy, for non-zone based methods) |
291
|
|
|
if ( ! $this->instance_id ) { |
292
|
|
|
$countries = is_array( $this->countries ) ? $this->countries : array(); |
|
|
|
|
293
|
|
|
|
294
|
|
|
switch ( $this->availability ) { |
|
|
|
|
295
|
|
|
case 'specific' : |
296
|
|
View Code Duplication |
case 'including' : |
|
|
|
|
297
|
|
|
$available = in_array( $package['destination']['country'], array_intersect( $countries, array_keys( WC()->countries->get_shipping_countries() ) ) ); |
298
|
|
|
break; |
299
|
|
View Code Duplication |
case 'excluding' : |
|
|
|
|
300
|
|
|
$available = in_array( $package['destination']['country'], array_diff( array_keys( WC()->countries->get_shipping_countries() ), $countries ) ); |
301
|
|
|
break; |
302
|
|
View Code Duplication |
default : |
|
|
|
|
303
|
|
|
$available = in_array( $package['destination']['country'], array_keys( WC()->countries->get_shipping_countries() ) ); |
304
|
|
|
break; |
305
|
|
|
} |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
return apply_filters( 'woocommerce_shipping_' . $this->id . '_is_available', $available, $package ); |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
/** |
312
|
|
|
* Get fee to add to shipping cost. |
313
|
|
|
* @param string|float $fee |
314
|
|
|
* @param float $total |
315
|
|
|
* @return float |
316
|
|
|
*/ |
317
|
|
|
public function get_fee( $fee, $total ) { |
318
|
|
|
if ( strstr( $fee, '%' ) ) { |
319
|
|
|
$fee = ( $total / 100 ) * str_replace( '%', '', $fee ); |
320
|
|
|
} |
321
|
|
|
if ( ! empty( $this->minimum_fee ) && $this->minimum_fee > $fee ) { |
322
|
|
|
$fee = $this->minimum_fee; |
323
|
|
|
} |
324
|
|
|
return $fee; |
325
|
|
|
} |
326
|
|
|
|
327
|
|
|
/** |
328
|
|
|
* Does this method have a settings page? |
329
|
|
|
* @return bool |
330
|
|
|
*/ |
331
|
|
|
public function has_settings() { |
332
|
|
|
return $this->instance_id ? $this->supports( 'instance-settings' ) : $this->supports( 'settings' ); |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
/** |
336
|
|
|
* Output the shipping settings screen. |
337
|
|
|
*/ |
338
|
|
|
public function admin_options() { |
339
|
|
|
if ( $this->instance_id ) { |
340
|
|
|
echo wp_kses_post( wpautop( $this->get_method_description() ) ); |
341
|
|
|
echo '<table class="form-table">' . $this->generate_settings_html( $this->get_instance_form_fields(), false ) . '</table>'; |
342
|
|
|
} else { |
343
|
|
|
echo '<h2>' . esc_html( $this->get_method_title() ) . '</h2>'; |
344
|
|
|
echo wp_kses_post( wpautop( $this->get_method_description() ) ); |
345
|
|
|
echo '<table class="form-table">' . $this->generate_settings_html( $this->get_form_fields(), false ) . '</table>'; |
346
|
|
|
} |
347
|
|
|
} |
348
|
|
|
|
349
|
|
|
/** |
350
|
|
|
* get_option function. |
351
|
|
|
* |
352
|
|
|
* Gets and option from the settings API, using defaults if necessary to prevent undefined notices. |
353
|
|
|
* |
354
|
|
|
* @param string $key |
355
|
|
|
* @param mixed $empty_value |
356
|
|
|
* @return mixed The value specified for the option or a default value for the option. |
357
|
|
|
*/ |
358
|
|
|
public function get_option( $key, $empty_value = null ) { |
359
|
|
|
// Instance options take priority over global options |
360
|
|
|
if ( in_array( $key, array_keys( $this->get_instance_form_fields() ) ) ) { |
361
|
|
|
return $this->get_instance_option( $key, $empty_value ); |
362
|
|
|
} |
363
|
|
|
|
364
|
|
|
// Return global option |
365
|
|
|
return parent::get_option( $key, $empty_value ); |
366
|
|
|
} |
367
|
|
|
|
368
|
|
|
/** |
369
|
|
|
* Gets an option from the settings API, using defaults if necessary to prevent undefined notices. |
370
|
|
|
* |
371
|
|
|
* @param string $key |
372
|
|
|
* @param mixed $empty_value |
373
|
|
|
* @return mixed The value specified for the option or a default value for the option. |
374
|
|
|
*/ |
375
|
|
View Code Duplication |
public function get_instance_option( $key, $empty_value = null ) { |
|
|
|
|
376
|
|
|
if ( empty( $this->instance_settings ) ) { |
377
|
|
|
$this->init_instance_settings(); |
378
|
|
|
} |
379
|
|
|
|
380
|
|
|
// Get option default if unset. |
381
|
|
|
if ( ! isset( $this->instance_settings[ $key ] ) ) { |
382
|
|
|
$form_fields = $this->get_instance_form_fields(); |
383
|
|
|
$this->instance_settings[ $key ] = $this->get_field_default( $form_fields[ $key ] ); |
384
|
|
|
} |
385
|
|
|
|
386
|
|
|
if ( ! is_null( $empty_value ) && '' === $this->instance_settings[ $key ] ) { |
387
|
|
|
$this->instance_settings[ $key ] = $empty_value; |
388
|
|
|
} |
389
|
|
|
|
390
|
|
|
return $this->instance_settings[ $key ]; |
391
|
|
|
} |
392
|
|
|
|
393
|
|
|
/** |
394
|
|
|
* Get settings fields for instances of this shipping method (within zones). |
395
|
|
|
* Should be overridden by shipping methods to add options. |
396
|
|
|
* @since 2.6.0 |
397
|
|
|
* @return array |
398
|
|
|
*/ |
399
|
|
|
public function get_instance_form_fields() { |
400
|
|
|
return apply_filters( 'woocommerce_shipping_instance_form_fields_' . $this->id, $this->instance_form_fields ); |
401
|
|
|
} |
402
|
|
|
|
403
|
|
|
/** |
404
|
|
|
* Return the name of the option in the WP DB. |
405
|
|
|
* @since 2.6.0 |
406
|
|
|
* @return string |
407
|
|
|
*/ |
408
|
|
|
public function get_instance_option_key() { |
409
|
|
|
return $this->instance_id ? $this->plugin_id . $this->id . $this->instance_id . '_settings' : ''; |
410
|
|
|
} |
411
|
|
|
|
412
|
|
|
/** |
413
|
|
|
* Initialise Settings for instances. |
414
|
|
|
* @since 2.6.0 |
415
|
|
|
*/ |
416
|
|
View Code Duplication |
public function init_instance_settings() { |
|
|
|
|
417
|
|
|
$this->instance_settings = get_option( $this->get_instance_option_key(), null ); |
418
|
|
|
|
419
|
|
|
// If there are no settings defined, use defaults. |
420
|
|
|
if ( ! is_array( $this->instance_settings ) ) { |
421
|
|
|
$form_fields = $this->get_instance_form_fields(); |
422
|
|
|
$this->instance_settings = array_merge( array_fill_keys( array_keys( $form_fields ), '' ), wp_list_pluck( $form_fields, 'default' ) ); |
423
|
|
|
} |
424
|
|
|
} |
425
|
|
|
|
426
|
|
|
/** |
427
|
|
|
* Processes and saves options. |
428
|
|
|
* If there is an error thrown, will continue to save and validate fields, but will leave the erroring field out. |
429
|
|
|
* @since 2.6.0 |
430
|
|
|
* @return bool was anything saved? |
431
|
|
|
*/ |
432
|
|
|
public function process_admin_options() { |
433
|
|
|
if ( $this->instance_id ) { |
434
|
|
|
$this->init_instance_settings(); |
435
|
|
|
|
436
|
|
View Code Duplication |
foreach ( $this->get_instance_form_fields() as $key => $field ) { |
|
|
|
|
437
|
|
|
if ( ! in_array( $this->get_field_type( $field ), array( 'title' ) ) ) { |
438
|
|
|
try { |
439
|
|
|
$this->instance_settings[ $key ] = $this->get_field_value( $key, $field ); |
440
|
|
|
} catch ( Exception $e ) { |
441
|
|
|
$this->add_error( $e->getMessage() ); |
442
|
|
|
} |
443
|
|
|
} |
444
|
|
|
} |
445
|
|
|
|
446
|
|
|
return update_option( $this->get_instance_option_key(), apply_filters( 'woocommerce_shipping_' . $this->id . '_instance_settings_values', $this->instance_settings, $this ) ); |
447
|
|
|
} else { |
448
|
|
|
return parent::process_admin_options(); |
449
|
|
|
} |
450
|
|
|
} |
451
|
|
|
} |
452
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.