Няма описание

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

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