Keine Beschreibung

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

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