class-wc-shipping.php 10 KB

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