WC_Shipping_Legacy_Free_Shipping

Free Shipping Method.

Defined (1)

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

/includes/shipping/legacy-free-shipping/class-wc-shipping-legacy-free-shipping.php  
  1. class WC_Shipping_Legacy_Free_Shipping extends WC_Shipping_Method { 
  2.  
  3. /** @var float Min amount to be valid */ 
  4. public $min_amount; 
  5.  
  6. /** @var string Requires option */ 
  7. public $requires; 
  8.  
  9. /** 
  10. * Constructor. 
  11. */ 
  12. public function __construct() { 
  13. $this->id = 'legacy_free_shipping'; 
  14. $this->method_title = __( 'Free shipping (legacy)', 'woocommerce' ); 
  15. $this->method_description = '<strong>' . sprintf( __( 'This method is deprecated in 2.6.0 and will be removed in future versions - we recommend disabling it and instead setting up a new rate within your <a href="%s">Shipping zones</a>.', 'woocommerce' ), admin_url( 'admin.php?page=wc-settings&tab=shipping' ) ) . '</strong>'; 
  16. $this->init(); 
  17.  
  18. /** 
  19. * Process and redirect if disabled. 
  20. */ 
  21. public function process_admin_options() { 
  22. parent::process_admin_options(); 
  23.  
  24. if ( 'no' === $this->settings['enabled'] ) { 
  25. wp_redirect( admin_url( 'admin.php?page=wc-settings&tab=shipping§ion=options' ) ); 
  26. exit; 
  27.  
  28. /** 
  29. * Return the name of the option in the WP DB. 
  30. * @since 2.6.0 
  31. * @return string 
  32. */ 
  33. public function get_option_key() { 
  34. return $this->plugin_id . 'free_shipping' . '_settings'; 
  35.  
  36. /** 
  37. * init function. 
  38. */ 
  39. public function init() { 
  40.  
  41. // Load the settings. 
  42. $this->init_form_fields(); 
  43. $this->init_settings(); 
  44.  
  45. // Define user set variables 
  46. $this->enabled = $this->get_option( 'enabled' ); 
  47. $this->title = $this->get_option( 'title' ); 
  48. $this->min_amount = $this->get_option( 'min_amount', 0 ); 
  49. $this->availability = $this->get_option( 'availability' ); 
  50. $this->countries = $this->get_option( 'countries' ); 
  51. $this->requires = $this->get_option( 'requires' ); 
  52.  
  53. // Actions 
  54. add_action( 'woocommerce_update_options_shipping_' . $this->id, array( $this, 'process_admin_options' ) ); 
  55.  
  56. /** 
  57. * Initialise Gateway Settings Form Fields. 
  58. */ 
  59. public function init_form_fields() { 
  60. $this->form_fields = array( 
  61. 'enabled' => array( 
  62. 'title' => __( 'Enable/Disable', 'woocommerce' ),  
  63. 'type' => 'checkbox',  
  64. 'label' => __( 'Once disabled, this legacy method will no longer be available.', 'woocommerce' ),  
  65. 'default' => 'no',  
  66. ),  
  67. 'title' => array( 
  68. 'title' => __( 'Method title', 'woocommerce' ),  
  69. 'type' => 'text',  
  70. 'description' => __( 'This controls the title which the user sees during checkout.', 'woocommerce' ),  
  71. 'default' => __( 'Free Shipping', 'woocommerce' ),  
  72. 'desc_tip' => true,  
  73. ),  
  74. 'availability' => array( 
  75. 'title' => __( 'Method availability', 'woocommerce' ),  
  76. 'type' => 'select',  
  77. 'default' => 'all',  
  78. 'class' => 'availability wc-enhanced-select',  
  79. 'options' => array( 
  80. 'all' => __( 'All allowed countries', 'woocommerce' ),  
  81. 'specific' => __( 'Specific Countries', 'woocommerce' ),  
  82. ),  
  83. ),  
  84. 'countries' => array( 
  85. 'title' => __( 'Specific countries', 'woocommerce' ),  
  86. 'type' => 'multiselect',  
  87. 'class' => 'wc-enhanced-select',  
  88. 'css' => 'width: 400px;',  
  89. 'default' => '',  
  90. 'options' => WC()->countries->get_shipping_countries(),  
  91. 'custom_attributes' => array( 
  92. 'data-placeholder' => __( 'Select some countries', 'woocommerce' ),  
  93. ),  
  94. ),  
  95. 'requires' => array( 
  96. 'title' => __( 'Free shipping requires...', 'woocommerce' ),  
  97. 'type' => 'select',  
  98. 'class' => 'wc-enhanced-select',  
  99. 'default' => '',  
  100. 'options' => array( 
  101. '' => __( 'N/A', 'woocommerce' ),  
  102. 'coupon' => __( 'A valid free shipping coupon', 'woocommerce' ),  
  103. 'min_amount' => __( 'A minimum order amount', 'woocommerce' ),  
  104. 'either' => __( 'A minimum order amount OR a coupon', 'woocommerce' ),  
  105. 'both' => __( 'A minimum order amount AND a coupon', 'woocommerce' ),  
  106. ),  
  107. ),  
  108. 'min_amount' => array( 
  109. 'title' => __( 'Minimum order amount', 'woocommerce' ),  
  110. 'type' => 'price',  
  111. 'placeholder' => wc_format_localized_price( 0 ),  
  112. 'description' => __( 'Users will need to spend this amount to get free shipping (if enabled above).', 'woocommerce' ),  
  113. 'default' => '0',  
  114. 'desc_tip' => true,  
  115. ),  
  116. ); 
  117.  
  118. /** 
  119. * is_available function. 
  120. * @param array $package 
  121. * @return bool 
  122. */ 
  123. public function is_available( $package ) { 
  124. if ( 'no' == $this->enabled ) { 
  125. return false; 
  126.  
  127. if ( 'specific' == $this->availability ) { 
  128. $ship_to_countries = $this->countries; 
  129. } else { 
  130. $ship_to_countries = array_keys( WC()->countries->get_shipping_countries() ); 
  131.  
  132. if ( is_array( $ship_to_countries ) && ! in_array( $package['destination']['country'], $ship_to_countries ) ) { 
  133. return false; 
  134.  
  135. // Enabled logic 
  136. $is_available = false; 
  137. $has_coupon = false; 
  138. $has_met_min_amount = false; 
  139.  
  140. if ( in_array( $this->requires, array( 'coupon', 'either', 'both' ) ) ) { 
  141.  
  142. if ( $coupons = WC()->cart->get_coupons() ) { 
  143. foreach ( $coupons as $code => $coupon ) { 
  144. if ( $coupon->is_valid() && $coupon->get_free_shipping() ) { 
  145. $has_coupon = true; 
  146.  
  147. if ( in_array( $this->requires, array( 'min_amount', 'either', 'both' ) ) && isset( WC()->cart->cart_contents_total ) ) { 
  148. if ( WC()->cart->prices_include_tax ) { 
  149. $total = WC()->cart->cart_contents_total + array_sum( WC()->cart->taxes ); 
  150. } else { 
  151. $total = WC()->cart->cart_contents_total; 
  152.  
  153. if ( $total >= $this->min_amount ) { 
  154. $has_met_min_amount = true; 
  155.  
  156. switch ( $this->requires ) { 
  157. case 'min_amount' : 
  158. if ( $has_met_min_amount ) { 
  159. $is_available = true; 
  160. break; 
  161. case 'coupon' : 
  162. if ( $has_coupon ) { 
  163. $is_available = true; 
  164. break; 
  165. case 'both' : 
  166. if ( $has_met_min_amount && $has_coupon ) { 
  167. $is_available = true; 
  168. break; 
  169. case 'either' : 
  170. if ( $has_met_min_amount || $has_coupon ) { 
  171. $is_available = true; 
  172. break; 
  173. default : 
  174. $is_available = true; 
  175. break; 
  176.  
  177. return apply_filters( 'woocommerce_shipping_' . $this->id . '_is_available', $is_available, $package ); 
  178.  
  179. /** 
  180. * calculate_shipping function. 
  181. * @return array 
  182. */ 
  183. public function calculate_shipping( $package = array() ) { 
  184. $args = array( 
  185. 'id' => $this->id,  
  186. 'label' => $this->title,  
  187. 'cost' => 0,  
  188. 'taxes' => false,  
  189. 'package' => $package,  
  190. ); 
  191. $this->add_rate( $args );