Leave other fee total logic alone, reduce the fee array to get a simple sum

This commit is contained in:
Matt Harvey 2019-12-18 22:08:39 -08:00
parent 73189340c8
commit 4326e39250
1 changed files with 18 additions and 19 deletions

View File

@ -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 ) {