From 4326e39250d06002f2b5c736e8c1f19fc982f7d7 Mon Sep 17 00:00:00 2001 From: Matt Harvey Date: Wed, 18 Dec 2019 22:08:39 -0800 Subject: [PATCH] Leave other fee total logic alone, reduce the fee array to get a simple sum --- includes/abstracts/abstract-wc-order.php | 37 ++++++++++++------------ 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/includes/abstracts/abstract-wc-order.php b/includes/abstracts/abstract-wc-order.php index a6e67ce4d4b..4bc933be3cc 100644 --- a/includes/abstracts/abstract-wc-order.php +++ b/includes/abstracts/abstract-wc-order.php @@ -1455,26 +1455,13 @@ abstract class WC_Abstract_Order extends WC_Abstract_Legacy_Order { /** * Calculate fees for all line items. + * + * @return float Fee total. */ public function calculate_fees() { - // Sum fee costs. - $fees_total = 0; - - foreach ( $this->get_fees() as $item ) { - $fee_total = $item->get_total(); - - if ( 0 > $fee_total ) { - $max_discount = round( $cart_total + $fees_total + $shipping_total, wc_get_price_decimals() ) * -1; - - if ( $fee_total < $max_discount && 0 > $max_discount ) { - $item->set_total( $max_discount ); - } - } - - $fees_total += $item->get_total(); - } - - return $fees_total; + return array_reduce($this->get_fees(), function($carry, $item) { + return $carry + $item->get_total(); + }); } /** @@ -1578,6 +1565,7 @@ abstract class WC_Abstract_Order extends WC_Abstract_Legacy_Order { public function calculate_totals( $and_taxes = true ) { do_action( 'woocommerce_order_before_calculate_totals', $and_taxes, $this ); + $fees_total = 0; $shipping_total = 0; $cart_subtotal_tax = 0; $cart_total_tax = 0; @@ -1593,7 +1581,18 @@ abstract class WC_Abstract_Order extends WC_Abstract_Legacy_Order { $this->set_shipping_total( $shipping_total ); // Sum fee costs. - $fees_total = $this->calculate_fees(); + foreach ( $this->get_fees() as $item ) { + $fee_total = $item->get_total(); + + if ( 0 > $fee_total ) { + $max_discount = round( $cart_total + $fees_total + $shipping_total, wc_get_price_decimals() ) * -1; + + if ( $fee_total < $max_discount && 0 > $max_discount ) { + $item->set_total( $max_discount ); + } + } + $fees_total += $fee_total; + } // Calculate taxes for items, shipping, discounts. Note; this also triggers save(). if ( $and_taxes ) {