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