WC_Shipping_Free_Shipping

Free Shipping Method.

Defined (1)

The class is defined in the following location(s).

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