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 |