PageRenderTime 49ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/woocommerce/includes/shipping/flat-rate/class-wc-shipping-flat-rate.php

https://bitbucket.org/theshipswakecreative/psw
PHP | 723 lines | 482 code | 107 blank | 134 comment | 70 complexity | 4579e25f4879d6347fce8331ce4eb511 MD5 | raw file
Possible License(s): LGPL-3.0, Apache-2.0
  1. <?php
  2. if ( ! defined( 'ABSPATH' ) ) {
  3. exit; // Exit if accessed directly
  4. }
  5. /**
  6. * Flat Rate Shipping Method
  7. *
  8. * A simple shipping method for a flat fee per item or per order
  9. *
  10. * @class WC_Shipping_Flat_Rate
  11. * @version 2.0.0
  12. * @package WooCommerce/Classes/Shipping
  13. * @author WooThemes
  14. */
  15. class WC_Shipping_Flat_Rate extends WC_Shipping_Method {
  16. /**
  17. * __construct function.
  18. *
  19. * @access public
  20. * @return void
  21. */
  22. function __construct() {
  23. $this->id = 'flat_rate';
  24. $this->method_title = __( 'Flat Rate', 'woocommerce' );
  25. $this->flat_rate_option = 'woocommerce_flat_rates';
  26. $this->method_description = __( 'Flat rates let you define a standard rate per item, or per order.', 'woocommerce' );
  27. add_action( 'woocommerce_update_options_shipping_' . $this->id, array( $this, 'process_admin_options' ) );
  28. add_action( 'woocommerce_update_options_shipping_' . $this->id, array( $this, 'process_flat_rates' ) );
  29. add_filter( 'woocommerce_settings_api_sanitized_fields_' . $this->id, array( $this, 'save_default_costs' ) );
  30. $this->init();
  31. }
  32. /**
  33. * init function.
  34. *
  35. * @access public
  36. * @return void
  37. */
  38. function init() {
  39. // Load the settings.
  40. $this->init_form_fields();
  41. $this->init_settings();
  42. // Define user set variables
  43. $this->title = $this->get_option( 'title' );
  44. $this->availability = $this->get_option( 'availability' );
  45. $this->countries = $this->get_option( 'countries' );
  46. $this->type = $this->get_option( 'type' );
  47. $this->tax_status = $this->get_option( 'tax_status' );
  48. $this->cost = $this->get_option( 'cost' );
  49. $this->cost_per_order = $this->get_option( 'cost_per_order' );
  50. $this->fee = $this->get_option( 'fee' );
  51. $this->minimum_fee = $this->get_option( 'minimum_fee' );
  52. $this->options = (array) explode( "\n", $this->get_option( 'options' ) );
  53. // Load Flat rates
  54. $this->get_flat_rates();
  55. }
  56. /**
  57. * Initialise Gateway Settings Form Fields
  58. *
  59. * @access public
  60. * @return void
  61. */
  62. function init_form_fields() {
  63. $this->form_fields = array(
  64. 'enabled' => array(
  65. 'title' => __( 'Enable/Disable', 'woocommerce' ),
  66. 'type' => 'checkbox',
  67. 'label' => __( 'Enable this shipping method', 'woocommerce' ),
  68. 'default' => 'no',
  69. ),
  70. 'title' => array(
  71. 'title' => __( 'Method Title', 'woocommerce' ),
  72. 'type' => 'text',
  73. 'description' => __( 'This controls the title which the user sees during checkout.', 'woocommerce' ),
  74. 'default' => __( 'Flat Rate', 'woocommerce' ),
  75. 'desc_tip' => true
  76. ),
  77. 'availability' => array(
  78. 'title' => __( 'Availability', 'woocommerce' ),
  79. 'type' => 'select',
  80. 'default' => 'all',
  81. 'class' => 'availability',
  82. 'options' => array(
  83. 'all' => __( 'All allowed countries', 'woocommerce' ),
  84. 'specific' => __( 'Specific Countries', 'woocommerce' ),
  85. ),
  86. ),
  87. 'countries' => array(
  88. 'title' => __( 'Specific Countries', 'woocommerce' ),
  89. 'type' => 'multiselect',
  90. 'class' => 'chosen_select',
  91. 'css' => 'width: 450px;',
  92. 'default' => '',
  93. 'options' => WC()->countries->get_shipping_countries(),
  94. 'custom_attributes' => array(
  95. 'data-placeholder' => __( 'Select some countries', 'woocommerce' )
  96. )
  97. ),
  98. 'tax_status' => array(
  99. 'title' => __( 'Tax Status', 'woocommerce' ),
  100. 'type' => 'select',
  101. 'default' => 'taxable',
  102. 'options' => array(
  103. 'taxable' => __( 'Taxable', 'woocommerce' ),
  104. 'none' => _x( 'None', 'Tax status', 'woocommerce' )
  105. ),
  106. ),
  107. 'cost_per_order' => array(
  108. 'title' => __( 'Cost per order', 'woocommerce' ),
  109. 'type' => 'price',
  110. 'placeholder' => wc_format_localized_price( 0 ),
  111. 'description' => __( 'Enter a cost (excluding tax) per order, e.g. 5.00. Default is 0.', 'woocommerce' ),
  112. 'default' => '',
  113. 'desc_tip' => true
  114. ),
  115. 'options' => array(
  116. 'title' => __( 'Additional Rates', 'woocommerce' ),
  117. 'type' => 'textarea',
  118. 'description' => __( 'Optional extra shipping options with additional costs (one per line): Option Name | Additional Cost [+- Percents] | Per Cost Type (order, class, or item) Example: <code>Priority Mail | 6.95 [+ 0.2%] | order</code>.', 'woocommerce' ),
  119. 'default' => '',
  120. 'desc_tip' => true,
  121. 'placeholder' => __( 'Option Name | Additional Cost [+- Percents%] | Per Cost Type (order, class, or item)', 'woocommerce' )
  122. ),
  123. 'additional_costs' => array(
  124. 'title' => __( 'Additional Costs', 'woocommerce' ),
  125. 'type' => 'title',
  126. 'description' => __( 'Additional costs can be added below - these will all be added to the per-order cost above.', 'woocommerce' )
  127. ),
  128. 'type' => array(
  129. 'title' => __( 'Costs Added...', 'woocommerce' ),
  130. 'type' => 'select',
  131. 'default' => 'order',
  132. 'options' => array(
  133. 'order' => __( 'Per Order - charge shipping for the entire order as a whole', 'woocommerce' ),
  134. 'item' => __( 'Per Item - charge shipping for each item individually', 'woocommerce' ),
  135. 'class' => __( 'Per Class - charge shipping for each shipping class in an order', 'woocommerce' ),
  136. ),
  137. ),
  138. 'additional_costs_table' => array(
  139. 'type' => 'additional_costs_table'
  140. ),
  141. 'minimum_fee' => array(
  142. 'title' => __( 'Minimum Handling Fee', 'woocommerce' ),
  143. 'type' => 'price',
  144. 'placeholder' => wc_format_localized_price( 0 ),
  145. 'description' => __( 'Enter a minimum fee amount. Fee\'s less than this will be increased. Leave blank to disable.', 'woocommerce' ),
  146. 'default' => '',
  147. 'desc_tip' => true
  148. ),
  149. );
  150. }
  151. /**
  152. * calculate_shipping function.
  153. *
  154. * @access public
  155. * @param array $package (default: array())
  156. * @return void
  157. */
  158. function calculate_shipping( $package = array() ) {
  159. $this->rates = array();
  160. $cost_per_order = ( isset( $this->cost_per_order ) && ! empty( $this->cost_per_order ) ) ? $this->cost_per_order : 0;
  161. if ( $this->type == 'order' ) {
  162. $shipping_total = $this->order_shipping( $package );
  163. if ( ! is_null( $shipping_total ) || $cost_per_order > 0 ) {
  164. $rate = array(
  165. 'id' => $this->id,
  166. 'label' => $this->title,
  167. 'cost' => $shipping_total + $cost_per_order,
  168. );
  169. }
  170. } elseif ( $this->type == 'class' ) {
  171. $shipping_total = $this->class_shipping( $package );
  172. if ( ! is_null( $shipping_total ) || $cost_per_order > 0 ) {
  173. $rate = array(
  174. 'id' => $this->id,
  175. 'label' => $this->title,
  176. 'cost' => $shipping_total + $cost_per_order,
  177. );
  178. }
  179. } elseif ( $this->type == 'item' ) {
  180. $costs = $this->item_shipping( $package );
  181. if ( ! is_null( $costs ) || $cost_per_order > 0 ) {
  182. if ( ! is_array( $costs ) ) {
  183. $costs = array();
  184. }
  185. $costs['order'] = $cost_per_order;
  186. $rate = array(
  187. 'id' => $this->id,
  188. 'label' => $this->title,
  189. 'cost' => $costs,
  190. 'calc_tax' => 'per_item',
  191. );
  192. }
  193. }
  194. if ( isset( $rate ) ) {
  195. $this->add_rate( $rate );
  196. }
  197. // Add any extra rates
  198. if ( sizeof( $this->options ) > 0) {
  199. if ( ! isset( $rate ) ) {
  200. $rate = array(
  201. 'id' => $this->id,
  202. 'label' => $this->title,
  203. 'cost' => 0,
  204. );
  205. }
  206. // Get item qty
  207. $total_quantity = 0;
  208. foreach ( $package['contents'] as $item_id => $values ) {
  209. if ( $values['quantity'] > 0 && $values['data']->needs_shipping() ) {
  210. $total_quantity += $values['quantity'];
  211. }
  212. }
  213. // Loop options
  214. foreach ( $this->options as $option ) {
  215. $this_option = array_map( 'trim', explode( WC_DELIMITER, $option ) );
  216. if ( sizeof( $this_option ) !== 3 ) continue;
  217. $extra_rate = $rate;
  218. $extra_rate['id'] = $this->id . ':' . urldecode( sanitize_title( $this_option[0] ) );
  219. $extra_rate['label'] = $this_option[0];
  220. $this_cost = $this_option[1];
  221. $this_cost_percents = '';
  222. $pattern =
  223. '/' . // start regex
  224. '(\d+\.?\d*)' . // capture digits, optionally capture a `.` and more digits
  225. '\s*' . // match whitespace
  226. '(\+|-)' . // capture the operand
  227. '\s*'. // match whitespace
  228. '(\d+\.?\d*)'. // capture digits, optionally capture a `.` and more digits
  229. '\%/'; // match the percent sign & end regex
  230. if ( preg_match( $pattern, $this_cost, $this_cost_matches ) ) {
  231. $this_cost_mathop = $this_cost_matches[2];
  232. $this_cost_percents = $this_cost_matches[3] / 100;
  233. $this_cost = $this_cost_matches[1];
  234. unset( $this_cost_matches );
  235. }
  236. // Backwards compat with yes and no
  237. if ( $this_option[2] == 'yes' ) {
  238. $this_type = 'order';
  239. } elseif ( $this_option[2] == 'no' ) {
  240. $this_type = $this->type;
  241. } else {
  242. $this_type = $this_option[2];
  243. }
  244. switch ( $this_type ) {
  245. case 'class' :
  246. $this_cost = $this_cost * sizeof( $this->find_shipping_classes( $package ) );
  247. // Factor $this_cost by the percentage if provided.
  248. if ( $this_cost_percents ) {
  249. foreach ( $this->find_shipping_classes( $package ) as $shipping_class => $items ){
  250. foreach ( $items as $item_id => $values ) {
  251. $this_cost = $this->calc_percentage_adjustment( $this_cost, $this_cost_percents, $this_cost_mathop, $values['line_total'] );
  252. }
  253. }
  254. }
  255. break;
  256. case 'item' :
  257. $this_cost = $this_cost * $total_quantity;
  258. // Factor $this_cost by the percentage if provided.
  259. if ( $this_cost_percents ) {
  260. foreach ( $package['contents'] as $item_id => $values ) {
  261. $this_cost = $this->calc_percentage_adjustment( $this_cost, $this_cost_percents, $this_cost_mathop, $values['line_total'] );
  262. }
  263. }
  264. break;
  265. case 'order' :
  266. // Factor $this_cost by the percentage if provided.
  267. if ( $this_cost_percents ) {
  268. $this_cost = $this->calc_percentage_adjustment( $this_cost, $this_cost_percents, $this_cost_mathop, $package['contents_cost'] );
  269. }
  270. break;
  271. }
  272. // Per item rates
  273. if ( is_array( $extra_rate['cost'] ) ) $extra_rate['cost']['order'] = $extra_rate['cost']['order'] + $this_cost;
  274. // Per order or class rates
  275. else $extra_rate['cost'] = $extra_rate['cost'] + $this_cost;
  276. $this->add_rate( $extra_rate );
  277. }
  278. }
  279. }
  280. /**
  281. * Calculate the percentage adjustment for each shipping rate.
  282. *
  283. * @access public
  284. * @param float $cost
  285. * @param float $percent_adjustment
  286. * @param string $percent_operator
  287. * @param float $base_price
  288. * @return float
  289. */
  290. function calc_percentage_adjustment( $cost, $percent_adjustment, $percent_operator, $base_price ) {
  291. if ( '+' == $percent_operator ) {
  292. $cost += $percent_adjustment * $base_price;
  293. } else {
  294. $cost -= $percent_adjustment * $base_price;
  295. }
  296. return $cost;
  297. }
  298. /**
  299. * order_shipping function.
  300. *
  301. * @access public
  302. * @param array $package
  303. * @return float
  304. */
  305. function order_shipping( $package ) {
  306. $cost = null;
  307. $fee = null;
  308. if ( sizeof( $this->flat_rates ) > 0 ) {
  309. $found_shipping_classes = $this->find_shipping_classes( $package );
  310. // Find most expensive class (if found)
  311. foreach ( $found_shipping_classes as $shipping_class => $products ) {
  312. if ( isset( $this->flat_rates[ $shipping_class ] ) ) {
  313. if ( $this->flat_rates[ $shipping_class ]['cost'] > $cost ) {
  314. $cost = $this->flat_rates[ $shipping_class ]['cost'];
  315. $fee = $this->flat_rates[ $shipping_class ]['fee'];
  316. }
  317. } else {
  318. // No matching classes so use defaults
  319. if ( ! empty( $this->cost ) && $this->cost > $cost ) {
  320. $cost = $this->cost;
  321. $fee = $this->fee;
  322. }
  323. }
  324. }
  325. }
  326. // Default rates if set
  327. if ( is_null( $cost ) && $this->cost !== '' ) {
  328. $cost = $this->cost;
  329. $fee = $this->fee;
  330. } elseif ( is_null( $cost ) ) {
  331. // Set rates to 0 if nothing is set by the user
  332. $cost = 0;
  333. $fee = 0;
  334. }
  335. // Shipping for whole order
  336. return $cost + $this->get_fee( $fee, $package['contents_cost'] );
  337. }
  338. /**
  339. * class_shipping function.
  340. *
  341. * @access public
  342. * @param array $package
  343. * @return float
  344. */
  345. function class_shipping( $package ) {
  346. $cost = null;
  347. $fee = null;
  348. $matched = false;
  349. if ( sizeof( $this->flat_rates ) > 0 || $this->cost !== '' ) {
  350. // Find shipping classes for products in the cart.
  351. $found_shipping_classes = $this->find_shipping_classes( $package );
  352. // Store prices too, so we can calc a fee for the class.
  353. $found_shipping_classes_values = array();
  354. foreach ( $found_shipping_classes as $shipping_class => $products ) {
  355. if ( ! isset( $found_shipping_classes_values[ $shipping_class ] ) ) {
  356. $found_shipping_classes_values[ $shipping_class ] = 0;
  357. }
  358. foreach ( $products as $product ) {
  359. $found_shipping_classes_values[ $shipping_class ] += $product['data']->get_price() * $product['quantity'];
  360. }
  361. }
  362. // For each found class, add up the costs and fees
  363. foreach ( $found_shipping_classes_values as $shipping_class => $class_price ) {
  364. if ( isset( $this->flat_rates[ $shipping_class ] ) ) {
  365. $cost += $this->flat_rates[ $shipping_class ]['cost'];
  366. $fee += $this->get_fee( $this->flat_rates[ $shipping_class ]['fee'], $class_price );
  367. $matched = true;
  368. } elseif ( $this->cost !== '' ) {
  369. // Class not set so we use default rate if its set
  370. $cost += $this->cost;
  371. $fee += $this->get_fee( $this->fee, $class_price );
  372. $matched = true;
  373. }
  374. }
  375. }
  376. // Total
  377. if ( $matched ) {
  378. return $cost + $fee;
  379. } else {
  380. return null;
  381. }
  382. }
  383. /**
  384. * item_shipping function.
  385. *
  386. * @access public
  387. * @param array $package
  388. * @return array
  389. */
  390. function item_shipping( $package ) {
  391. // Per item shipping so we pass an array of costs (per item) instead of a single value
  392. $costs = array();
  393. $matched = false;
  394. // Shipping per item
  395. foreach ( $package['contents'] as $item_id => $values ) {
  396. $_product = $values['data'];
  397. if ( $values['quantity'] > 0 && $_product->needs_shipping() ) {
  398. $shipping_class = $_product->get_shipping_class();
  399. $fee = $cost = 0;
  400. if ( isset( $this->flat_rates[ $shipping_class ] ) ) {
  401. $cost = $this->flat_rates[ $shipping_class ]['cost'];
  402. $fee = $this->get_fee( $this->flat_rates[ $shipping_class ]['fee'], $_product->get_price() );
  403. $matched = true;
  404. } elseif ( $this->cost !== '' ) {
  405. $cost = $this->cost;
  406. $fee = $this->get_fee( $this->fee, $_product->get_price() );
  407. $matched = true;
  408. }
  409. $costs[ $item_id ] = ( ( $cost + $fee ) * $values['quantity'] );
  410. }
  411. }
  412. if ( $matched ) {
  413. return $costs;
  414. } else {
  415. return null;
  416. }
  417. }
  418. /**
  419. * Finds and returns shipping classes and the products with said class.
  420. *
  421. * @access public
  422. * @param mixed $package
  423. * @return array
  424. */
  425. public function find_shipping_classes( $package ) {
  426. $found_shipping_classes = array();
  427. // Find shipping classes for products in the cart
  428. if ( sizeof( $package['contents'] ) > 0 ) {
  429. foreach ( $package['contents'] as $item_id => $values ) {
  430. if ( $values['data']->needs_shipping() ) {
  431. $found_class = $values['data']->get_shipping_class();
  432. if ( ! isset( $found_shipping_classes[ $found_class ] ) ) {
  433. $found_shipping_classes[ $found_class ] = array();
  434. }
  435. $found_shipping_classes[ $found_class ][ $item_id ] = $values;
  436. }
  437. }
  438. }
  439. return $found_shipping_classes;
  440. }
  441. /**
  442. * validate_additional_costs_field function.
  443. *
  444. * @access public
  445. * @param mixed $key
  446. * @return bool
  447. */
  448. function validate_additional_costs_table_field( $key ) {
  449. return false;
  450. }
  451. /**
  452. * generate_additional_costs_html function.
  453. *
  454. * @access public
  455. * @return string
  456. */
  457. function generate_additional_costs_table_html() {
  458. ob_start();
  459. ?>
  460. <tr valign="top">
  461. <th scope="row" class="titledesc"><?php _e( 'Costs', 'woocommerce' ); ?>:</th>
  462. <td class="forminp" id="<?php echo $this->id; ?>_flat_rates">
  463. <table class="shippingrows widefat" cellspacing="0">
  464. <thead>
  465. <tr>
  466. <th class="check-column"><input type="checkbox"></th>
  467. <th class="shipping_class"><?php _e( 'Shipping Class', 'woocommerce' ); ?></th>
  468. <th><?php _e( 'Cost', 'woocommerce' ); ?> <a class="tips" data-tip="<?php _e( 'Cost, excluding tax.', 'woocommerce' ); ?>">[?]</a></th>
  469. <th><?php _e( 'Handling Fee', 'woocommerce' ); ?> <a class="tips" data-tip="<?php _e( 'Fee excluding tax. Enter an amount, e.g. 2.50, or a percentage, e.g. 5%.', 'woocommerce' ); ?>">[?]</a></th>
  470. </tr>
  471. </thead>
  472. <tbody class="flat_rates">
  473. <tr>
  474. <td></td>
  475. <td class="flat_rate_class"><?php _e( 'Any class', 'woocommerce' ); ?></td>
  476. <td><input type="text" value="<?php echo esc_attr( wc_format_localized_price( $this->cost ) ); ?>" name="default_cost" placeholder="<?php _e( 'N/A', 'woocommerce' ); ?>" size="4" class="wc_input_price" /></td>
  477. <td><input type="text" value="<?php echo esc_attr( wc_format_localized_price( $this->fee ) ); ?>" name="default_fee" placeholder="<?php _e( 'N/A', 'woocommerce' ); ?>" size="4" class="wc_input_price" /></td>
  478. </tr>
  479. <?php
  480. $i = -1;
  481. if ( $this->flat_rates ) {
  482. foreach ( $this->flat_rates as $class => $rate ) {
  483. $i++;
  484. echo '<tr class="flat_rate">
  485. <th class="check-column"><input type="checkbox" name="select" /></th>
  486. <td class="flat_rate_class">
  487. <select name="' . esc_attr( $this->id . '_class[' . $i . ']' ) . '" class="select">';
  488. if ( WC()->shipping->get_shipping_classes() ) {
  489. foreach ( WC()->shipping->get_shipping_classes() as $shipping_class ) {
  490. echo '<option value="' . esc_attr( $shipping_class->slug ) . '" '.selected($shipping_class->slug, $class, false).'>'.$shipping_class->name.'</option>';
  491. }
  492. } else {
  493. echo '<option value="">'.__( 'Select a class&hellip;', 'woocommerce' ).'</option>';
  494. }
  495. echo '</select>
  496. </td>
  497. <td><input type="text" value="' . esc_attr( $rate['cost'] ) . '" name="' . esc_attr( $this->id .'_cost[' . $i . ']' ) . '" placeholder="' . wc_format_localized_price( 0 ) . '" size="4" class="wc_input_price" /></td>
  498. <td><input type="text" value="' . esc_attr( $rate['fee'] ) . '" name="' . esc_attr( $this->id .'_fee[' . $i . ']' ) . '" placeholder="' . wc_format_localized_price( 0 ) . '" size="4" class="wc_input_price" /></td>
  499. </tr>';
  500. }
  501. }
  502. ?>
  503. </tbody>
  504. <tfoot>
  505. <tr>
  506. <th colspan="4"><a href="#" class="add button"><?php _e( 'Add Cost', 'woocommerce' ); ?></a> <a href="#" class="remove button"><?php _e( 'Delete selected costs', 'woocommerce' ); ?></a></th>
  507. </tr>
  508. </tfoot>
  509. </table>
  510. <script type="text/javascript">
  511. jQuery(function() {
  512. jQuery('#<?php echo $this->id; ?>_flat_rates').on( 'click', 'a.add', function(){
  513. var size = jQuery('#<?php echo $this->id; ?>_flat_rates tbody .flat_rate').size();
  514. jQuery('<tr class="flat_rate">\
  515. <th class="check-column"><input type="checkbox" name="select" /></th>\
  516. <td class="flat_rate_class">\
  517. <select name="<?php echo $this->id; ?>_class[' + size + ']" class="select">\
  518. <?php
  519. if (WC()->shipping->get_shipping_classes()) :
  520. foreach (WC()->shipping->get_shipping_classes() as $class) :
  521. echo '<option value="' . esc_attr( $class->slug ) . '">' . esc_js( $class->name ) . '</option>';
  522. endforeach;
  523. else :
  524. echo '<option value="">'.__( 'Select a class&hellip;', 'woocommerce' ).'</option>';
  525. endif;
  526. ?>\
  527. </select>\
  528. </td>\
  529. <td><input type="text" name="<?php echo $this->id; ?>_cost[' + size + ']" placeholder="<?php echo wc_format_localized_price( 0 ); ?>" size="4" class="wc_input_price" /></td>\
  530. <td><input type="text" name="<?php echo $this->id; ?>_fee[' + size + ']" placeholder="<?php echo wc_format_localized_price( 0 ); ?>" size="4" class="wc_input_price" /></td>\
  531. </tr>').appendTo('#<?php echo $this->id; ?>_flat_rates table tbody');
  532. return false;
  533. });
  534. // Remove row
  535. jQuery('#<?php echo $this->id; ?>_flat_rates').on( 'click', 'a.remove', function(){
  536. var answer = confirm("<?php _e( 'Delete the selected rates?', 'woocommerce' ); ?>");
  537. if (answer) {
  538. jQuery('#<?php echo $this->id; ?>_flat_rates table tbody tr th.check-column input:checked').each(function(i, el){
  539. jQuery(el).closest('tr').remove();
  540. });
  541. }
  542. return false;
  543. });
  544. });
  545. </script>
  546. </td>
  547. </tr>
  548. <?php
  549. return ob_get_clean();
  550. }
  551. /**
  552. * process_flat_rates function.
  553. *
  554. * @access public
  555. * @return void
  556. */
  557. function process_flat_rates() {
  558. // Save the rates
  559. $flat_rate_class = array();
  560. $flat_rate_cost = array();
  561. $flat_rate_fee = array();
  562. $flat_rates = array();
  563. if ( isset( $_POST[ $this->id . '_class'] ) ) $flat_rate_class = array_map( 'wc_clean', $_POST[ $this->id . '_class'] );
  564. if ( isset( $_POST[ $this->id . '_cost'] ) ) $flat_rate_cost = array_map( 'stripslashes', $_POST[ $this->id . '_cost'] );
  565. if ( isset( $_POST[ $this->id . '_fee'] ) ) $flat_rate_fee = array_map( 'stripslashes', $_POST[ $this->id . '_fee'] );
  566. // Get max key
  567. $values = $flat_rate_class;
  568. ksort( $values );
  569. $value = end( $values );
  570. $key = key( $values );
  571. for ( $i = 0; $i <= $key; $i++ ) {
  572. if ( ! empty( $flat_rate_class[ $i ] ) && isset( $flat_rate_cost[ $i ] ) && isset( $flat_rate_fee[ $i ] ) ) {
  573. $flat_rate_cost[ $i ] = wc_format_decimal( $flat_rate_cost[$i] );
  574. if ( ! strstr( $flat_rate_fee[$i], '%' ) ) {
  575. $flat_rate_fee[ $i ] = wc_format_decimal( $flat_rate_fee[$i] );
  576. } else {
  577. $flat_rate_fee[ $i ] = wc_clean( $flat_rate_fee[$i] );
  578. }
  579. // Add to flat rates array
  580. $flat_rates[ urldecode( sanitize_title( $flat_rate_class[ $i ] ) ) ] = array(
  581. 'cost' => $flat_rate_cost[ $i ],
  582. 'fee' => $flat_rate_fee[ $i ],
  583. );
  584. }
  585. }
  586. update_option( $this->flat_rate_option, $flat_rates );
  587. $this->get_flat_rates();
  588. }
  589. /**
  590. * save_default_costs function.
  591. *
  592. * @access public
  593. * @param array $fields
  594. * @return array
  595. */
  596. function save_default_costs( $fields ) {
  597. $default_cost = ( $_POST['default_cost'] === '' ) ? '' : wc_format_decimal( $_POST['default_cost'] );
  598. if ( ! strstr( $_POST['default_fee'], '%' ) ) {
  599. $default_fee = ( $_POST['default_fee'] === '' ) ? '' : wc_format_decimal( $_POST['default_fee'] );
  600. } else {
  601. $default_fee = wc_clean( $_POST['default_fee'] );
  602. }
  603. $fields['cost'] = $default_cost;
  604. $fields['fee'] = $default_fee;
  605. return $fields;
  606. }
  607. /**
  608. * get_flat_rates function.
  609. *
  610. * @access public
  611. * @return void
  612. */
  613. function get_flat_rates() {
  614. $this->flat_rates = array_filter( (array) get_option( $this->flat_rate_option ) );
  615. }
  616. }