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.0.9 6.2.0-rc.2 3.1.0 6.2.1 3.1.1 6.2.2 3.1.2 6.2.3 3.2.0 6.3.0 3.2.1 6.3.0-beta.1 3.2.2 6.3.0-rc.1 3.2.3 6.3.0-rc.2 3.2.4 6.3.1 3.2.5 6.3.2 3.2.6 6.4.0 3.3.0 6.4.0-beta.1 3.3.1 6.4.0-rc.1 3.3.2 6.4.1 3.3.2-rc.1 6.4.2 3.3.3 6.5.0 3.3.4 6.5.0-beta.1 3.3.5 6.5.0-rc.1 3.3.6 6.5.0-rc.2 3.4.0 6.5.1 3.4.0-beta.1 6.5.2 3.4.0-rc.2 6.6.0 3.4.1 6.6.0-beta.1 3.4.2 6.6.0-rc.1 3.4.3 6.6.0-rc.2 3.4.4 6.6.1 3.4.5 6.6.2 3.4.6 6.7.0 3.4.7 6.7.0-beta.1 3.4.8 6.7.0-beta.2 3.5.0 6.7.0-rc.1 3.5.0-beta.1 6.7.1 3.5.0-rc.1 6.8.0 3.5.0-rc.2 6.8.0-beta.1 3.5.1 6.8.0-beta.2 3.5.10 6.8.0-rc.1 3.5.2 6.8.1 3.5.3 6.8.2 3.5.4 6.8.3 3.5.5 6.9.0 3.5.6 6.9.0-beta.1 3.5.7 6.9.0-beta.2 3.5.8 6.9.0-rc.1 3.5.9 6.9.1 3.6.0 6.9.2 3.6.0-beta.1 6.9.3 3.6.0-rc.1 6.9.4 3.6.0-rc.2 6.9.5 3.6.0-rc.3 7.0.0 3.6.1 7.0.0-beta.1 3.6.2 7.0.0-beta.2 3.6.3 7.0.0-beta.3 3.6.4 7.0.0-rc.1 3.6.5 7.0.0-rc.2 3.6.6 7.0.1 3.6.7 7.0.2 3.7.0 7.1.0 3.7.0-beta.1 7.1.0-beta.1 3.7.0-rc.1 7.1.0-beta.2 3.7.0-rc.2 7.1.0-rc.1 3.7.1 7.1.0-rc.2 3.7.2 7.1.1
woocommerce / includes / abstracts / abstract-wc-shipping-method.php
woocommerce / includes / abstracts Last commit date
abstract-wc-address-provider.php 9 months ago abstract-wc-data.php 4 months ago abstract-wc-deprecated-hooks.php 6 years ago abstract-wc-integration.php 5 years ago abstract-wc-log-handler.php 2 years ago abstract-wc-object-query.php 5 years ago abstract-wc-order.php 1 month ago abstract-wc-payment-gateway.php 1 month ago abstract-wc-payment-token.php 5 years ago abstract-wc-privacy.php 5 years ago abstract-wc-product.php 1 month ago abstract-wc-session.php 10 months ago abstract-wc-settings-api.php 1 year ago abstract-wc-shipping-method.php 4 months ago abstract-wc-widget.php 5 years ago class-wc-background-process.php 5 years ago
abstract-wc-shipping-method.php
628 lines
1 <?php
2 /**
3 * Abstract shipping method
4 *
5 * @class WC_Shipping_Method
6 * @package WooCommerce\Abstracts
7 */
8
9 if ( ! defined( 'ABSPATH' ) ) {
10 exit;
11 }
12
13 use Automattic\WooCommerce\Enums\ProductTaxStatus;
14
15 /**
16 * WooCommerce Shipping Method Class.
17 *
18 * Extended by shipping methods to handle shipping calculations etc.
19 *
20 * @class WC_Shipping_Method
21 * @version 3.0.0
22 * @package WooCommerce\Abstracts
23 */
24 abstract class WC_Shipping_Method extends WC_Settings_API {
25
26 /**
27 * Features this method supports. Possible features used by core:
28 * - shipping-zones Shipping zone functionality + instances
29 * - instance-settings Instance settings screens.
30 * - settings Non-instance settings screens. Enabled by default for BW compatibility with methods before instances existed.
31 * - instance-settings-modal Allows the instance settings to be loaded within a modal in the zones UI.
32 *
33 * @var array
34 */
35 public $supports = array( 'settings' );
36
37 /**
38 * Unique ID for the shipping method - must be set.
39 *
40 * @var string
41 */
42 public $id = '';
43
44 /**
45 * Method title.
46 *
47 * @var string
48 */
49 public $method_title = '';
50
51 /**
52 * Method description.
53 *
54 * @var string
55 */
56 public $method_description = '';
57
58 /**
59 * Yes or no based on whether the method is enabled.
60 *
61 * @var string
62 */
63 public $enabled = 'yes';
64
65 /**
66 * Shipping method title for the frontend.
67 *
68 * @var string
69 */
70 public $title;
71
72 /**
73 * This is an array of rates - methods must populate this array to register shipping costs.
74 *
75 * @var array
76 */
77 public $rates = array();
78
79 /**
80 * If 'taxable' tax will be charged for this method (if applicable).
81 *
82 * @var string
83 */
84 public $tax_status = ProductTaxStatus::TAXABLE;
85
86 /**
87 * Fee for the method (if applicable).
88 *
89 * @var string
90 */
91 public $fee = null;
92
93 /**
94 * Minimum fee for the method (if applicable).
95 *
96 * @var string
97 */
98 public $minimum_fee = null;
99
100 /**
101 * Instance ID if used.
102 *
103 * @var int
104 */
105 public $instance_id = 0;
106
107 /**
108 * Instance form fields.
109 *
110 * @var array
111 */
112 public $instance_form_fields = array();
113
114 /**
115 * Instance settings.
116 *
117 * @var array
118 */
119 public $instance_settings = array();
120
121 /**
122 * Availability - legacy. Used for method Availability.
123 * No longer useful for instance based shipping methods.
124 *
125 * @deprecated 2.6.0
126 * @var string
127 */
128 public $availability;
129
130 /**
131 * Availability countries - legacy. Used for method Availability.
132 * No longer useful for instance based shipping methods.
133 *
134 * @deprecated 2.6.0
135 * @var array
136 */
137 public $countries = array();
138
139 /**
140 * Shipping method order.
141 *
142 * @var int
143 */
144 public $method_order;
145
146 /**
147 * Whether the shipping method has settings or not. Preferably, use {@see has_settings()} instead.
148 *
149 * @var bool
150 */
151 public $has_settings;
152
153 /**
154 * When the method supports the settings modal, this is the admin settings HTML.
155 * Preferably, use {@see get_admin_options_html()} instead.
156 *
157 * @var string|bool
158 */
159 public $settings_html;
160
161
162
163 /**
164 * Constructor.
165 *
166 * @param int $instance_id Instance ID.
167 */
168 public function __construct( $instance_id = 0 ) {
169 $this->instance_id = absint( $instance_id );
170 }
171
172 /**
173 * Check if a shipping method supports a given feature.
174 *
175 * Methods should override this to declare support (or lack of support) for a feature.
176 *
177 * @param string $feature The name of a feature to test support for.
178 * @return bool True if the shipping method supports the feature, false otherwise.
179 */
180 public function supports( $feature ) {
181 return apply_filters( 'woocommerce_shipping_method_supports', in_array( $feature, $this->supports ), $feature, $this );
182 }
183
184 /**
185 * Called to calculate shipping rates for this method. Rates can be added using the add_rate() method.
186 *
187 * @param array $package Package array.
188 */
189 public function calculate_shipping( $package = array() ) {}
190
191 /**
192 * Whether or not we need to calculate tax on top of the shipping rate.
193 *
194 * @return boolean
195 */
196 public function is_taxable() {
197 return wc_tax_enabled() && ProductTaxStatus::TAXABLE === $this->tax_status && ( WC()->customer && ! WC()->customer->get_is_vat_exempt() );
198 }
199
200 /**
201 * Whether or not this method is enabled in settings.
202 *
203 * @since 2.6.0
204 * @return boolean
205 */
206 public function is_enabled() {
207 return 'yes' === $this->enabled;
208 }
209
210 /**
211 * Return the shipping method instance ID.
212 *
213 * @since 2.6.0
214 * @return int
215 */
216 public function get_instance_id() {
217 return $this->instance_id;
218 }
219
220 /**
221 * Return the shipping method title.
222 *
223 * @since 2.6.0
224 * @return string
225 */
226 public function get_method_title() {
227 return apply_filters( 'woocommerce_shipping_method_title', $this->method_title, $this );
228 }
229
230 /**
231 * Return the shipping method description.
232 *
233 * @since 2.6.0
234 * @return string
235 */
236 public function get_method_description() {
237 return apply_filters( 'woocommerce_shipping_method_description', $this->method_description, $this );
238 }
239
240 /**
241 * Return the shipping title which is user set.
242 *
243 * @return string
244 */
245 public function get_title() {
246 return apply_filters( 'woocommerce_shipping_method_title', $this->title, $this->id );
247 }
248
249 /**
250 * Return calculated rates for a package.
251 *
252 * @since 2.6.0
253 * @param array $package Package array.
254 * @return array
255 */
256 public function get_rates_for_package( $package ) {
257 $this->rates = array();
258 if ( $this->is_available( $package ) && ( empty( $package['ship_via'] ) || in_array( $this->id, $package['ship_via'] ) ) ) {
259 $this->calculate_shipping( $package );
260 }
261 return $this->rates;
262 }
263
264 /**
265 * Returns a rate ID based on this methods ID and instance, with an optional
266 * suffix if distinguishing between multiple rates.
267 *
268 * @since 2.6.0
269 * @param string $suffix Suffix.
270 * @return string
271 */
272 public function get_rate_id( $suffix = '' ) {
273 $rate_id = array( $this->id );
274
275 if ( $this->instance_id ) {
276 $rate_id[] = $this->instance_id;
277 }
278
279 if ( $suffix ) {
280 $rate_id[] = $suffix;
281 }
282
283 return implode( ':', $rate_id );
284 }
285
286 /**
287 * Add a shipping rate. If taxes are not set they will be calculated based on cost.
288 *
289 * @param array $args Arguments (default: array()).
290 */
291 public function add_rate( $args = array() ) {
292 $args = apply_filters(
293 'woocommerce_shipping_method_add_rate_args',
294 wp_parse_args(
295 $args,
296 array(
297 'id' => $this->get_rate_id(), // ID for the rate. If not passed, this id:instance default will be used.
298 'label' => '', // Label for the rate.
299 'cost' => '0', // Amount or array of costs (per item shipping).
300 'taxes' => '', // Pass taxes, or leave empty to have it calculated for you, or 'false' to disable calculations.
301 'calc_tax' => 'per_order', // Calc tax per_order or per_item. Per item needs an array of costs.
302 'meta_data' => array(), // Array of misc meta data to store along with this rate - key value pairs.
303 'package' => false, // Package array this rate was generated for @since 2.6.0.
304 'price_decimals' => false,
305 )
306 ),
307 $this
308 );
309
310 // ID and label are required.
311 if ( ! $args['id'] || ! $args['label'] ) {
312 return;
313 }
314
315 // Total up the cost.
316 $total_cost = is_array( $args['cost'] ) ? array_sum( $args['cost'] ) : $args['cost'];
317 $taxes = $args['taxes'];
318
319 // 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.
320 if ( ! is_array( $taxes ) && false !== $taxes && $total_cost > 0 && $this->is_taxable() ) {
321 if ( 'per_item' === $args['calc_tax'] ) {
322 $taxes = $this->get_taxes_per_item( $args['cost'] );
323 } else {
324 $shipping_tax_rates = WC_Tax::get_shipping_tax_rates();
325 $taxes = WC_Tax::calc_shipping_tax( $total_cost, $shipping_tax_rates );
326 }
327
328 /**
329 * Filter whether shipping prices include tax.
330 *
331 * @since 10.6.0
332 * @param bool $shipping_prices_include_tax Whether shipping cost includes tax. Default false.
333 */
334 $shipping_prices_include_tax = wc_string_to_bool( apply_filters( 'woocommerce_shipping_prices_include_tax', false ) );
335
336 // If prices include tax, convert gross to net.
337 if ( $shipping_prices_include_tax && ! empty( $taxes ) ) {
338 $total_cost = $total_cost - array_sum( $taxes );
339 }
340 }
341
342 // Round the total cost after taxes have been calculated.
343 $total_cost = wc_format_decimal( $total_cost, $args['price_decimals'] );
344
345 // If the total cost is empty, set it to 0 to prevent issues with arithmetic operations.
346 if ( '' === $total_cost ) {
347 $total_cost = '0';
348 }
349
350 // Create rate object.
351 $rate = new WC_Shipping_Rate();
352 $rate->set_id( $args['id'] );
353 $rate->set_method_id( $this->id );
354 $rate->set_instance_id( $this->instance_id );
355 $rate->set_label( $args['label'] );
356 $rate->set_cost( $total_cost );
357 $rate->set_taxes( $taxes );
358 $rate->set_tax_status( $this->tax_status );
359
360 if ( ! empty( $args['meta_data'] ) ) {
361 foreach ( $args['meta_data'] as $key => $value ) {
362 $rate->add_meta_data( $key, $value );
363 }
364 }
365
366 // Store package data.
367 if ( $args['package'] ) {
368 $items_in_package = array();
369 foreach ( $args['package']['contents'] as $item ) {
370 $product = $item['data'];
371 $items_in_package[] = $product->get_name() . ' &times; ' . $item['quantity'];
372 }
373 $rate->add_meta_data( __( 'Items', 'woocommerce' ), implode( ', ', $items_in_package ) );
374 }
375
376 $this->rates[ $args['id'] ] = apply_filters( 'woocommerce_shipping_method_add_rate', $rate, $args, $this );
377 }
378
379 /**
380 * Calc taxes per item being shipping in costs array.
381 *
382 * @since 2.6.0
383 * @param array $costs Costs.
384 * @return array of taxes
385 */
386 protected function get_taxes_per_item( $costs ) {
387 $taxes = array();
388
389 // If we have an array of costs we can look up each items tax class and add tax accordingly.
390 if ( is_array( $costs ) ) {
391
392 $cart = WC()->cart->get_cart();
393
394 foreach ( $costs as $cost_key => $amount ) {
395 if ( ! isset( $cart[ $cost_key ] ) ) {
396 continue;
397 }
398
399 $cart_item_data = $cart[ $cost_key ]['data'];
400 if ( is_object( $cart_item_data ) && is_callable( array( $cart_item_data, 'get_tax_class' ) ) ) {
401 $tax_class = $cart_item_data->get_tax_class();
402 } else {
403 $tax_class = null;
404 }
405 $item_tax_rates = WC_Tax::get_shipping_tax_rates( $tax_class );
406 $item_taxes = WC_Tax::calc_shipping_tax( $amount, $item_tax_rates );
407
408 // Sum the item taxes.
409 foreach ( array_keys( $taxes + $item_taxes ) as $key ) {
410 $taxes[ $key ] = ( isset( $item_taxes[ $key ] ) ? $item_taxes[ $key ] : 0 ) + ( isset( $taxes[ $key ] ) ? $taxes[ $key ] : 0 );
411 }
412 }
413
414 // Add any cost for the order - order costs are in the key 'order'.
415 if ( isset( $costs['order'] ) ) {
416 $order_tax_rates = WC_Tax::get_shipping_tax_rates();
417 $item_taxes = WC_Tax::calc_shipping_tax( $costs['order'], $order_tax_rates );
418
419 // Sum the item taxes.
420 foreach ( array_keys( $taxes + $item_taxes ) as $key ) {
421 $taxes[ $key ] = ( isset( $item_taxes[ $key ] ) ? $item_taxes[ $key ] : 0 ) + ( isset( $taxes[ $key ] ) ? $taxes[ $key ] : 0 );
422 }
423 }
424 }
425
426 return $taxes;
427 }
428
429 /**
430 * Is this method available?
431 *
432 * @param array $package Package.
433 * @return bool
434 */
435 public function is_available( $package ) {
436 $available = $this->is_enabled();
437
438 // Country availability (legacy, for non-zone based methods).
439 if ( ! $this->instance_id && $available ) {
440 $countries = is_array( $this->countries ) ? $this->countries : array();
441
442 switch ( $this->availability ) {
443 case 'specific':
444 case 'including':
445 $available = in_array( $package['destination']['country'], array_intersect( $countries, array_keys( WC()->countries->get_shipping_countries() ) ) );
446 break;
447 case 'excluding':
448 $available = in_array( $package['destination']['country'], array_diff( array_keys( WC()->countries->get_shipping_countries() ), $countries ) );
449 break;
450 default:
451 $available = in_array( $package['destination']['country'], array_keys( WC()->countries->get_shipping_countries() ) );
452 break;
453 }
454 }
455
456 return apply_filters( 'woocommerce_shipping_' . $this->id . '_is_available', $available, $package, $this );
457 }
458
459 /**
460 * Get fee to add to shipping cost.
461 *
462 * @param string|float $fee Fee.
463 * @param float $total Total.
464 * @return float
465 */
466 public function get_fee( $fee, $total ) {
467 if ( strstr( $fee, '%' ) ) {
468 $fee = ( $total / 100 ) * str_replace( '%', '', $fee );
469 }
470 if ( ! empty( $this->minimum_fee ) && $this->minimum_fee > $fee ) {
471 $fee = $this->minimum_fee;
472 }
473 return $fee;
474 }
475
476 /**
477 * Does this method have a settings page?
478 *
479 * @return bool
480 */
481 public function has_settings() {
482 return $this->instance_id ? $this->supports( 'instance-settings' ) : $this->supports( 'settings' );
483 }
484
485 /**
486 * Return admin options as a html string.
487 *
488 * @return string
489 */
490 public function get_admin_options_html() {
491 if ( $this->instance_id ) {
492 $settings_html = $this->generate_settings_html( $this->get_instance_form_fields(), false );
493 } else {
494 $settings_html = $this->generate_settings_html( $this->get_form_fields(), false );
495 }
496
497 return '<table class="form-table">' . $settings_html . '</table>';
498 }
499
500 /**
501 * Output the shipping settings screen.
502 */
503 public function admin_options() {
504 if ( ! $this->instance_id ) {
505 echo '<h2>' . esc_html( $this->get_method_title() ) . '</h2>';
506 }
507 echo wp_kses_post( wpautop( $this->get_method_description() ) );
508 echo $this->get_admin_options_html(); // phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped
509 }
510
511 /**
512 * Get_option function.
513 *
514 * Gets an option from the settings API, using defaults if necessary to prevent undefined notices.
515 *
516 * @param string $key Key.
517 * @param mixed $empty_value Empty value.
518 * @return mixed The value specified for the option or a default value for the option.
519 */
520 public function get_option( $key, $empty_value = null ) {
521 // Instance options take priority over global options.
522 if ( $this->instance_id && array_key_exists( $key, $this->get_instance_form_fields() ) ) {
523 return $this->get_instance_option( $key, $empty_value );
524 }
525
526 // Return global option.
527 $option = apply_filters( 'woocommerce_shipping_' . $this->id . '_option', parent::get_option( $key, $empty_value ), $key, $this );
528 return $option;
529 }
530
531 /**
532 * Gets an option from the settings API, using defaults if necessary to prevent undefined notices.
533 *
534 * @param string $key Key.
535 * @param mixed $empty_value Empty value.
536 * @return mixed The value specified for the option or a default value for the option.
537 */
538 public function get_instance_option( $key, $empty_value = null ) {
539 if ( empty( $this->instance_settings ) ) {
540 $this->init_instance_settings();
541 }
542
543 // Get option default if unset.
544 if ( ! isset( $this->instance_settings[ $key ] ) ) {
545 $form_fields = $this->get_instance_form_fields();
546 $this->instance_settings[ $key ] = $this->get_field_default( $form_fields[ $key ] );
547 }
548
549 if ( ! is_null( $empty_value ) && '' === $this->instance_settings[ $key ] ) {
550 $this->instance_settings[ $key ] = $empty_value;
551 }
552
553 $instance_option = apply_filters( 'woocommerce_shipping_' . $this->id . '_instance_option', $this->instance_settings[ $key ], $key, $this );
554 return $instance_option;
555 }
556
557 /**
558 * Get settings fields for instances of this shipping method (within zones).
559 * Should be overridden by shipping methods to add options.
560 *
561 * @since 2.6.0
562 * @return array
563 */
564 public function get_instance_form_fields() {
565 return apply_filters( 'woocommerce_shipping_instance_form_fields_' . $this->id, array_map( array( $this, 'set_defaults' ), $this->instance_form_fields ) );
566 }
567
568 /**
569 * Return the name of the option in the WP DB.
570 *
571 * @since 2.6.0
572 * @return string
573 */
574 public function get_instance_option_key() {
575 return $this->instance_id ? $this->plugin_id . $this->id . '_' . $this->instance_id . '_settings' : '';
576 }
577
578 /**
579 * Initialise Settings for instances.
580 *
581 * @since 2.6.0
582 */
583 public function init_instance_settings() {
584 $this->instance_settings = get_option( $this->get_instance_option_key(), null );
585
586 // If there are no settings defined, use defaults.
587 if ( ! is_array( $this->instance_settings ) ) {
588 $form_fields = $this->get_instance_form_fields();
589 $this->instance_settings = array_merge( array_fill_keys( array_keys( $form_fields ), '' ), wp_list_pluck( $form_fields, 'default' ) );
590 }
591 }
592
593 /**
594 * Processes and saves global shipping method options in the admin area.
595 *
596 * This method is usually attached to woocommerce_update_options_x hooks.
597 *
598 * @since 2.6.0
599 * @return bool was anything saved?
600 */
601 public function process_admin_options() {
602 if ( ! $this->instance_id ) {
603 return parent::process_admin_options();
604 }
605
606 // Check we are processing the correct form for this instance.
607 if ( ! isset( $_REQUEST['instance_id'] ) || absint( $_REQUEST['instance_id'] ) !== $this->instance_id ) { // WPCS: input var ok, CSRF ok.
608 return false;
609 }
610
611 $this->init_instance_settings();
612
613 $post_data = $this->get_post_data();
614
615 foreach ( $this->get_instance_form_fields() as $key => $field ) {
616 if ( 'title' !== $this->get_field_type( $field ) ) {
617 try {
618 $this->instance_settings[ $key ] = $this->get_field_value( $key, $field, $post_data );
619 } catch ( Exception $e ) {
620 $this->add_error( $e->getMessage() );
621 }
622 }
623 }
624
625 return update_option( $this->get_instance_option_key(), apply_filters( 'woocommerce_shipping_' . $this->id . '_instance_settings_values', $this->instance_settings, $this ), 'yes' );
626 }
627 }
628