WC_Shipping_Legacy_Flat_Rate

Flat Rate Shipping Method.

Defined (1)

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

/includes/shipping/legacy-flat-rate/class-wc-shipping-legacy-flat-rate.php  
  1. class WC_Shipping_Legacy_Flat_Rate extends WC_Shipping_Method { 
  2.  
  3. /** @var string cost passed to [fee] shortcode */ 
  4. protected $fee_cost = ''; 
  5.  
  6. /** 
  7. * Constructor. 
  8. */ 
  9. public function __construct() { 
  10. $this->id = 'legacy_flat_rate'; 
  11. $this->method_title = __( 'Flat rate (legacy)', 'woocommerce' ); 
  12. $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>'; 
  13. $this->init(); 
  14.  
  15. add_action( 'woocommerce_update_options_shipping_' . $this->id, array( $this, 'process_admin_options' ) ); 
  16. add_action( 'woocommerce_flat_rate_shipping_add_rate', array( $this, 'calculate_extra_shipping' ), 10, 2 ); 
  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 . 'flat_rate' . '_settings'; 
  35.  
  36. /** 
  37. * init function. 
  38. */ 
  39. public function init() { 
  40. // Load the settings. 
  41. $this->init_form_fields(); 
  42. $this->init_settings(); 
  43.  
  44. // Define user set variables 
  45. $this->title = $this->get_option( 'title' ); 
  46. $this->availability = $this->get_option( 'availability' ); 
  47. $this->countries = $this->get_option( 'countries' ); 
  48. $this->tax_status = $this->get_option( 'tax_status' ); 
  49. $this->cost = $this->get_option( 'cost' ); 
  50. $this->type = $this->get_option( 'type', 'class' ); 
  51. $this->options = $this->get_option( 'options', false ); // @deprecated in 2.4.0 
  52.  
  53. /** 
  54. * Initialise Settings Form Fields. 
  55. */ 
  56. public function init_form_fields() { 
  57. $this->form_fields = include( 'includes/settings-flat-rate.php' ); 
  58.  
  59. /** 
  60. * Evaluate a cost from a sum/string. 
  61. * @param string $sum 
  62. * @param array $args 
  63. * @return string 
  64. */ 
  65. protected function evaluate_cost( $sum, $args = array() ) { 
  66. include_once( WC()->plugin_path() . '/includes/libraries/class-wc-eval-math.php' ); 
  67.  
  68. $locale = localeconv(); 
  69. $decimals = array( wc_get_price_decimal_separator(), $locale['decimal_point'], $locale['mon_decimal_point'] ); 
  70.  
  71. $this->fee_cost = $args['cost']; 
  72.  
  73. // Expand shortcodes 
  74. add_shortcode( 'fee', array( $this, 'fee' ) ); 
  75.  
  76. $sum = do_shortcode( str_replace( 
  77. array( 
  78. '[qty]',  
  79. '[cost]',  
  80. ),  
  81. array( 
  82. $args['qty'],  
  83. $args['cost'],  
  84. ),  
  85. $sum 
  86. ) ); 
  87.  
  88. remove_shortcode( 'fee', array( $this, 'fee' ) ); 
  89.  
  90. // Remove whitespace from string 
  91. $sum = preg_replace( '/\s+/', '', $sum ); 
  92.  
  93. // Remove locale from string 
  94. $sum = str_replace( $decimals, '.', $sum ); 
  95.  
  96. // Trim invalid start/end characters 
  97. $sum = rtrim( ltrim( $sum, "\t\n\r\0\x0B+*/" ), "\t\n\r\0\x0B+-*/" ); 
  98.  
  99. // Do the math 
  100. return $sum ? WC_Eval_Math::evaluate( $sum ) : 0; 
  101.  
  102. /** 
  103. * Work out fee (shortcode). 
  104. * @param array $atts 
  105. * @return string 
  106. */ 
  107. public function fee( $atts ) { 
  108. $atts = shortcode_atts( array( 
  109. 'percent' => '',  
  110. 'min_fee' => '',  
  111. ), $atts, 'fee' ); 
  112.  
  113. $calculated_fee = 0; 
  114.  
  115. if ( $atts['percent'] ) { 
  116. $calculated_fee = $this->fee_cost * ( floatval( $atts['percent'] ) / 100 ); 
  117.  
  118. if ( $atts['min_fee'] && $calculated_fee < $atts['min_fee'] ) { 
  119. $calculated_fee = $atts['min_fee']; 
  120.  
  121. return $calculated_fee; 
  122.  
  123. /** 
  124. * calculate_shipping function. 
  125. * @param array $package (default: array()) 
  126. */ 
  127. public function calculate_shipping( $package = array() ) { 
  128. $rate = array( 
  129. 'id' => $this->id,  
  130. 'label' => $this->title,  
  131. 'cost' => 0,  
  132. 'package' => $package,  
  133. ); 
  134.  
  135. // Calculate the costs 
  136. $has_costs = false; // True when a cost is set. False if all costs are blank strings. 
  137. $cost = $this->get_option( 'cost' ); 
  138.  
  139. if ( '' !== $cost ) { 
  140. $has_costs = true; 
  141. $rate['cost'] = $this->evaluate_cost( $cost, array( 
  142. 'qty' => $this->get_package_item_qty( $package ),  
  143. 'cost' => $package['contents_cost'],  
  144. ) ); 
  145.  
  146. // Add shipping class costs 
  147. $found_shipping_classes = $this->find_shipping_classes( $package ); 
  148. $highest_class_cost = 0; 
  149.  
  150. foreach ( $found_shipping_classes as $shipping_class => $products ) { 
  151. // Also handles BW compatibility when slugs were used instead of ids 
  152. $shipping_class_term = get_term_by( 'slug', $shipping_class, 'product_shipping_class' ); 
  153. $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', '' ); 
  154.  
  155. if ( '' === $class_cost_string ) { 
  156. continue; 
  157.  
  158. $has_costs = true; 
  159. $class_cost = $this->evaluate_cost( $class_cost_string, array( 
  160. 'qty' => array_sum( wp_list_pluck( $products, 'quantity' ) ),  
  161. 'cost' => array_sum( wp_list_pluck( $products, 'line_total' ) ),  
  162. ) ); 
  163.  
  164. if ( 'class' === $this->type ) { 
  165. $rate['cost'] += $class_cost; 
  166. } else { 
  167. $highest_class_cost = $class_cost > $highest_class_cost ? $class_cost : $highest_class_cost; 
  168.  
  169. if ( 'order' === $this->type && $highest_class_cost ) { 
  170. $rate['cost'] += $highest_class_cost; 
  171.  
  172. $rate['package'] = $package; 
  173.  
  174. // Add the rate 
  175. if ( $has_costs ) { 
  176. $this->add_rate( $rate ); 
  177.  
  178. /** 
  179. * Developers can add additional flat rates based on this one via this action since @version 2.4. 
  180. * Previously there were (overly complex) options to add additional rates however this was not user. 
  181. * friendly and goes against what Flat Rate Shipping was originally intended for. 
  182. * This example shows how you can add an extra rate based on this flat rate via custom function: 
  183. * add_action( 'woocommerce_flat_rate_shipping_add_rate', 'add_another_custom_flat_rate', 10, 2 ); 
  184. * function add_another_custom_flat_rate( $method, $rate ) { 
  185. * $new_rate = $rate; 
  186. * $new_rate['id'] .= ':' . 'custom_rate_name'; // Append a custom ID. 
  187. * $new_rate['label'] = 'Rushed Shipping'; // Rename to 'Rushed Shipping'. 
  188. * $new_rate['cost'] += 2; // Add $2 to the cost. 
  189. * // Add it to WC. 
  190. * $method->add_rate( $new_rate ); 
  191. * }. 
  192. */ 
  193. do_action( 'woocommerce_flat_rate_shipping_add_rate', $this, $rate ); 
  194.  
  195. /** 
  196. * Get items in package. 
  197. * @param array $package 
  198. * @return int 
  199. */ 
  200. public function get_package_item_qty( $package ) { 
  201. $total_quantity = 0; 
  202. foreach ( $package['contents'] as $item_id => $values ) { 
  203. if ( $values['quantity'] > 0 && $values['data']->needs_shipping() ) { 
  204. $total_quantity += $values['quantity']; 
  205. return $total_quantity; 
  206.  
  207. /** 
  208. * Finds and returns shipping classes and the products with said class. 
  209. * @param mixed $package 
  210. * @return array 
  211. */ 
  212. public function find_shipping_classes( $package ) { 
  213. $found_shipping_classes = array(); 
  214.  
  215. foreach ( $package['contents'] as $item_id => $values ) { 
  216. if ( $values['data']->needs_shipping() ) { 
  217. $found_class = $values['data']->get_shipping_class(); 
  218.  
  219. if ( ! isset( $found_shipping_classes[ $found_class ] ) ) { 
  220. $found_shipping_classes[ $found_class ] = array(); 
  221.  
  222. $found_shipping_classes[ $found_class ][ $item_id ] = $values; 
  223.  
  224. return $found_shipping_classes; 
  225.  
  226. /** 
  227. * Adds extra calculated flat rates. 
  228. * @deprecated 2.4.0 
  229. * Additional rates defined like this: 
  230. * Option Name | Additional Cost [+- Percents%] | Per Cost Type (order, class, or item). 
  231. */ 
  232. public function calculate_extra_shipping( $method, $rate ) { 
  233. if ( $this->options ) { 
  234. $options = array_filter( (array) explode( "\n", $this->options ) ); 
  235.  
  236. foreach ( $options as $option ) { 
  237. $this_option = array_map( 'trim', explode( WC_DELIMITER, $option ) ); 
  238. if ( sizeof( $this_option ) !== 3 ) { 
  239. continue; 
  240. $extra_rate = $rate; 
  241. $extra_rate['id'] = $this->id . ':' . urldecode( sanitize_title( $this_option[0] ) ); 
  242. $extra_rate['label'] = $this_option[0]; 
  243. $extra_cost = $this->get_extra_cost( $this_option[1], $this_option[2], $rate['package'] ); 
  244. if ( is_array( $extra_rate['cost'] ) ) { 
  245. $extra_rate['cost']['order'] = $extra_rate['cost']['order'] + $extra_cost; 
  246. } else { 
  247. $extra_rate['cost'] += $extra_cost; 
  248.  
  249. $this->add_rate( $extra_rate ); 
  250.  
  251. /** 
  252. * Calculate the percentage adjustment for each shipping rate. 
  253. * @deprecated 2.4.0 
  254. * @param float $cost 
  255. * @param float $percent_adjustment 
  256. * @param string $percent_operator 
  257. * @param float $base_price 
  258. * @return float 
  259. */ 
  260. public function calc_percentage_adjustment( $cost, $percent_adjustment, $percent_operator, $base_price ) { 
  261. if ( '+' == $percent_operator ) { 
  262. $cost += $percent_adjustment * $base_price; 
  263. } else { 
  264. $cost -= $percent_adjustment * $base_price; 
  265. return $cost; 
  266.  
  267. /** 
  268. * Get extra cost. 
  269. * @deprecated 2.4.0 
  270. * @param string $cost_string 
  271. * @param string $type 
  272. * @param array $package 
  273. * @return float 
  274. */ 
  275. public function get_extra_cost( $cost_string, $type, $package ) { 
  276. $cost = $cost_string; 
  277. $cost_percent = false; 
  278. // @codingStandardsIgnoreStart 
  279. $pattern = 
  280. '/' . // Start regex. 
  281. '(\d+\.?\d*)' . // Capture digits, optionally capture a `.` and more digits. 
  282. '\s*' . // Match whitespace. 
  283. '(\+|-)' . // Capture the operand. 
  284. '\s*' . // Match whitespace. 
  285. '(\d+\.?\d*)' . // Capture digits, optionally capture a `.` and more digits. 
  286. '\%/'; // Match the percent sign & end regex. 
  287. // @codingStandardsIgnoreEnd 
  288. if ( preg_match( $pattern, $cost_string, $this_cost_matches ) ) { 
  289. $cost_operator = $this_cost_matches[2]; 
  290. $cost_percent = $this_cost_matches[3] / 100; 
  291. $cost = $this_cost_matches[1]; 
  292. switch ( $type ) { 
  293. case 'class' : 
  294. $cost = $cost * sizeof( $this->find_shipping_classes( $package ) ); 
  295. break; 
  296. case 'item' : 
  297. $cost = $cost * $this->get_package_item_qty( $package ); 
  298. break; 
  299. if ( $cost_percent ) { 
  300. switch ( $type ) { 
  301. case 'class' : 
  302. $shipping_classes = $this->find_shipping_classes( $package ); 
  303. foreach ( $shipping_classes as $shipping_class => $items ) { 
  304. foreach ( $items as $item_id => $values ) { 
  305. $cost = $this->calc_percentage_adjustment( $cost, $cost_percent, $cost_operator, $values['line_total'] ); 
  306. break; 
  307. case 'item' : 
  308. foreach ( $package['contents'] as $item_id => $values ) { 
  309. if ( $values['data']->needs_shipping() ) { 
  310. $cost = $this->calc_percentage_adjustment( $cost, $cost_percent, $cost_operator, $values['line_total'] ); 
  311. break; 
  312. case 'order' : 
  313. $cost = $this->calc_percentage_adjustment( $cost, $cost_percent, $cost_operator, $package['contents_cost'] ); 
  314. break; 
  315. return $cost;