abstract-wc-shipping-method.php 15 KB

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