class-wc-shipping-legacy-free-shipping.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. <?php
  2. /**
  3. * Class WC_Shipping_Legacy_Free_Shipping file.
  4. *
  5. * @package WooCommerce\Shipping
  6. */
  7. if ( ! defined( 'ABSPATH' ) ) {
  8. exit; // Exit if accessed directly.
  9. }
  10. /**
  11. * Free Shipping Method.
  12. *
  13. * This class is here for backwards compatibility for methods existing before zones existed.
  14. *
  15. * @deprecated 2.6.0
  16. * @version 2.4.0
  17. * @package WooCommerce/Classes/Shipping
  18. */
  19. class WC_Shipping_Legacy_Free_Shipping extends WC_Shipping_Method {
  20. /**
  21. * Min amount to be valid.
  22. *
  23. * @var float
  24. */
  25. public $min_amount;
  26. /**
  27. * Requires option.
  28. *
  29. * @var string
  30. */
  31. public $requires;
  32. /**
  33. * Constructor.
  34. */
  35. public function __construct() {
  36. $this->id = 'legacy_free_shipping';
  37. $this->method_title = __( 'Free shipping (legacy)', 'woocommerce' );
  38. /* translators: %s: Admin shipping settings URL */
  39. $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>';
  40. $this->init();
  41. }
  42. /**
  43. * Process and redirect if disabled.
  44. */
  45. public function process_admin_options() {
  46. parent::process_admin_options();
  47. if ( 'no' === $this->settings['enabled'] ) {
  48. wp_redirect( admin_url( 'admin.php?page=wc-settings&tab=shipping&section=options' ) );
  49. exit;
  50. }
  51. }
  52. /**
  53. * Return the name of the option in the WP DB.
  54. *
  55. * @since 2.6.0
  56. * @return string
  57. */
  58. public function get_option_key() {
  59. return $this->plugin_id . 'free_shipping_settings';
  60. }
  61. /**
  62. * Init function.
  63. */
  64. public function init() {
  65. // Load the settings.
  66. $this->init_form_fields();
  67. $this->init_settings();
  68. // Define user set variables.
  69. $this->enabled = $this->get_option( 'enabled' );
  70. $this->title = $this->get_option( 'title' );
  71. $this->min_amount = $this->get_option( 'min_amount', 0 );
  72. $this->availability = $this->get_option( 'availability' );
  73. $this->countries = $this->get_option( 'countries' );
  74. $this->requires = $this->get_option( 'requires' );
  75. // Actions.
  76. add_action( 'woocommerce_update_options_shipping_' . $this->id, array( $this, 'process_admin_options' ) );
  77. }
  78. /**
  79. * Initialise Gateway Settings Form Fields.
  80. */
  81. public function init_form_fields() {
  82. $this->form_fields = array(
  83. 'enabled' => array(
  84. 'title' => __( 'Enable/Disable', 'woocommerce' ),
  85. 'type' => 'checkbox',
  86. 'label' => __( 'Once disabled, this legacy method will no longer be available.', 'woocommerce' ),
  87. 'default' => 'no',
  88. ),
  89. 'title' => array(
  90. 'title' => __( 'Method title', 'woocommerce' ),
  91. 'type' => 'text',
  92. 'description' => __( 'This controls the title which the user sees during checkout.', 'woocommerce' ),
  93. 'default' => __( 'Free Shipping', 'woocommerce' ),
  94. 'desc_tip' => true,
  95. ),
  96. 'availability' => array(
  97. 'title' => __( 'Method availability', 'woocommerce' ),
  98. 'type' => 'select',
  99. 'default' => 'all',
  100. 'class' => 'availability wc-enhanced-select',
  101. 'options' => array(
  102. 'all' => __( 'All allowed countries', 'woocommerce' ),
  103. 'specific' => __( 'Specific Countries', 'woocommerce' ),
  104. ),
  105. ),
  106. 'countries' => array(
  107. 'title' => __( 'Specific countries', 'woocommerce' ),
  108. 'type' => 'multiselect',
  109. 'class' => 'wc-enhanced-select',
  110. 'css' => 'width: 400px;',
  111. 'default' => '',
  112. 'options' => WC()->countries->get_shipping_countries(),
  113. 'custom_attributes' => array(
  114. 'data-placeholder' => __( 'Select some countries', 'woocommerce' ),
  115. ),
  116. ),
  117. 'requires' => array(
  118. 'title' => __( 'Free shipping requires...', 'woocommerce' ),
  119. 'type' => 'select',
  120. 'class' => 'wc-enhanced-select',
  121. 'default' => '',
  122. 'options' => array(
  123. '' => __( 'N/A', 'woocommerce' ),
  124. 'coupon' => __( 'A valid free shipping coupon', 'woocommerce' ),
  125. 'min_amount' => __( 'A minimum order amount', 'woocommerce' ),
  126. 'either' => __( 'A minimum order amount OR a coupon', 'woocommerce' ),
  127. 'both' => __( 'A minimum order amount AND a coupon', 'woocommerce' ),
  128. ),
  129. ),
  130. 'min_amount' => array(
  131. 'title' => __( 'Minimum order amount', 'woocommerce' ),
  132. 'type' => 'price',
  133. 'placeholder' => wc_format_localized_price( 0 ),
  134. 'description' => __( 'Users will need to spend this amount to get free shipping (if enabled above).', 'woocommerce' ),
  135. 'default' => '0',
  136. 'desc_tip' => true,
  137. ),
  138. );
  139. }
  140. /**
  141. * Check if package is available.
  142. *
  143. * @param array $package Package information.
  144. * @return bool
  145. */
  146. public function is_available( $package ) {
  147. if ( 'no' === $this->enabled ) {
  148. return false;
  149. }
  150. if ( 'specific' === $this->availability ) {
  151. $ship_to_countries = $this->countries;
  152. } else {
  153. $ship_to_countries = array_keys( WC()->countries->get_shipping_countries() );
  154. }
  155. if ( is_array( $ship_to_countries ) && ! in_array( $package['destination']['country'], $ship_to_countries, true ) ) {
  156. return false;
  157. }
  158. // Enabled logic.
  159. $is_available = false;
  160. $has_coupon = false;
  161. $has_met_min_amount = false;
  162. if ( in_array( $this->requires, array( 'coupon', 'either', 'both' ), true ) ) {
  163. $coupons = WC()->cart->get_coupons();
  164. if ( $coupons ) {
  165. foreach ( $coupons as $code => $coupon ) {
  166. if ( $coupon->is_valid() && $coupon->get_free_shipping() ) {
  167. $has_coupon = true;
  168. }
  169. }
  170. }
  171. }
  172. if ( in_array( $this->requires, array( 'min_amount', 'either', 'both' ), true ) ) {
  173. $total = WC()->cart->get_displayed_subtotal();
  174. if ( WC()->cart->display_prices_including_tax() ) {
  175. $total = round( $total - ( WC()->cart->get_discount_total() + WC()->cart->get_discount_tax() ), wc_get_price_decimals() );
  176. } else {
  177. $total = round( $total - WC()->cart->get_discount_total(), wc_get_price_decimals() );
  178. }
  179. if ( $total >= $this->min_amount ) {
  180. $has_met_min_amount = true;
  181. }
  182. }
  183. switch ( $this->requires ) {
  184. case 'min_amount':
  185. if ( $has_met_min_amount ) {
  186. $is_available = true;
  187. }
  188. break;
  189. case 'coupon':
  190. if ( $has_coupon ) {
  191. $is_available = true;
  192. }
  193. break;
  194. case 'both':
  195. if ( $has_met_min_amount && $has_coupon ) {
  196. $is_available = true;
  197. }
  198. break;
  199. case 'either':
  200. if ( $has_met_min_amount || $has_coupon ) {
  201. $is_available = true;
  202. }
  203. break;
  204. default:
  205. $is_available = true;
  206. break;
  207. }
  208. return apply_filters( 'woocommerce_shipping_' . $this->id . '_is_available', $is_available, $package, $this );
  209. }
  210. /**
  211. * Calculate shipping.
  212. *
  213. * @param array $package Package information.
  214. */
  215. public function calculate_shipping( $package = array() ) {
  216. $args = array(
  217. 'id' => $this->id,
  218. 'label' => $this->title,
  219. 'cost' => 0,
  220. 'taxes' => false,
  221. 'package' => $package,
  222. );
  223. $this->add_rate( $args );
  224. }
  225. }