class-wc-shipping-free-shipping.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?php
  2. /**
  3. * Class WC_Shipping_Free_Shipping file.
  4. *
  5. * @package WooCommerce\Shipping
  6. */
  7. if ( ! defined( 'ABSPATH' ) ) {
  8. exit;
  9. }
  10. /**
  11. * Free Shipping Method.
  12. *
  13. * A simple shipping method for free shipping.
  14. *
  15. * @class WC_Shipping_Free_Shipping
  16. * @version 2.6.0
  17. * @package WooCommerce/Classes/Shipping
  18. */
  19. class WC_Shipping_Free_Shipping extends WC_Shipping_Method {
  20. /**
  21. * Min amount to be valid.
  22. *
  23. * @var integer
  24. */
  25. public $min_amount = 0;
  26. /**
  27. * Requires option.
  28. *
  29. * @var string
  30. */
  31. public $requires = '';
  32. /**
  33. * Constructor.
  34. *
  35. * @param int $instance_id Shipping method instance.
  36. */
  37. public function __construct( $instance_id = 0 ) {
  38. $this->id = 'free_shipping';
  39. $this->instance_id = absint( $instance_id );
  40. $this->method_title = __( 'Free shipping', 'woocommerce' );
  41. $this->method_description = __( 'Free shipping is a special method which can be triggered with coupons and minimum spends.', 'woocommerce' );
  42. $this->supports = array(
  43. 'shipping-zones',
  44. 'instance-settings',
  45. 'instance-settings-modal',
  46. );
  47. $this->init();
  48. }
  49. /**
  50. * Initialize free shipping.
  51. */
  52. public function init() {
  53. // Load the settings.
  54. $this->init_form_fields();
  55. $this->init_settings();
  56. // Define user set variables.
  57. $this->title = $this->get_option( 'title' );
  58. $this->min_amount = $this->get_option( 'min_amount', 0 );
  59. $this->requires = $this->get_option( 'requires' );
  60. // Actions.
  61. add_action( 'woocommerce_update_options_shipping_' . $this->id, array( $this, 'process_admin_options' ) );
  62. }
  63. /**
  64. * Init form fields.
  65. */
  66. public function init_form_fields() {
  67. $this->instance_form_fields = array(
  68. 'title' => array(
  69. 'title' => __( 'Title', 'woocommerce' ),
  70. 'type' => 'text',
  71. 'description' => __( 'This controls the title which the user sees during checkout.', 'woocommerce' ),
  72. 'default' => $this->method_title,
  73. 'desc_tip' => true,
  74. ),
  75. 'requires' => array(
  76. 'title' => __( 'Free shipping requires...', 'woocommerce' ),
  77. 'type' => 'select',
  78. 'class' => 'wc-enhanced-select',
  79. 'default' => '',
  80. 'options' => array(
  81. '' => __( 'N/A', 'woocommerce' ),
  82. 'coupon' => __( 'A valid free shipping coupon', 'woocommerce' ),
  83. 'min_amount' => __( 'A minimum order amount', 'woocommerce' ),
  84. 'either' => __( 'A minimum order amount OR a coupon', 'woocommerce' ),
  85. 'both' => __( 'A minimum order amount AND a coupon', 'woocommerce' ),
  86. ),
  87. ),
  88. 'min_amount' => array(
  89. 'title' => __( 'Minimum order amount', 'woocommerce' ),
  90. 'type' => 'price',
  91. 'placeholder' => wc_format_localized_price( 0 ),
  92. 'description' => __( 'Users will need to spend this amount to get free shipping (if enabled above).', 'woocommerce' ),
  93. 'default' => '0',
  94. 'desc_tip' => true,
  95. ),
  96. );
  97. }
  98. /**
  99. * Get setting form fields for instances of this shipping method within zones.
  100. *
  101. * @return array
  102. */
  103. public function get_instance_form_fields() {
  104. if ( is_admin() ) {
  105. wc_enqueue_js(
  106. "jQuery( function( $ ) {
  107. function wcFreeShippingShowHideMinAmountField( el ) {
  108. var form = $( el ).closest( 'form' );
  109. var minAmountField = $( '#woocommerce_free_shipping_min_amount', form ).closest( 'tr' );
  110. if ( 'coupon' === $( el ).val() || '' === $( el ).val() ) {
  111. minAmountField.hide();
  112. } else {
  113. minAmountField.show();
  114. }
  115. }
  116. $( document.body ).on( 'change', '#woocommerce_free_shipping_requires', function() {
  117. wcFreeShippingShowHideMinAmountField( this );
  118. });
  119. // Change while load.
  120. $( '#woocommerce_free_shipping_requires' ).change();
  121. $( document.body ).on( 'wc_backbone_modal_loaded', function( evt, target ) {
  122. if ( 'wc-modal-shipping-method-settings' === target ) {
  123. wcFreeShippingShowHideMinAmountField( $( '#wc-backbone-modal-dialog #woocommerce_free_shipping_requires', evt.currentTarget ) );
  124. }
  125. } );
  126. });"
  127. );
  128. }
  129. return parent::get_instance_form_fields();
  130. }
  131. /**
  132. * See if free shipping is available based on the package and cart.
  133. *
  134. * @param array $package Shipping package.
  135. * @return bool
  136. */
  137. public function is_available( $package ) {
  138. $has_coupon = false;
  139. $has_met_min_amount = false;
  140. if ( in_array( $this->requires, array( 'coupon', 'either', 'both' ), true ) ) {
  141. $coupons = WC()->cart->get_coupons();
  142. if ( $coupons ) {
  143. foreach ( $coupons as $code => $coupon ) {
  144. if ( $coupon->is_valid() && $coupon->get_free_shipping() ) {
  145. $has_coupon = true;
  146. break;
  147. }
  148. }
  149. }
  150. }
  151. if ( in_array( $this->requires, array( 'min_amount', 'either', 'both' ), true ) ) {
  152. $total = WC()->cart->get_displayed_subtotal();
  153. if ( WC()->cart->display_prices_including_tax() ) {
  154. $total = round( $total - ( WC()->cart->get_discount_total() + WC()->cart->get_discount_tax() ), wc_get_price_decimals() );
  155. } else {
  156. $total = round( $total - WC()->cart->get_discount_total(), wc_get_price_decimals() );
  157. }
  158. if ( $total >= $this->min_amount ) {
  159. $has_met_min_amount = true;
  160. }
  161. }
  162. switch ( $this->requires ) {
  163. case 'min_amount':
  164. $is_available = $has_met_min_amount;
  165. break;
  166. case 'coupon':
  167. $is_available = $has_coupon;
  168. break;
  169. case 'both':
  170. $is_available = $has_met_min_amount && $has_coupon;
  171. break;
  172. case 'either':
  173. $is_available = $has_met_min_amount || $has_coupon;
  174. break;
  175. default:
  176. $is_available = true;
  177. break;
  178. }
  179. return apply_filters( 'woocommerce_shipping_' . $this->id . '_is_available', $is_available, $package, $this );
  180. }
  181. /**
  182. * Called to calculate shipping rates for this method. Rates can be added using the add_rate() method.
  183. *
  184. * @uses WC_Shipping_Method::add_rate()
  185. *
  186. * @param array $package Shipping package.
  187. */
  188. public function calculate_shipping( $package = array() ) {
  189. $this->add_rate(
  190. array(
  191. 'label' => $this->title,
  192. 'cost' => 0,
  193. 'taxes' => false,
  194. 'package' => $package,
  195. )
  196. );
  197. }
  198. }