Нет описания

class-wc-shipping.php 11KB

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