class-wc-shipping-flat-rate.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. <?php
  2. /**
  3. * Flat Rate Shipping Method.
  4. *
  5. * @version 2.6.0
  6. * @package WooCommerce/Classes/Shipping
  7. */
  8. defined( 'ABSPATH' ) || exit;
  9. /**
  10. * WC_Shipping_Flat_Rate class.
  11. */
  12. class WC_Shipping_Flat_Rate extends WC_Shipping_Method {
  13. /**
  14. * Cost passed to [fee] shortcode.
  15. *
  16. * @var string Cost.
  17. */
  18. protected $fee_cost = '';
  19. /**
  20. * Constructor.
  21. *
  22. * @param int $instance_id Shipping method instance ID.
  23. */
  24. public function __construct( $instance_id = 0 ) {
  25. $this->id = 'flat_rate';
  26. $this->instance_id = absint( $instance_id );
  27. $this->method_title = __( 'Flat rate', 'woocommerce' );
  28. $this->method_description = __( 'Lets you charge a fixed rate for shipping.', 'woocommerce' );
  29. $this->supports = array(
  30. 'shipping-zones',
  31. 'instance-settings',
  32. 'instance-settings-modal',
  33. );
  34. $this->init();
  35. add_action( 'woocommerce_update_options_shipping_' . $this->id, array( $this, 'process_admin_options' ) );
  36. }
  37. /**
  38. * Init user set variables.
  39. */
  40. public function init() {
  41. $this->instance_form_fields = include 'includes/settings-flat-rate.php';
  42. $this->title = $this->get_option( 'title' );
  43. $this->tax_status = $this->get_option( 'tax_status' );
  44. $this->cost = $this->get_option( 'cost' );
  45. $this->type = $this->get_option( 'type', 'class' );
  46. }
  47. /**
  48. * Evaluate a cost from a sum/string.
  49. *
  50. * @param string $sum Sum of shipping.
  51. * @param array $args Args.
  52. * @return string
  53. */
  54. protected function evaluate_cost( $sum, $args = array() ) {
  55. include_once WC()->plugin_path() . '/includes/libraries/class-wc-eval-math.php';
  56. // Allow 3rd parties to process shipping cost arguments.
  57. $args = apply_filters( 'woocommerce_evaluate_shipping_cost_args', $args, $sum, $this );
  58. $locale = localeconv();
  59. $decimals = array( wc_get_price_decimal_separator(), $locale['decimal_point'], $locale['mon_decimal_point'], ',' );
  60. $this->fee_cost = $args['cost'];
  61. // Expand shortcodes.
  62. add_shortcode( 'fee', array( $this, 'fee' ) );
  63. $sum = do_shortcode(
  64. str_replace(
  65. array(
  66. '[qty]',
  67. '[cost]',
  68. ),
  69. array(
  70. $args['qty'],
  71. $args['cost'],
  72. ),
  73. $sum
  74. )
  75. );
  76. remove_shortcode( 'fee', array( $this, 'fee' ) );
  77. // Remove whitespace from string.
  78. $sum = preg_replace( '/\s+/', '', $sum );
  79. // Remove locale from string.
  80. $sum = str_replace( $decimals, '.', $sum );
  81. // Trim invalid start/end characters.
  82. $sum = rtrim( ltrim( $sum, "\t\n\r\0\x0B+*/" ), "\t\n\r\0\x0B+-*/" );
  83. // Do the math.
  84. return $sum ? WC_Eval_Math::evaluate( $sum ) : 0;
  85. }
  86. /**
  87. * Work out fee (shortcode).
  88. *
  89. * @param array $atts Attributes.
  90. * @return string
  91. */
  92. public function fee( $atts ) {
  93. $atts = shortcode_atts(
  94. array(
  95. 'percent' => '',
  96. 'min_fee' => '',
  97. 'max_fee' => '',
  98. ), $atts, 'fee'
  99. );
  100. $calculated_fee = 0;
  101. if ( $atts['percent'] ) {
  102. $calculated_fee = $this->fee_cost * ( floatval( $atts['percent'] ) / 100 );
  103. }
  104. if ( $atts['min_fee'] && $calculated_fee < $atts['min_fee'] ) {
  105. $calculated_fee = $atts['min_fee'];
  106. }
  107. if ( $atts['max_fee'] && $calculated_fee > $atts['max_fee'] ) {
  108. $calculated_fee = $atts['max_fee'];
  109. }
  110. return $calculated_fee;
  111. }
  112. /**
  113. * Calculate the shipping costs.
  114. *
  115. * @param array $package Package of items from cart.
  116. */
  117. public function calculate_shipping( $package = array() ) {
  118. $rate = array(
  119. 'id' => $this->get_rate_id(),
  120. 'label' => $this->title,
  121. 'cost' => 0,
  122. 'package' => $package,
  123. );
  124. // Calculate the costs.
  125. $has_costs = false; // True when a cost is set. False if all costs are blank strings.
  126. $cost = $this->get_option( 'cost' );
  127. if ( '' !== $cost ) {
  128. $has_costs = true;
  129. $rate['cost'] = $this->evaluate_cost(
  130. $cost, array(
  131. 'qty' => $this->get_package_item_qty( $package ),
  132. 'cost' => $package['contents_cost'],
  133. )
  134. );
  135. }
  136. // Add shipping class costs.
  137. $shipping_classes = WC()->shipping->get_shipping_classes();
  138. if ( ! empty( $shipping_classes ) ) {
  139. $found_shipping_classes = $this->find_shipping_classes( $package );
  140. $highest_class_cost = 0;
  141. foreach ( $found_shipping_classes as $shipping_class => $products ) {
  142. // Also handles BW compatibility when slugs were used instead of ids.
  143. $shipping_class_term = get_term_by( 'slug', $shipping_class, 'product_shipping_class' );
  144. $class_cost_string = $shipping_class_term && $shipping_class_term->term_id ? $this->get_option( 'class_cost_' . $shipping_class_term->term_id, $this->get_option( 'class_cost_' . $shipping_class, '' ) ) : $this->get_option( 'no_class_cost', '' );
  145. if ( '' === $class_cost_string ) {
  146. continue;
  147. }
  148. $has_costs = true;
  149. $class_cost = $this->evaluate_cost(
  150. $class_cost_string, array(
  151. 'qty' => array_sum( wp_list_pluck( $products, 'quantity' ) ),
  152. 'cost' => array_sum( wp_list_pluck( $products, 'line_total' ) ),
  153. )
  154. );
  155. if ( 'class' === $this->type ) {
  156. $rate['cost'] += $class_cost;
  157. } else {
  158. $highest_class_cost = $class_cost > $highest_class_cost ? $class_cost : $highest_class_cost;
  159. }
  160. }
  161. if ( 'order' === $this->type && $highest_class_cost ) {
  162. $rate['cost'] += $highest_class_cost;
  163. }
  164. }
  165. if ( $has_costs ) {
  166. $this->add_rate( $rate );
  167. }
  168. /**
  169. * Developers can add additional flat rates based on this one via this action since @version 2.4.
  170. *
  171. * Previously there were (overly complex) options to add additional rates however this was not user.
  172. * friendly and goes against what Flat Rate Shipping was originally intended for.
  173. */
  174. do_action( 'woocommerce_' . $this->id . '_shipping_add_rate', $this, $rate );
  175. }
  176. /**
  177. * Get items in package.
  178. *
  179. * @param array $package Package of items from cart.
  180. * @return int
  181. */
  182. public function get_package_item_qty( $package ) {
  183. $total_quantity = 0;
  184. foreach ( $package['contents'] as $item_id => $values ) {
  185. if ( $values['quantity'] > 0 && $values['data']->needs_shipping() ) {
  186. $total_quantity += $values['quantity'];
  187. }
  188. }
  189. return $total_quantity;
  190. }
  191. /**
  192. * Finds and returns shipping classes and the products with said class.
  193. *
  194. * @param mixed $package Package of items from cart.
  195. * @return array
  196. */
  197. public function find_shipping_classes( $package ) {
  198. $found_shipping_classes = array();
  199. foreach ( $package['contents'] as $item_id => $values ) {
  200. if ( $values['data']->needs_shipping() ) {
  201. $found_class = $values['data']->get_shipping_class();
  202. if ( ! isset( $found_shipping_classes[ $found_class ] ) ) {
  203. $found_shipping_classes[ $found_class ] = array();
  204. }
  205. $found_shipping_classes[ $found_class ][ $item_id ] = $values;
  206. }
  207. }
  208. return $found_shipping_classes;
  209. }
  210. /**
  211. * Sanitize the cost field.
  212. *
  213. * @since 3.4.0
  214. * @param string $value Unsanitized value.
  215. * @return string
  216. */
  217. public function sanitize_cost( $value ) {
  218. $value = is_null( $value ) ? '' : $value;
  219. $value = wp_kses_post( trim( wp_unslash( $value ) ) );
  220. $value = str_replace( array( get_woocommerce_currency_symbol(), html_entity_decode( get_woocommerce_currency_symbol() ) ), '', $value );
  221. return $value;
  222. }
  223. }