From cf81e63b3c08d7885ed8dbe2632e38e1c64f1334 Mon Sep 17 00:00:00 2001 From: Patrick Rauland Date: Mon, 24 Feb 2014 12:54:56 -0600 Subject: [PATCH] adding new method to calculate percentage rate adjustment for flat rate shipping --- .../flat-rate/class-wc-shipping-flat-rate.php | 42 +++++++++++-------- 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/includes/shipping/flat-rate/class-wc-shipping-flat-rate.php b/includes/shipping/flat-rate/class-wc-shipping-flat-rate.php index 9be14d270ae..e8e0fc74f2b 100644 --- a/includes/shipping/flat-rate/class-wc-shipping-flat-rate.php +++ b/includes/shipping/flat-rate/class-wc-shipping-flat-rate.php @@ -273,12 +273,7 @@ class WC_Shipping_Flat_Rate extends WC_Shipping_Method { if ( $this_cost_percents ) { foreach ( $this->find_shipping_classes( $package ) as $shipping_class => $items ){ foreach ( $items as $item_id => $values ) { - if ($this_cost_mathop == '+') { - $this_cost += $this_cost_percents * $values['line_total']; - } - else { - $this_cost -= $this_cost_percents * $values['line_total']; - } + $this_cost = $this->calc_percentage_adjustment( $this_cost, $this_cost_percents, $this_cost_mathop, $values['line_total'] ); } } } @@ -289,24 +284,14 @@ class WC_Shipping_Flat_Rate extends WC_Shipping_Method { // Factor $this_cost by the percentage if provided. if ( $this_cost_percents ) { foreach ( $package['contents'] as $item_id => $values ) { - if ($this_cost_mathop == '+') { - $this_cost += $this_cost_percents * $values['line_total']; - } - else { - $this_cost -= $this_cost_percents * $values['line_total']; - } + $this_cost = $this->calc_percentage_adjustment( $this_cost, $this_cost_percents, $this_cost_mathop, $values['line_total'] ); } } break; case 'order' : // Factor $this_cost by the percentage if provided. if ( $this_cost_percents ) { - if ($this_cost_mathop == '+') { - $this_cost += $this_cost_percents * $package['contents_cost']; - } - else { - $this_cost -= $this_cost_percents * $package['contents_cost']; - } + $this_cost = $this->calc_percentage_adjustment( $this_cost, $this_cost_percents, $this_cost_mathop, $package['contents_cost'] ); } break; } @@ -323,6 +308,27 @@ class WC_Shipping_Flat_Rate extends WC_Shipping_Method { } + /** + * Calculate the percentage adjustment for each shipping rate. + * + * @access public + * @param float $cost + * @param float $percent_adjustment + * @param string $percent_operator + * @param float $base_price + * @return float + */ + function calc_percentage_adjustment( $cost, $percent_adjustment, $percent_operator, $base_price ) { + if ( '+' == $percent_operator ) { + $cost += $percent_adjustment * $base_price; + } else { + $cost -= $percent_adjustment * $base_price; + } + + return $cost; + } + + /** * order_shipping function. *