PageRenderTime 49ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/woocommerce/includes/abstracts/abstract-wc-shipping-method.php

https://gitlab.com/iamgraeme/royalmile
PHP | 509 lines | 216 code | 72 blank | 221 comment | 37 complexity | 5b361a64a30abb86ad33c881de429b6d MD5 | raw file
  1. <?php
  2. if ( ! defined( 'ABSPATH' ) ) {
  3. exit;
  4. }
  5. /**
  6. * WooCommerce Shipping Method Class.
  7. *
  8. * Extended by shipping methods to handle shipping calculations etc.
  9. *
  10. * @class WC_Shipping_Method
  11. * @version 2.6.0
  12. * @package WooCommerce/Abstracts
  13. * @category Abstract Class
  14. * @author WooThemes
  15. */
  16. abstract class WC_Shipping_Method extends WC_Settings_API {
  17. /**
  18. * Features this method supports. Possible features used by core:
  19. * - shipping-zones Shipping zone functionality + instances
  20. * - instance-settings Instance settings screens.
  21. * - settings Non-instance settings screens. Enabled by default for BW compatibility with methods before instances existed.
  22. * - instance-settings-modal Allows the instance settings to be loaded within a modal in the zones UI.
  23. * @var array
  24. */
  25. public $supports = array( 'settings' );
  26. /**
  27. * Unique ID for the shipping method - must be set.
  28. * @var string
  29. */
  30. public $id = '';
  31. /**
  32. * Method title.
  33. * @var string
  34. */
  35. public $method_title = '';
  36. /**
  37. * Method description.
  38. * @var string
  39. */
  40. public $method_description = '';
  41. /**
  42. * yes or no based on whether the method is enabled.
  43. * @var string
  44. */
  45. public $enabled = 'yes';
  46. /**
  47. * Shipping method title for the frontend.
  48. * @var string
  49. */
  50. public $title;
  51. /**
  52. * This is an array of rates - methods must populate this array to register shipping costs.
  53. * @var array
  54. */
  55. public $rates = array();
  56. /**
  57. * If 'taxable' tax will be charged for this method (if applicable).
  58. * @var string
  59. */
  60. public $tax_status = 'taxable';
  61. /**
  62. * Fee for the method (if applicable).
  63. * @var string
  64. */
  65. public $fee = null;
  66. /**
  67. * Minimum fee for the method (if applicable).
  68. * @var string
  69. */
  70. public $minimum_fee = null;
  71. /**
  72. * Instance ID if used.
  73. * @var int
  74. */
  75. public $instance_id = 0;
  76. /**
  77. * Instance form fields.
  78. * @var array
  79. */
  80. public $instance_form_fields = array();
  81. /**
  82. * Instance settings.
  83. * @var array
  84. */
  85. public $instance_settings = array();
  86. /**
  87. * Availability - legacy. Used for method Availability.
  88. * No longer useful for instance based shipping methods.
  89. * @deprecated 2.6.0
  90. * @var string
  91. */
  92. public $availability;
  93. /**
  94. * Availability countries - legacy. Used for method Availability.
  95. * No longer useful for instance based shipping methods.
  96. * @deprecated 2.6.0
  97. * @var array
  98. */
  99. public $countries = array();
  100. /**
  101. * Constructor.
  102. * @param int $instance_id
  103. */
  104. public function __construct( $instance_id = 0 ) {
  105. $this->instance_id = absint( $instance_id );
  106. }
  107. /**
  108. * Check if a shipping method supports a given feature.
  109. *
  110. * Methods should override this to declare support (or lack of support) for a feature.
  111. *
  112. * @param $feature string The name of a feature to test support for.
  113. * @return bool True if the shipping method supports the feature, false otherwise.
  114. */
  115. public function supports( $feature ) {
  116. return apply_filters( 'woocommerce_shipping_method_supports', in_array( $feature, $this->supports ), $feature, $this );
  117. }
  118. /**
  119. * Called to calculate shipping rates for this method. Rates can be added using the add_rate() method.
  120. */
  121. public function calculate_shipping( $package = array() ) {}
  122. /**
  123. * Whether or not we need to calculate tax on top of the shipping rate.
  124. * @return boolean
  125. */
  126. public function is_taxable() {
  127. return wc_tax_enabled() && 'taxable' === $this->tax_status && ! WC()->customer->is_vat_exempt();
  128. }
  129. /**
  130. * Whether or not this method is enabled in settings.
  131. * @since 2.6.0
  132. * @return boolean
  133. */
  134. public function is_enabled() {
  135. return 'yes' === $this->enabled;
  136. }
  137. /**
  138. * Return the shipping method instance ID.
  139. * @since 2.6.0
  140. * @return int
  141. */
  142. public function get_instance_id() {
  143. return $this->instance_id;
  144. }
  145. /**
  146. * Return the shipping method title.
  147. * @since 2.6.0
  148. * @return string
  149. */
  150. public function get_method_title() {
  151. return apply_filters( 'woocommerce_shipping_method_title', $this->method_title, $this );
  152. }
  153. /**
  154. * Return the shipping method description.
  155. * @since 2.6.0
  156. * @return string
  157. */
  158. public function get_method_description() {
  159. return apply_filters( 'woocommerce_shipping_method_description', $this->method_description, $this );
  160. }
  161. /**
  162. * Return the shipping title which is user set.
  163. *
  164. * @return string
  165. */
  166. public function get_title() {
  167. return apply_filters( 'woocommerce_shipping_method_title', $this->title, $this->id );
  168. }
  169. /**
  170. * Return calculated rates for a package.
  171. * @since 2.6.0
  172. * @param object $package
  173. * @return array
  174. */
  175. public function get_rates_for_package( $package ) {
  176. $this->rates = array();
  177. if ( $this->is_available( $package ) && ( empty( $package['ship_via'] ) || in_array( $this->id, $package['ship_via'] ) ) ) {
  178. $this->calculate_shipping( $package );
  179. }
  180. return $this->rates;
  181. }
  182. /**
  183. * Returns a rate ID based on this methods ID and instance, with an optional
  184. * suffix if distinguishing between multiple rates.
  185. * @since 2.6.0
  186. * @param string $suffix
  187. * @return string
  188. */
  189. public function get_rate_id( $suffix = '' ) {
  190. $rate_id = array( $this->id );
  191. if ( $this->instance_id ) {
  192. $rate_id[] = $this->instance_id;
  193. }
  194. if ( $suffix ) {
  195. $rate_id[] = $suffix;
  196. }
  197. return implode( ':', $rate_id );
  198. }
  199. /**
  200. * Add a shipping rate. If taxes are not set they will be calculated based on cost.
  201. * @param array $args (default: array())
  202. */
  203. public function add_rate( $args = array() ) {
  204. $args = wp_parse_args( $args, array(
  205. 'id' => $this->get_rate_id(), // ID for the rate. If not passed, this id:instance default will be used.
  206. 'label' => '', // Label for the rate
  207. 'cost' => '0', // Amount or array of costs (per item shipping)
  208. 'taxes' => '', // Pass taxes, or leave empty to have it calculated for you, or 'false' to disable calculations
  209. 'calc_tax' => 'per_order', // Calc tax per_order or per_item. Per item needs an array of costs
  210. 'meta_data' => array(), // Array of misc meta data to store along with this rate - key value pairs.
  211. 'package' => false, // Package array this rate was generated for @since 2.6.0
  212. ) );
  213. // ID and label are required
  214. if ( ! $args['id'] || ! $args['label'] ) {
  215. return;
  216. }
  217. // Total up the cost
  218. $total_cost = is_array( $args['cost'] ) ? array_sum( $args['cost'] ) : $args['cost'];
  219. $taxes = $args['taxes'];
  220. // Taxes - if not an array and not set to false, calc tax based on cost and passed calc_tax variable. This saves shipping methods having to do complex tax calculations.
  221. if ( ! is_array( $taxes ) && $taxes !== false && $total_cost > 0 && $this->is_taxable() ) {
  222. $taxes = 'per_item' === $args['calc_tax'] ? $this->get_taxes_per_item( $args['cost'] ) : WC_Tax::calc_shipping_tax( $total_cost, WC_Tax::get_shipping_tax_rates() );
  223. }
  224. // Round the total cost after taxes have been calculated.
  225. $total_cost = wc_format_decimal( $total_cost, wc_get_price_decimals() );
  226. // Create rate object
  227. $rate = new WC_Shipping_Rate( $args['id'], $args['label'], $total_cost, $taxes, $this->id );
  228. if ( ! empty( $args['meta_data'] ) ) {
  229. foreach ( $args['meta_data'] as $key => $value ) {
  230. $rate->add_meta_data( $key, $value );
  231. }
  232. }
  233. // Store package data
  234. if ( $args['package'] ) {
  235. $items_in_package = array();
  236. foreach ( $args['package']['contents'] as $item ) {
  237. $product = $item['data'];
  238. $items_in_package[] = $product->get_title() . ' &times; ' . $item['quantity'];
  239. }
  240. $rate->add_meta_data( __( 'Items', 'woocommerce' ), implode( ', ', $items_in_package ) );
  241. }
  242. $this->rates[ $args['id'] ] = $rate;
  243. }
  244. /**
  245. * Calc taxes per item being shipping in costs array.
  246. * @since 2.6.0
  247. * @access protected
  248. * @param array $costs
  249. * @return array of taxes
  250. */
  251. protected function get_taxes_per_item( $costs ) {
  252. $taxes = array();
  253. // If we have an array of costs we can look up each items tax class and add tax accordingly
  254. if ( is_array( $costs ) ) {
  255. $cart = WC()->cart->get_cart();
  256. foreach ( $costs as $cost_key => $amount ) {
  257. if ( ! isset( $cart[ $cost_key ] ) ) {
  258. continue;
  259. }
  260. $item_taxes = WC_Tax::calc_shipping_tax( $amount, WC_Tax::get_shipping_tax_rates( $cart[ $cost_key ]['data']->get_tax_class() ) );
  261. // Sum the item taxes
  262. foreach ( array_keys( $taxes + $item_taxes ) as $key ) {
  263. $taxes[ $key ] = ( isset( $item_taxes[ $key ] ) ? $item_taxes[ $key ] : 0 ) + ( isset( $taxes[ $key ] ) ? $taxes[ $key ] : 0 );
  264. }
  265. }
  266. // Add any cost for the order - order costs are in the key 'order'
  267. if ( isset( $costs['order'] ) ) {
  268. $item_taxes = WC_Tax::calc_shipping_tax( $costs['order'], WC_Tax::get_shipping_tax_rates() );
  269. // Sum the item taxes
  270. foreach ( array_keys( $taxes + $item_taxes ) as $key ) {
  271. $taxes[ $key ] = ( isset( $item_taxes[ $key ] ) ? $item_taxes[ $key ] : 0 ) + ( isset( $taxes[ $key ] ) ? $taxes[ $key ] : 0 );
  272. }
  273. }
  274. }
  275. return $taxes;
  276. }
  277. /**
  278. * Is this method available?
  279. * @param array $package
  280. * @return bool
  281. */
  282. public function is_available( $package ) {
  283. $available = $this->is_enabled();
  284. // Country availability (legacy, for non-zone based methods)
  285. if ( ! $this->instance_id && $available ) {
  286. $countries = is_array( $this->countries ) ? $this->countries : array();
  287. switch ( $this->availability ) {
  288. case 'specific' :
  289. case 'including' :
  290. $available = in_array( $package['destination']['country'], array_intersect( $countries, array_keys( WC()->countries->get_shipping_countries() ) ) );
  291. break;
  292. case 'excluding' :
  293. $available = in_array( $package['destination']['country'], array_diff( array_keys( WC()->countries->get_shipping_countries() ), $countries ) );
  294. break;
  295. default :
  296. $available = in_array( $package['destination']['country'], array_keys( WC()->countries->get_shipping_countries() ) );
  297. break;
  298. }
  299. }
  300. return apply_filters( 'woocommerce_shipping_' . $this->id . '_is_available', $available, $package );
  301. }
  302. /**
  303. * Get fee to add to shipping cost.
  304. * @param string|float $fee
  305. * @param float $total
  306. * @return float
  307. */
  308. public function get_fee( $fee, $total ) {
  309. if ( strstr( $fee, '%' ) ) {
  310. $fee = ( $total / 100 ) * str_replace( '%', '', $fee );
  311. }
  312. if ( ! empty( $this->minimum_fee ) && $this->minimum_fee > $fee ) {
  313. $fee = $this->minimum_fee;
  314. }
  315. return $fee;
  316. }
  317. /**
  318. * Does this method have a settings page?
  319. * @return bool
  320. */
  321. public function has_settings() {
  322. return $this->instance_id ? $this->supports( 'instance-settings' ) : $this->supports( 'settings' );
  323. }
  324. /**
  325. * Return admin options as a html string.
  326. * @return string
  327. */
  328. public function get_admin_options_html() {
  329. if ( $this->instance_id ) {
  330. $settings_html = $this->generate_settings_html( $this->get_instance_form_fields(), false );
  331. } else {
  332. $settings_html = $this->generate_settings_html( $this->get_form_fields(), false );
  333. }
  334. return '<table class="form-table">' . $settings_html . '</table>';
  335. }
  336. /**
  337. * Output the shipping settings screen.
  338. */
  339. public function admin_options() {
  340. if ( ! $this->instance_id ) {
  341. echo '<h2>' . esc_html( $this->get_method_title() ) . '</h2>';
  342. }
  343. echo wp_kses_post( wpautop( $this->get_method_description() ) );
  344. echo $this->get_admin_options_html();
  345. }
  346. /**
  347. * get_option function.
  348. *
  349. * Gets and option from the settings API, using defaults if necessary to prevent undefined notices.
  350. *
  351. * @param string $key
  352. * @param mixed $empty_value
  353. * @return mixed The value specified for the option or a default value for the option.
  354. */
  355. public function get_option( $key, $empty_value = null ) {
  356. // Instance options take priority over global options
  357. if ( array_key_exists( $key, $this->get_instance_form_fields() ) ) {
  358. return $this->get_instance_option( $key, $empty_value );
  359. }
  360. // Return global option
  361. return parent::get_option( $key, $empty_value );
  362. }
  363. /**
  364. * Gets an option from the settings API, using defaults if necessary to prevent undefined notices.
  365. *
  366. * @param string $key
  367. * @param mixed $empty_value
  368. * @return mixed The value specified for the option or a default value for the option.
  369. */
  370. public function get_instance_option( $key, $empty_value = null ) {
  371. if ( empty( $this->instance_settings ) ) {
  372. $this->init_instance_settings();
  373. }
  374. // Get option default if unset.
  375. if ( ! isset( $this->instance_settings[ $key ] ) ) {
  376. $form_fields = $this->get_instance_form_fields();
  377. $this->instance_settings[ $key ] = $this->get_field_default( $form_fields[ $key ] );
  378. }
  379. if ( ! is_null( $empty_value ) && '' === $this->instance_settings[ $key ] ) {
  380. $this->instance_settings[ $key ] = $empty_value;
  381. }
  382. return $this->instance_settings[ $key ];
  383. }
  384. /**
  385. * Get settings fields for instances of this shipping method (within zones).
  386. * Should be overridden by shipping methods to add options.
  387. * @since 2.6.0
  388. * @return array
  389. */
  390. public function get_instance_form_fields() {
  391. return apply_filters( 'woocommerce_shipping_instance_form_fields_' . $this->id, array_map( array( $this, 'set_defaults' ), $this->instance_form_fields ) );
  392. }
  393. /**
  394. * Return the name of the option in the WP DB.
  395. * @since 2.6.0
  396. * @return string
  397. */
  398. public function get_instance_option_key() {
  399. return $this->instance_id ? $this->plugin_id . $this->id . '_' . $this->instance_id . '_settings' : '';
  400. }
  401. /**
  402. * Initialise Settings for instances.
  403. * @since 2.6.0
  404. */
  405. public function init_instance_settings() {
  406. $this->instance_settings = get_option( $this->get_instance_option_key(), null );
  407. // If there are no settings defined, use defaults.
  408. if ( ! is_array( $this->instance_settings ) ) {
  409. $form_fields = $this->get_instance_form_fields();
  410. $this->instance_settings = array_merge( array_fill_keys( array_keys( $form_fields ), '' ), wp_list_pluck( $form_fields, 'default' ) );
  411. }
  412. }
  413. /**
  414. * Processes and saves options.
  415. * If there is an error thrown, will continue to save and validate fields, but will leave the erroring field out.
  416. * @since 2.6.0
  417. * @return bool was anything saved?
  418. */
  419. public function process_admin_options() {
  420. if ( $this->instance_id ) {
  421. $this->init_instance_settings();
  422. $post_data = $this->get_post_data();
  423. foreach ( $this->get_instance_form_fields() as $key => $field ) {
  424. if ( 'title' !== $this->get_field_type( $field ) ) {
  425. try {
  426. $this->instance_settings[ $key ] = $this->get_field_value( $key, $field, $post_data );
  427. } catch ( Exception $e ) {
  428. $this->add_error( $e->getMessage() );
  429. }
  430. }
  431. }
  432. return update_option( $this->get_instance_option_key(), apply_filters( 'woocommerce_shipping_' . $this->id . '_instance_settings_values', $this->instance_settings, $this ) );
  433. } else {
  434. return parent::process_admin_options();
  435. }
  436. }
  437. }