Aucune description

abstract-wc-shipping-method.php 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  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 && ! 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 array $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 = apply_filters(
  235. 'woocommerce_shipping_method_add_rate_args',
  236. wp_parse_args(
  237. $args,
  238. array(
  239. 'id' => $this->get_rate_id(), // ID for the rate. If not passed, this id:instance default will be used.
  240. 'label' => '', // Label for the rate.
  241. 'cost' => '0', // Amount or array of costs (per item shipping).
  242. 'taxes' => '', // Pass taxes, or leave empty to have it calculated for you, or 'false' to disable calculations.
  243. 'calc_tax' => 'per_order', // Calc tax per_order or per_item. Per item needs an array of costs.
  244. 'meta_data' => array(), // Array of misc meta data to store along with this rate - key value pairs.
  245. 'package' => false, // Package array this rate was generated for @since 2.6.0.
  246. 'price_decimals' => wc_get_price_decimals(),
  247. )
  248. ),
  249. $this
  250. );
  251. // ID and label are required.
  252. if ( ! $args['id'] || ! $args['label'] ) {
  253. return;
  254. }
  255. // Total up the cost.
  256. $total_cost = is_array( $args['cost'] ) ? array_sum( $args['cost'] ) : $args['cost'];
  257. $taxes = $args['taxes'];
  258. // 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.
  259. if ( ! is_array( $taxes ) && false !== $taxes && $total_cost > 0 && $this->is_taxable() ) {
  260. $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() );
  261. }
  262. // Round the total cost after taxes have been calculated.
  263. $total_cost = wc_format_decimal( $total_cost, $args['price_decimals'] );
  264. // Create rate object.
  265. $rate = new WC_Shipping_Rate();
  266. $rate->set_id( $args['id'] );
  267. $rate->set_method_id( $this->id );
  268. $rate->set_instance_id( $this->instance_id );
  269. $rate->set_label( $args['label'] );
  270. $rate->set_cost( $total_cost );
  271. $rate->set_taxes( $taxes );
  272. if ( ! empty( $args['meta_data'] ) ) {
  273. foreach ( $args['meta_data'] as $key => $value ) {
  274. $rate->add_meta_data( $key, $value );
  275. }
  276. }
  277. // Store package data.
  278. if ( $args['package'] ) {
  279. $items_in_package = array();
  280. foreach ( $args['package']['contents'] as $item ) {
  281. $product = $item['data'];
  282. $items_in_package[] = $product->get_name() . ' &times; ' . $item['quantity'];
  283. }
  284. $rate->add_meta_data( __( 'Items', 'woocommerce' ), implode( ', ', $items_in_package ) );
  285. }
  286. $this->rates[ $args['id'] ] = apply_filters( 'woocommerce_shipping_method_add_rate', $rate, $args, $this );
  287. }
  288. /**
  289. * Calc taxes per item being shipping in costs array.
  290. *
  291. * @since 2.6.0
  292. * @param array $costs Costs.
  293. * @return array of taxes
  294. */
  295. protected function get_taxes_per_item( $costs ) {
  296. $taxes = array();
  297. // If we have an array of costs we can look up each items tax class and add tax accordingly.
  298. if ( is_array( $costs ) ) {
  299. $cart = WC()->cart->get_cart();
  300. foreach ( $costs as $cost_key => $amount ) {
  301. if ( ! isset( $cart[ $cost_key ] ) ) {
  302. continue;
  303. }
  304. $item_taxes = WC_Tax::calc_shipping_tax( $amount, WC_Tax::get_shipping_tax_rates( $cart[ $cost_key ]['data']->get_tax_class() ) );
  305. // Sum the item taxes.
  306. foreach ( array_keys( $taxes + $item_taxes ) as $key ) {
  307. $taxes[ $key ] = ( isset( $item_taxes[ $key ] ) ? $item_taxes[ $key ] : 0 ) + ( isset( $taxes[ $key ] ) ? $taxes[ $key ] : 0 );
  308. }
  309. }
  310. // Add any cost for the order - order costs are in the key 'order'.
  311. if ( isset( $costs['order'] ) ) {
  312. $item_taxes = WC_Tax::calc_shipping_tax( $costs['order'], WC_Tax::get_shipping_tax_rates() );
  313. // Sum the item taxes.
  314. foreach ( array_keys( $taxes + $item_taxes ) as $key ) {
  315. $taxes[ $key ] = ( isset( $item_taxes[ $key ] ) ? $item_taxes[ $key ] : 0 ) + ( isset( $taxes[ $key ] ) ? $taxes[ $key ] : 0 );
  316. }
  317. }
  318. }
  319. return $taxes;
  320. }
  321. /**
  322. * Is this method available?
  323. *
  324. * @param array $package Package.
  325. * @return bool
  326. */
  327. public function is_available( $package ) {
  328. $available = $this->is_enabled();
  329. // Country availability (legacy, for non-zone based methods).
  330. if ( ! $this->instance_id && $available ) {
  331. $countries = is_array( $this->countries ) ? $this->countries : array();
  332. switch ( $this->availability ) {
  333. case 'specific':
  334. case 'including':
  335. $available = in_array( $package['destination']['country'], array_intersect( $countries, array_keys( WC()->countries->get_shipping_countries() ) ) );
  336. break;
  337. case 'excluding':
  338. $available = in_array( $package['destination']['country'], array_diff( array_keys( WC()->countries->get_shipping_countries() ), $countries ) );
  339. break;
  340. default:
  341. $available = in_array( $package['destination']['country'], array_keys( WC()->countries->get_shipping_countries() ) );
  342. break;
  343. }
  344. }
  345. return apply_filters( 'woocommerce_shipping_' . $this->id . '_is_available', $available, $package, $this );
  346. }
  347. /**
  348. * Get fee to add to shipping cost.
  349. *
  350. * @param string|float $fee Fee.
  351. * @param float $total Total.
  352. * @return float
  353. */
  354. public function get_fee( $fee, $total ) {
  355. if ( strstr( $fee, '%' ) ) {
  356. $fee = ( $total / 100 ) * str_replace( '%', '', $fee );
  357. }
  358. if ( ! empty( $this->minimum_fee ) && $this->minimum_fee > $fee ) {
  359. $fee = $this->minimum_fee;
  360. }
  361. return $fee;
  362. }
  363. /**
  364. * Does this method have a settings page?
  365. *
  366. * @return bool
  367. */
  368. public function has_settings() {
  369. return $this->instance_id ? $this->supports( 'instance-settings' ) : $this->supports( 'settings' );
  370. }
  371. /**
  372. * Return admin options as a html string.
  373. *
  374. * @return string
  375. */
  376. public function get_admin_options_html() {
  377. if ( $this->instance_id ) {
  378. $settings_html = $this->generate_settings_html( $this->get_instance_form_fields(), false );
  379. } else {
  380. $settings_html = $this->generate_settings_html( $this->get_form_fields(), false );
  381. }
  382. return '<table class="form-table">' . $settings_html . '</table>';
  383. }
  384. /**
  385. * Output the shipping settings screen.
  386. */
  387. public function admin_options() {
  388. if ( ! $this->instance_id ) {
  389. echo '<h2>' . esc_html( $this->get_method_title() ) . '</h2>';
  390. }
  391. echo wp_kses_post( wpautop( $this->get_method_description() ) );
  392. echo $this->get_admin_options_html(); // phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped
  393. }
  394. /**
  395. * Get_option function.
  396. *
  397. * Gets and option from the settings API, using defaults if necessary to prevent undefined notices.
  398. *
  399. * @param string $key Key.
  400. * @param mixed $empty_value Empty value.
  401. * @return mixed The value specified for the option or a default value for the option.
  402. */
  403. public function get_option( $key, $empty_value = null ) {
  404. // Instance options take priority over global options.
  405. if ( $this->instance_id && array_key_exists( $key, $this->get_instance_form_fields() ) ) {
  406. return $this->get_instance_option( $key, $empty_value );
  407. }
  408. // Return global option.
  409. $option = apply_filters( 'woocommerce_shipping_' . $this->id . '_option', parent::get_option( $key, $empty_value ), $key, $this );
  410. return $option;
  411. }
  412. /**
  413. * Gets an option from the settings API, using defaults if necessary to prevent undefined notices.
  414. *
  415. * @param string $key Key.
  416. * @param mixed $empty_value Empty value.
  417. * @return mixed The value specified for the option or a default value for the option.
  418. */
  419. public function get_instance_option( $key, $empty_value = null ) {
  420. if ( empty( $this->instance_settings ) ) {
  421. $this->init_instance_settings();
  422. }
  423. // Get option default if unset.
  424. if ( ! isset( $this->instance_settings[ $key ] ) ) {
  425. $form_fields = $this->get_instance_form_fields();
  426. $this->instance_settings[ $key ] = $this->get_field_default( $form_fields[ $key ] );
  427. }
  428. if ( ! is_null( $empty_value ) && '' === $this->instance_settings[ $key ] ) {
  429. $this->instance_settings[ $key ] = $empty_value;
  430. }
  431. $instance_option = apply_filters( 'woocommerce_shipping_' . $this->id . '_instance_option', $this->instance_settings[ $key ], $key, $this );
  432. return $instance_option;
  433. }
  434. /**
  435. * Get settings fields for instances of this shipping method (within zones).
  436. * Should be overridden by shipping methods to add options.
  437. *
  438. * @since 2.6.0
  439. * @return array
  440. */
  441. public function get_instance_form_fields() {
  442. return apply_filters( 'woocommerce_shipping_instance_form_fields_' . $this->id, array_map( array( $this, 'set_defaults' ), $this->instance_form_fields ) );
  443. }
  444. /**
  445. * Return the name of the option in the WP DB.
  446. *
  447. * @since 2.6.0
  448. * @return string
  449. */
  450. public function get_instance_option_key() {
  451. return $this->instance_id ? $this->plugin_id . $this->id . '_' . $this->instance_id . '_settings' : '';
  452. }
  453. /**
  454. * Initialise Settings for instances.
  455. *
  456. * @since 2.6.0
  457. */
  458. public function init_instance_settings() {
  459. $this->instance_settings = get_option( $this->get_instance_option_key(), null );
  460. // If there are no settings defined, use defaults.
  461. if ( ! is_array( $this->instance_settings ) ) {
  462. $form_fields = $this->get_instance_form_fields();
  463. $this->instance_settings = array_merge( array_fill_keys( array_keys( $form_fields ), '' ), wp_list_pluck( $form_fields, 'default' ) );
  464. }
  465. }
  466. /**
  467. * Processes and saves global shipping method options in the admin area.
  468. *
  469. * This method is usually attached to woocommerce_update_options_x hooks.
  470. *
  471. * @since 2.6.0
  472. * @return bool was anything saved?
  473. */
  474. public function process_admin_options() {
  475. if ( ! $this->instance_id ) {
  476. return parent::process_admin_options();
  477. }
  478. // Check we are processing the correct form for this instance.
  479. if ( ! isset( $_REQUEST['instance_id'] ) || absint( $_REQUEST['instance_id'] ) !== $this->instance_id ) { // WPCS: input var ok, CSRF ok.
  480. return false;
  481. }
  482. $this->init_instance_settings();
  483. $post_data = $this->get_post_data();
  484. foreach ( $this->get_instance_form_fields() as $key => $field ) {
  485. if ( 'title' !== $this->get_field_type( $field ) ) {
  486. try {
  487. $this->instance_settings[ $key ] = $this->get_field_value( $key, $field, $post_data );
  488. } catch ( Exception $e ) {
  489. $this->add_error( $e->getMessage() );
  490. }
  491. }
  492. }
  493. return update_option( $this->get_instance_option_key(), apply_filters( 'woocommerce_shipping_' . $this->id . '_instance_settings_values', $this->instance_settings, $this ), 'yes' );
  494. }
  495. }