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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 / class-wc-shipping.php
woocommerce / includes Last commit date
abstracts 1 month ago admin 6 days ago blocks 11 months ago cli 1 month ago customizer 4 months ago data-stores 1 month ago emails 6 days ago export 1 year ago gateways 1 month ago import 3 months ago integrations 2 months ago interfaces 4 months ago legacy 4 months ago libraries 1 month ago log-handlers 1 year ago payment-tokens 5 years ago product-usage 1 year ago queue 4 months ago react-admin 1 month ago rest-api 1 month ago shipping 3 months ago shortcodes 1 month ago theme-support 2 years ago tracks 1 month ago traits 5 years ago walkers 5 years ago wccom-site 1 month ago widgets 2 months ago class-wc-ajax.php 1 month ago class-wc-auth.php 1 year ago class-wc-autoloader.php 9 months ago class-wc-background-emailer.php 2 months ago class-wc-background-updater.php 5 years ago class-wc-brands-brand-settings-manager.php 1 year ago class-wc-brands-coupons.php 1 year ago class-wc-brands.php 1 month ago class-wc-breadcrumb.php 4 months ago class-wc-cache-helper.php 1 month ago class-wc-cart-fees.php 2 years ago class-wc-cart-session.php 3 months ago class-wc-cart-totals.php 11 months ago class-wc-cart.php 1 month ago class-wc-checkout.php 1 month ago class-wc-cli.php 10 months ago class-wc-comments.php 4 months ago class-wc-countries.php 2 months ago class-wc-coupon.php 2 months ago class-wc-customer-download-log.php 5 years ago class-wc-customer-download.php 1 year ago class-wc-customer.php 1 day ago class-wc-data-exception.php 8 years ago class-wc-data-store.php 3 years ago class-wc-datetime.php 4 years ago class-wc-deprecated-action-hooks.php 2 years ago class-wc-deprecated-filter-hooks.php 3 months ago class-wc-discounts.php 11 months ago class-wc-download-handler.php 1 month ago class-wc-emails.php 1 month ago class-wc-embed.php 1 year ago class-wc-form-handler.php 3 months ago class-wc-frontend-scripts.php 2 months ago class-wc-geo-ip.php 9 months ago class-wc-geolite-integration.php 6 years ago class-wc-geolocation.php 2 months ago class-wc-https.php 3 years ago class-wc-install.php 6 days ago class-wc-integrations.php 5 years ago class-wc-log-levels.php 2 years ago class-wc-logger.php 4 months ago class-wc-meta-data.php 1 month ago class-wc-order-factory.php 2 months ago class-wc-order-item-coupon.php 4 years ago class-wc-order-item-fee.php 5 months ago class-wc-order-item-meta.php 4 years ago class-wc-order-item-product.php 1 month ago class-wc-order-item-shipping.php 5 months ago class-wc-order-item-tax.php 1 month ago class-wc-order-item.php 1 month ago class-wc-order-query.php 1 month ago class-wc-order-refund.php 1 year ago class-wc-order.php 1 month ago class-wc-payment-gateways.php 1 month ago class-wc-payment-tokens.php 3 years ago class-wc-post-data.php 2 months ago class-wc-post-types.php 1 month ago class-wc-privacy-background-process.php 1 year ago class-wc-privacy-erasers.php 10 months ago class-wc-privacy-exporters.php 4 years ago class-wc-privacy.php 1 year ago class-wc-product-attribute.php 4 months ago class-wc-product-download.php 4 months ago class-wc-product-external.php 1 year ago class-wc-product-factory.php 3 months ago class-wc-product-grouped.php 3 months ago class-wc-product-query.php 4 months ago class-wc-product-simple.php 11 months ago class-wc-product-variable.php 1 month ago class-wc-product-variation.php 1 month ago class-wc-query.php 1 month ago class-wc-rate-limiter.php 4 years ago class-wc-regenerate-images-request.php 3 years ago class-wc-regenerate-images.php 2 years ago class-wc-register-wp-admin-settings.php 5 years ago class-wc-rest-authentication.php 2 years ago class-wc-rest-exception.php 5 years ago class-wc-session-handler.php 3 months ago class-wc-shipping-rate.php 1 year ago class-wc-shipping-zone.php 5 years ago class-wc-shipping-zones.php 7 months ago class-wc-shipping.php 2 months ago class-wc-shortcodes.php 1 year ago class-wc-structured-data.php 1 month ago class-wc-tax.php 2 months ago class-wc-template-loader.php 7 months ago class-wc-tracker.php 1 month ago class-wc-validation.php 2 years ago class-wc-webhook.php 1 month ago class-woocommerce.php 1 day ago wc-account-functions.php 1 month ago wc-attribute-functions.php 1 month ago wc-brands-functions.php 1 year ago wc-cart-functions.php 1 month ago wc-conditional-functions.php 11 months ago wc-core-functions.php 2 months ago wc-coupon-functions.php 5 months ago wc-deprecated-functions.php 1 month ago wc-formatting-functions.php 1 month ago wc-interactivity-api-functions.php 2 months ago wc-notice-functions.php 5 months ago wc-order-functions.php 1 month ago wc-order-item-functions.php 3 years ago wc-order-step-logger-functions.php 4 months ago wc-page-functions.php 1 month ago wc-product-functions.php 2 months ago wc-rest-functions.php 7 months ago wc-stock-functions.php 7 months ago wc-template-functions.php 1 month ago wc-template-hooks.php 10 months ago wc-term-functions.php 2 months ago wc-update-functions.php 6 days ago wc-user-functions.php 1 month ago wc-webhook-functions.php 1 month ago wc-widget-functions.php 5 years ago
class-wc-shipping.php
450 lines
1 <?php
2 /**
3 * WooCommerce Shipping
4 *
5 * Handles shipping and loads shipping methods via hooks.
6 *
7 * @version 2.6.0
8 * @package WooCommerce\Classes\Shipping
9 */
10
11 use Automattic\Jetpack\Constants;
12 use Automattic\WooCommerce\StoreApi\Utilities\LocalPickupUtils;
13
14 if ( ! defined( 'ABSPATH' ) ) {
15 exit;
16 }
17
18 /**
19 * Shipping class.
20 */
21 class WC_Shipping {
22
23 /**
24 * True if shipping is enabled.
25 *
26 * @var bool
27 */
28 public $enabled = false;
29
30 /**
31 * Stores methods loaded into woocommerce.
32 *
33 * @var array|null
34 */
35 public $shipping_methods = null;
36
37 /**
38 * Stores the shipping classes.
39 *
40 * @var array
41 */
42 public $shipping_classes = array();
43
44 /**
45 * Stores packages to ship and to get quotes for.
46 *
47 * @var array
48 */
49 public $packages = array();
50
51 /**
52 * The single instance of the class
53 *
54 * @var WC_Shipping
55 * @since 2.1
56 */
57 protected static $_instance = null;
58
59 /**
60 * Main WC_Shipping Instance.
61 *
62 * Ensures only one instance of WC_Shipping is loaded or can be loaded.
63 *
64 * @since 2.1
65 * @return WC_Shipping Main instance
66 */
67 public static function instance() {
68 if ( is_null( self::$_instance ) ) {
69 self::$_instance = new self();
70 }
71 return self::$_instance;
72 }
73
74 /**
75 * Cloning is forbidden.
76 *
77 * @since 2.1
78 */
79 public function __clone() {
80 wc_doing_it_wrong( __FUNCTION__, __( 'Cloning is forbidden.', 'woocommerce' ), '2.1' );
81 }
82
83 /**
84 * Unserializing instances of this class is forbidden.
85 *
86 * @since 2.1
87 */
88 public function __wakeup() {
89 wc_doing_it_wrong( __FUNCTION__, __( 'Unserializing instances of this class is forbidden.', 'woocommerce' ), '2.1' );
90 }
91
92 /**
93 * Magic getter.
94 *
95 * @param string $name Property name.
96 * @return mixed
97 */
98 public function __get( $name ) {
99 // Grab from cart for backwards compatibility with versions prior to 3.2.
100 if ( 'shipping_total' === $name ) {
101 return WC()->cart->get_shipping_total();
102 }
103 if ( 'shipping_taxes' === $name ) {
104 return WC()->cart->get_shipping_taxes();
105 }
106 }
107
108 /**
109 * Initialize shipping.
110 */
111 public function __construct() {
112 $this->enabled = wc_shipping_enabled();
113
114 if ( $this->enabled ) {
115 $this->init();
116 }
117 }
118
119 /**
120 * Initialize shipping.
121 */
122 public function init() {
123 do_action( 'woocommerce_shipping_init' );
124 }
125
126 /**
127 * Shipping methods register themselves by returning their main class name through the woocommerce_shipping_methods filter.
128 *
129 * @return array
130 */
131 public function get_shipping_method_class_names() {
132 // Unique Method ID => Method Class name.
133 $shipping_methods = array(
134 'flat_rate' => 'WC_Shipping_Flat_Rate',
135 'free_shipping' => 'WC_Shipping_Free_Shipping',
136 'local_pickup' => 'WC_Shipping_Local_Pickup',
137 );
138
139 // For backwards compatibility with 2.5.x we load any ENABLED legacy shipping methods here.
140 $maybe_load_legacy_methods = array( 'flat_rate', 'free_shipping', 'international_delivery', 'local_delivery', 'local_pickup' );
141
142 // Prime caches to reduce future queries.
143 wp_prime_option_caches( array_map( fn( string $method ) => sprintf( 'woocommerce_%s_settings', $method ), $maybe_load_legacy_methods ) );
144
145 foreach ( $maybe_load_legacy_methods as $method ) {
146 $options = get_option( 'woocommerce_' . $method . '_settings' );
147 if ( $options && isset( $options['enabled'] ) && 'yes' === $options['enabled'] ) {
148 $shipping_methods[ 'legacy_' . $method ] = 'WC_Shipping_Legacy_' . $method;
149 }
150 }
151
152 return apply_filters( 'woocommerce_shipping_methods', $shipping_methods );
153 }
154
155 /**
156 * Loads all shipping methods which are hooked in.
157 * If a $package is passed, some methods may add themselves conditionally and zones will be used.
158 *
159 * @param array $package Package information.
160 * @return WC_Shipping_Method[]
161 */
162 public function load_shipping_methods( $package = array() ) {
163 if ( ! empty( $package ) ) {
164 $debug_mode = 'yes' === get_option( 'woocommerce_shipping_debug_mode', 'no' );
165 $shipping_zone = WC_Shipping_Zones::get_zone_matching_package( $package );
166 $this->shipping_methods = $shipping_zone->get_shipping_methods( true );
167
168 // translators: %s: shipping zone name.
169 $matched_zone_notice = sprintf( __( 'Customer matched zone "%s"', 'woocommerce' ), $shipping_zone->get_zone_name() );
170
171 // Debug output.
172 if ( $debug_mode && ! Constants::is_defined( 'WOOCOMMERCE_CHECKOUT' ) && ! Constants::is_defined( 'WC_DOING_AJAX' ) && ! wc_has_notice( $matched_zone_notice ) ) {
173 wc_add_notice( $matched_zone_notice );
174 }
175 } else {
176 $this->shipping_methods = array();
177 }
178
179 // For the settings in the backend, and for non-shipping zone methods, we still need to load any registered classes here.
180 foreach ( $this->get_shipping_method_class_names() as $method_id => $method_class ) {
181 $this->register_shipping_method( $method_class );
182 }
183
184 // Methods can register themselves manually through this hook if necessary.
185 do_action( 'woocommerce_load_shipping_methods', $package );
186
187 // Return loaded methods.
188 return $this->get_shipping_methods();
189 }
190
191 /**
192 * Register a shipping method.
193 *
194 * @param object|string $method Either the name of the method's class, or an instance of the method's class.
195 *
196 * @return bool|void
197 */
198 public function register_shipping_method( $method ) {
199 if ( ! is_object( $method ) ) {
200 if ( ! class_exists( $method ) ) {
201 return false;
202 }
203 $method = new $method();
204 }
205 if ( is_null( $this->shipping_methods ) ) {
206 $this->shipping_methods = array();
207 }
208 $this->shipping_methods[ $method->id ] = $method;
209 }
210
211 /**
212 * Unregister shipping methods.
213 */
214 public function unregister_shipping_methods() {
215 $this->shipping_methods = null;
216 }
217
218 /**
219 * Returns all registered shipping methods for usage.
220 *
221 * @return WC_Shipping_Method[]
222 */
223 public function get_shipping_methods() {
224 if ( is_null( $this->shipping_methods ) ) {
225 $this->load_shipping_methods();
226 }
227 return $this->shipping_methods;
228 }
229
230 /**
231 * Get an array of shipping classes.
232 *
233 * @return array
234 */
235 public function get_shipping_classes() {
236 if ( empty( $this->shipping_classes ) ) {
237 $classes = get_terms(
238 'product_shipping_class',
239 array(
240 'hide_empty' => '0',
241 'orderby' => 'name',
242 )
243 );
244 $this->shipping_classes = ! is_wp_error( $classes ) ? $classes : array();
245 }
246 return apply_filters( 'woocommerce_get_shipping_classes', $this->shipping_classes );
247 }
248
249 /**
250 * Calculate shipping for (multiple) packages of cart items.
251 *
252 * @param array $packages multi-dimensional array of cart items to calc shipping for.
253 * @return array Array of calculated packages.
254 */
255 public function calculate_shipping( $packages = array() ) {
256 $this->packages = array();
257
258 if ( ! $this->enabled || empty( $packages ) ) {
259 return array();
260 }
261
262 // Calculate costs for passed packages.
263 foreach ( $packages as $package_key => $package ) {
264 $this->packages[ $package_key ] = $this->calculate_shipping_for_package( $package, $package_key );
265 }
266
267 /**
268 * Allow packages to be reorganized after calculating the shipping.
269 *
270 * This filter can be used to apply some extra manipulation after the shipping costs are calculated for the packages
271 * but before WooCommerce does anything with them. A good example of usage is to merge the shipping methods for multiple
272 * packages for marketplaces.
273 *
274 * @since 2.6.0
275 *
276 * @param array $packages The array of packages after shipping costs are calculated.
277 */
278 $this->packages = array_filter( (array) apply_filters( 'woocommerce_shipping_packages', $this->packages ) );
279
280 return $this->packages;
281 }
282
283 /**
284 * See if package is shippable.
285 *
286 * Packages are shippable until proven otherwise e.g. after getting a shipping country.
287 *
288 * @param array $package Package of cart items.
289 * @return bool
290 */
291 public function is_package_shippable( $package ) {
292 // Packages are shippable until proven otherwise.
293 if ( empty( $package['destination']['country'] ) ) {
294 return true;
295 }
296
297 $allowed = array_keys( WC()->countries->get_shipping_countries() );
298 return in_array( $package['destination']['country'], $allowed, true );
299 }
300
301 /**
302 * Calculate shipping rates for a package,
303 *
304 * Calculates each shipping methods cost. Rates are stored in the session based on the package hash to avoid re-calculation every page load.
305 *
306 * @param array $package Package of cart items.
307 * @param int $package_key Index of the package being calculated. Used to cache multiple package rates.
308 *
309 * @return array|bool
310 */
311 public function calculate_shipping_for_package( $package = array(), $package_key = 0 ) {
312 // If shipping is disabled or the package is invalid, return false.
313 if ( ! $this->enabled || empty( $package ) ) {
314 return false;
315 }
316
317 $package['rates'] = array();
318
319 // If the package is not shippable, e.g. trying to ship to an invalid country, do not calculate rates. We can return
320 // local pickup rates here however since those are not shipped.
321 $is_shippable = $this->is_package_shippable( $package );
322
323 // Check if we need to recalculate shipping for this package.
324 $package_to_hash = $package;
325
326 // Remove data objects so hashes are consistent.
327 foreach ( $package_to_hash['contents'] as $item_id => $item ) {
328 unset( $package_to_hash['contents'][ $item_id ]['data'] );
329 }
330
331 // Get rates stored in the WC session data for this package.
332 $wc_session_key = 'shipping_for_package_' . $package_key;
333 $stored_rates = WC()->session->get( $wc_session_key );
334
335 // Calculate the hash for this package so we can tell if it's changed since last calculation.
336 $package_hash = 'wc_ship_' . md5( wp_json_encode( $package_to_hash ) . WC_Cache_Helper::get_transient_version( 'shipping' ) );
337
338 if ( ! is_array( $stored_rates ) || $package_hash !== $stored_rates['package_hash'] || 'yes' === get_option( 'woocommerce_shipping_debug_mode', 'no' ) ) {
339 foreach ( $this->load_shipping_methods( $package ) as $shipping_method ) {
340 // If the package is not shippable and the shipping method does not support local pickup, skip it.
341 if ( ! $is_shippable && ! LocalPickupUtils::is_local_pickup_method( $shipping_method->id ) ) {
342 continue;
343 }
344
345 if ( ! $shipping_method->supports( 'shipping-zones' ) || $shipping_method->get_instance_id() ) {
346 /**
347 * Fires before getting shipping rates for a package.
348 *
349 * @since 4.3.0
350 * @param array $package Package of cart items.
351 * @param WC_Shipping_Method $shipping_method Shipping method instance.
352 */
353 do_action( 'woocommerce_before_get_rates_for_package', $package, $shipping_method );
354
355 // Use + instead of array_merge to maintain numeric keys.
356 $package['rates'] = $package['rates'] + $shipping_method->get_rates_for_package( $package );
357
358 /**
359 * Fires after getting shipping rates for a package.
360 *
361 * @since 4.3.0
362 * @param array $package Package of cart items.
363 * @param WC_Shipping_Method $shipping_method Shipping method instance.
364 */
365 do_action( 'woocommerce_after_get_rates_for_package', $package, $shipping_method );
366 }
367 }
368
369 // Hide shipping rates when free shipping is available.
370 if ( 'yes' === get_option( 'woocommerce_shipping_hide_rates_when_free', 'no' ) ) {
371 $free_shipping = array();
372 $local_pickup = array();
373
374 foreach ( $package['rates'] as $rate ) {
375 if ( 'free_shipping' === $rate->method_id ) {
376 $free_shipping[ $rate->id ] = $rate;
377 continue;
378 }
379
380 if ( $this->shipping_methods[ $rate->method_id ]->supports( 'local-pickup' ) || 'local_pickup' === $rate->method_id ) {
381 $local_pickup[ $rate->id ] = $rate;
382 }
383 }
384
385 if ( ! empty( $free_shipping ) ) {
386 $package['rates'] = array_merge( $free_shipping, $local_pickup );
387 }
388 }
389
390 /**
391 * Filter the calculated shipping rates.
392 *
393 * @see https://gist.github.com/woogists/271654709e1d27648546e83253c1a813 for cache invalidation methods.
394 * @since 2.0.0
395 * @param array $package['rates'] Package rates.
396 * @param array $package Package of cart items.
397 */
398 $package['rates'] = apply_filters( 'woocommerce_package_rates', $package['rates'], $package );
399
400 // Package rates should be an array, if it was filtered into a non-array, reset it. Don't reset to the
401 // unfiltered value, as e.g. a 3pd could have set it to "false" to remove rates.
402 if ( ! is_array( $package['rates'] ) ) {
403 $package['rates'] = array();
404 }
405
406 // Store in session to avoid recalculation.
407 WC()->session->set(
408 $wc_session_key,
409 array(
410 'package_hash' => $package_hash,
411 'rates' => $package['rates'],
412 )
413 );
414 } else {
415 $package['rates'] = $stored_rates['rates'];
416 }
417
418 return $package;
419 }
420
421 /**
422 * Get packages.
423 *
424 * @return array
425 */
426 public function get_packages() {
427 return $this->packages;
428 }
429
430 /**
431 * Reset shipping.
432 *
433 * Reset the totals for shipping as a whole.
434 */
435 public function reset_shipping() {
436 unset( WC()->session->chosen_shipping_methods );
437 $this->packages = array();
438 }
439
440 /**
441 * Deprecated
442 *
443 * @deprecated 2.6.0 Was previously used to determine sort order of methods, but this is now controlled by zones and thus unused.
444 */
445 public function sort_shipping_methods() {
446 wc_deprecated_function( 'sort_shipping_methods', '2.6' );
447 return $this->shipping_methods;
448 }
449 }
450