PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/woocommerce/includes/class-wc-shipping.php

https://gitlab.com/webkod3r/tripolis
PHP | 442 lines | 215 code | 79 blank | 148 comment | 40 complexity | 010eef053d6ab1c0de0900854ff647c5 MD5 | raw file
  1. <?php
  2. /**
  3. * WooCommerce Shipping Class
  4. *
  5. * Handles shipping and loads shipping methods via hooks.
  6. *
  7. * @class WC_Shipping
  8. * @version 2.3.0
  9. * @package WooCommerce/Classes/Shipping
  10. * @category Class
  11. * @author WooThemes
  12. */
  13. if ( ! defined( 'ABSPATH' ) ) {
  14. exit; // Exit if accessed directly
  15. }
  16. class WC_Shipping {
  17. /** @var bool True if shipping is enabled. */
  18. public $enabled = false;
  19. /** @var array Stores methods loaded into woocommerce. */
  20. public $shipping_methods = array();
  21. /** @var float Stores the cost of shipping */
  22. public $shipping_total = 0;
  23. /** @var array Stores an array of shipping taxes. */
  24. public $shipping_taxes = array();
  25. /** @var array Stores the shipping classes. */
  26. public $shipping_classes = array();
  27. /** @var array Stores packages to ship and to get quotes for. */
  28. public $packages = array();
  29. /**
  30. * @var WC_Shipping The single instance of the class
  31. * @since 2.1
  32. */
  33. protected static $_instance = null;
  34. /**
  35. * Main WC_Shipping Instance.
  36. *
  37. * Ensures only one instance of WC_Shipping is loaded or can be loaded.
  38. *
  39. * @since 2.1
  40. * @static
  41. * @return WC_Shipping Main instance
  42. */
  43. public static function instance() {
  44. if ( is_null( self::$_instance ) )
  45. self::$_instance = new self();
  46. return self::$_instance;
  47. }
  48. /**
  49. * Cloning is forbidden.
  50. *
  51. * @since 2.1
  52. */
  53. public function __clone() {
  54. _doing_it_wrong( __FUNCTION__, __( 'Cheatin&#8217; huh?', 'woocommerce' ), '2.1' );
  55. }
  56. /**
  57. * Unserializing instances of this class is forbidden.
  58. *
  59. * @since 2.1
  60. */
  61. public function __wakeup() {
  62. _doing_it_wrong( __FUNCTION__, __( 'Cheatin&#8217; huh?', 'woocommerce' ), '2.1' );
  63. }
  64. /**
  65. * Initialize shipping.
  66. */
  67. public function __construct() {
  68. $this->init();
  69. }
  70. /**
  71. * Initialize shipping.
  72. */
  73. public function init() {
  74. do_action( 'woocommerce_shipping_init' );
  75. $this->enabled = ( get_option('woocommerce_calc_shipping') == 'no' ) ? false : true;
  76. }
  77. /**
  78. * Loads all shipping methods which are hooked in. If a $package is passed some methods may add themselves conditionally.
  79. *
  80. * Methods are sorted into their user-defined order after being loaded.
  81. *
  82. * @access public
  83. * @param array $package
  84. * @return array
  85. */
  86. public function load_shipping_methods( $package = array() ) {
  87. $this->unregister_shipping_methods();
  88. // Methods can register themselves through this hook
  89. do_action( 'woocommerce_load_shipping_methods', $package );
  90. // Register methods through a filter
  91. $shipping_methods_to_load = apply_filters( 'woocommerce_shipping_methods', array(
  92. 'WC_Shipping_Flat_Rate',
  93. 'WC_Shipping_Free_Shipping',
  94. 'WC_Shipping_International_Delivery',
  95. 'WC_Shipping_Local_Delivery',
  96. 'WC_Shipping_Local_Pickup'
  97. ) );
  98. foreach ( $shipping_methods_to_load as $method ) {
  99. $this->register_shipping_method( $method );
  100. }
  101. $this->sort_shipping_methods();
  102. return $this->shipping_methods;
  103. }
  104. /**
  105. * Register a shipping method for use in calculations.
  106. *
  107. * @param object|string $method Either the name of the method's class, or an instance of the method's class
  108. */
  109. public function register_shipping_method( $method ) {
  110. if ( ! is_object( $method ) ) {
  111. $method = new $method();
  112. }
  113. $id = empty( $method->instance_id ) ? $method->id : $method->instance_id;
  114. $this->shipping_methods[ $id ] = $method;
  115. }
  116. /**
  117. * Unregister shipping methods.
  118. */
  119. public function unregister_shipping_methods() {
  120. $this->shipping_methods = array();
  121. }
  122. /**
  123. * Sort shipping methods.
  124. *
  125. * Sorts shipping methods into the user defined order.
  126. *
  127. * @return array
  128. */
  129. public function sort_shipping_methods() {
  130. $sorted_shipping_methods = array();
  131. // Get order option
  132. $ordering = (array) get_option('woocommerce_shipping_method_order');
  133. $order_end = 999;
  134. // Load shipping methods in order
  135. foreach ( $this->shipping_methods as $method ) {
  136. if ( isset( $ordering[ $method->id ] ) && is_numeric( $ordering[ $method->id ] ) ) {
  137. // Add in position
  138. $sorted_shipping_methods[ $ordering[ $method->id ] ][] = $method;
  139. } else {
  140. // Add to end of the array
  141. $sorted_shipping_methods[ $order_end ][] = $method;
  142. }
  143. }
  144. ksort( $sorted_shipping_methods );
  145. $this->shipping_methods = array();
  146. foreach ( $sorted_shipping_methods as $methods )
  147. foreach ( $methods as $method ) {
  148. $id = empty( $method->instance_id ) ? $method->id : $method->instance_id;
  149. $this->shipping_methods[ $id ] = $method;
  150. }
  151. return $this->shipping_methods;
  152. }
  153. /**
  154. * Returns all registered shipping methods for usage.
  155. *
  156. * @access public
  157. * @return array
  158. */
  159. public function get_shipping_methods() {
  160. return $this->shipping_methods;
  161. }
  162. /**
  163. * Load shipping classes taxonomy terms.
  164. *
  165. * @access public
  166. * @return array
  167. */
  168. public function get_shipping_classes() {
  169. if ( empty( $this->shipping_classes ) ) {
  170. $classes = get_terms( 'product_shipping_class', array( 'hide_empty' => '0' ) );
  171. $this->shipping_classes = $classes && ! is_wp_error( $classes ) ? $classes : array();
  172. }
  173. return $this->shipping_classes;
  174. }
  175. /**
  176. * Get the default method.
  177. * @param array $available_methods
  178. * @param boolean $current_chosen_method
  179. * @return string
  180. */
  181. private function get_default_method( $available_methods, $current_chosen_method = false ) {
  182. $selection_priority = get_option( 'woocommerce_shipping_method_selection_priority', array() );
  183. if ( ! empty( $available_methods ) ) {
  184. // Is a method already chosen?
  185. if ( ! empty( $current_chosen_method ) && ! isset( $available_methods[ $current_chosen_method ] ) ) {
  186. foreach ( $available_methods as $method_key => $method ) {
  187. if ( strpos( $method->id, $current_chosen_method ) === 0 ) {
  188. return $method->id;
  189. }
  190. }
  191. }
  192. // Order by priorities and costs
  193. $prioritized_methods = array();
  194. foreach ( $available_methods as $method_key => $method ) {
  195. // Some IDs contain : if they have multiple rates so use $method->method_id
  196. $priority = isset( $selection_priority[ $method->method_id ] ) ? absint( $selection_priority[ $method->method_id ] ): 1;
  197. if ( empty( $prioritized_methods[ $priority ] ) ) {
  198. $prioritized_methods[ $priority ] = array();
  199. }
  200. $prioritized_methods[ $priority ][ $method_key ] = $method->cost;
  201. }
  202. ksort( $prioritized_methods );
  203. $prioritized_methods = current( $prioritized_methods );
  204. asort( $prioritized_methods );
  205. return current( array_keys( $prioritized_methods ) );
  206. }
  207. return false;
  208. }
  209. /**
  210. * Calculate shipping for (multiple) packages of cart items.
  211. *
  212. * @param array $packages multi-dimensional array of cart items to calc shipping for
  213. */
  214. public function calculate_shipping( $packages = array() ) {
  215. $this->shipping_total = null;
  216. $this->shipping_taxes = array();
  217. $this->packages = array();
  218. if ( ! $this->enabled || empty( $packages ) ) {
  219. return;
  220. }
  221. // Calculate costs for passed packages
  222. $package_keys = array_keys( $packages );
  223. $package_keys_size = sizeof( $package_keys );
  224. for ( $i = 0; $i < $package_keys_size; $i ++ ) {
  225. $this->packages[ $package_keys[ $i ] ] = $this->calculate_shipping_for_package( $packages[ $package_keys[ $i ] ] );
  226. }
  227. // Get all chosen methods
  228. $chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
  229. $method_counts = WC()->session->get( 'shipping_method_counts' );
  230. // Get chosen methods for each package
  231. foreach ( $this->packages as $i => $package ) {
  232. $chosen_method = false;
  233. $method_count = false;
  234. if ( ! empty( $chosen_methods[ $i ] ) ) {
  235. $chosen_method = $chosen_methods[ $i ];
  236. }
  237. if ( ! empty( $method_counts[ $i ] ) ) {
  238. $method_count = $method_counts[ $i ];
  239. }
  240. // Get available methods for package
  241. $available_methods = $package['rates'];
  242. if ( sizeof( $available_methods ) > 0 ) {
  243. // If not set, not available, or available methods have changed, set to the DEFAULT option
  244. if ( empty( $chosen_method ) || ! isset( $available_methods[ $chosen_method ] ) || $method_count != sizeof( $available_methods ) ) {
  245. $chosen_method = apply_filters( 'woocommerce_shipping_chosen_method', $this->get_default_method( $available_methods, $chosen_method ), $available_methods );
  246. $chosen_methods[ $i ] = $chosen_method;
  247. $method_counts[ $i ] = sizeof( $available_methods );
  248. do_action( 'woocommerce_shipping_method_chosen', $chosen_method );
  249. }
  250. // Store total costs
  251. if ( $chosen_method ) {
  252. $rate = $available_methods[ $chosen_method ];
  253. // Merge cost and taxes - label and ID will be the same
  254. $this->shipping_total += $rate->cost;
  255. if ( ! empty( $rate->taxes ) && is_array( $rate->taxes ) ) {
  256. foreach ( array_keys( $this->shipping_taxes + $rate->taxes ) as $key ) {
  257. $this->shipping_taxes[ $key ] = ( isset( $rate->taxes[$key] ) ? $rate->taxes[$key] : 0 ) + ( isset( $this->shipping_taxes[$key] ) ? $this->shipping_taxes[$key] : 0 );
  258. }
  259. }
  260. }
  261. }
  262. }
  263. // Save all chosen methods (array)
  264. WC()->session->set( 'chosen_shipping_methods', $chosen_methods );
  265. WC()->session->set( 'shipping_method_counts', $method_counts );
  266. }
  267. /**
  268. * Calculate shipping rates for a package,
  269. *
  270. * Calculates each shipping methods cost. Rates are stored in the session based on the package hash to avoid re-calculation every page load.
  271. *
  272. * @param array $package cart items
  273. * @return array
  274. */
  275. public function calculate_shipping_for_package( $package = array() ) {
  276. if ( ! $this->enabled || ! $package ) {
  277. return false;
  278. }
  279. // Check if we need to recalculate shipping for this package
  280. $package_hash = 'wc_ship_' . md5( json_encode( $package ) . WC_Cache_Helper::get_transient_version( 'shipping' ) );
  281. $status_options = get_option( 'woocommerce_status_options', array() );
  282. $stored_rates = WC()->session->get( 'shipping_for_package' );
  283. if ( ! is_array( $stored_rates ) || $package_hash !== $stored_rates['package_hash'] || ! empty( $status_options['shipping_debug_mode'] ) ) {
  284. // Calculate shipping method rates
  285. $package['rates'] = array();
  286. foreach ( $this->load_shipping_methods( $package ) as $shipping_method ) {
  287. if ( $shipping_method->is_available( $package ) && ( empty( $package['ship_via'] ) || in_array( $shipping_method->id, $package['ship_via'] ) ) ) {
  288. // Reset Rates
  289. $shipping_method->rates = array();
  290. // Calculate Shipping for package
  291. $shipping_method->calculate_shipping( $package );
  292. // Place rates in package array
  293. if ( ! empty( $shipping_method->rates ) && is_array( $shipping_method->rates ) ) {
  294. foreach ( $shipping_method->rates as $rate ) {
  295. $package['rates'][ $rate->id ] = $rate;
  296. }
  297. }
  298. }
  299. }
  300. // Filter the calculated rates
  301. $package['rates'] = apply_filters( 'woocommerce_package_rates', $package['rates'], $package );
  302. // Store in session to avoid recalculation
  303. WC()->session->set( 'shipping_for_package', array(
  304. 'package_hash' => $package_hash,
  305. 'rates' => $package['rates']
  306. ) );
  307. } else {
  308. $package['rates'] = $stored_rates['rates'];
  309. }
  310. return $package;
  311. }
  312. /**
  313. * Get packages.
  314. *
  315. * @return array
  316. */
  317. public function get_packages() {
  318. return $this->packages;
  319. }
  320. /**
  321. * Reset shipping.
  322. *
  323. * Reset the totals for shipping as a whole.
  324. */
  325. public function reset_shipping() {
  326. unset( WC()->session->chosen_shipping_methods );
  327. $this->shipping_total = null;
  328. $this->shipping_taxes = array();
  329. $this->packages = array();
  330. }
  331. /**
  332. * Process admin options.
  333. *
  334. * Saves options on the shipping setting page.
  335. */
  336. public function process_admin_options() {
  337. $method_order = isset( $_POST['method_order'] ) ? $_POST['method_order'] : '';
  338. $method_priority = isset( $_POST['method_priority'] ) ? $_POST['method_priority'] : '';
  339. $order = array();
  340. $selection_priority = array();
  341. if ( is_array( $method_order ) && sizeof( $method_order ) > 0 ) {
  342. $loop = 0;
  343. foreach ( $method_order as $method_id ) {
  344. $order[ $method_id ] = $loop;
  345. $selection_priority[ $method_id ] = absint( $method_priority[ $method_id ] );
  346. $loop ++;
  347. }
  348. }
  349. update_option( 'woocommerce_shipping_method_selection_priority', $selection_priority );
  350. update_option( 'woocommerce_shipping_method_order', $order );
  351. }
  352. }
  353. /**
  354. * Register a shipping method.
  355. *
  356. * Registers a shipping method ready to be loaded. Accepts a class name (string) or a class object.
  357. *
  358. * @package WooCommerce/Classes/Shipping
  359. * @since 1.5.7
  360. */
  361. function woocommerce_register_shipping_method( $shipping_method ) {
  362. WC()->shipping->register_shipping_method( $shipping_method );
  363. }