diff --git a/includes/class-wc-discounts.php b/includes/class-wc-discounts.php index 6133500ce7b..cdc16c5a64a 100644 --- a/includes/class-wc-discounts.php +++ b/includes/class-wc-discounts.php @@ -78,10 +78,11 @@ class WC_Discounts { * Get all discount totals with precision. * * @since 3.2.0 + * @param bool $in_cents Should the totals be returned in cents, or without precision. * @return array */ - public function get_discounts() { - return $this->discounts; + public function get_discounts( $in_cents = false ) { + return $in_cents ? $this->discounts : array_map( array( $this, 'remove_precision' ), $this->discounts ); } /** @@ -173,13 +174,39 @@ class WC_Discounts { } /** - * Remove precision from a price. + * Add precision (deep) to a price. * - * @param int $value Value to remove precision from. + * @since 3.2.0 + * @param int|array $value Value to remove precision from. + * @return float + */ + protected function add_precision( $value ) { + if ( is_array( $value ) ) { + foreach ( $value as $key => $subvalue ) { + $value[ $key ] = $this->add_precision( $subvalue ); + } + } else { + $value = $value * $this->precision; + } + return $value; + } + + /** + * Remove precision (deep) from a price. + * + * @since 3.2.0 + * @param int|array $value Value to remove precision from. * @return float */ protected function remove_precision( $value ) { - return wc_format_decimal( $value / $this->precision, wc_get_price_decimals() ); + if ( is_array( $value ) ) { + foreach ( $value as $key => $subvalue ) { + $value[ $key ] = $this->remove_precision( $subvalue ); + } + } else { + $value = wc_format_decimal( $value / $this->precision, wc_get_price_decimals() ); + } + return $value; } /** diff --git a/includes/class-wc-totals.php b/includes/class-wc-totals.php index 4a8ac0f1031..f79a176e2a2 100644 --- a/includes/class-wc-totals.php +++ b/includes/class-wc-totals.php @@ -408,7 +408,7 @@ class WC_Totals { $discounts->apply_coupon( $coupon ); } - $this->discount_totals = $discounts->get_discounts(); + $this->discount_totals = $discounts->get_discounts( true ); $this->totals['discounts_total'] = array_sum( $this->discount_totals ); // See how much tax was 'discounted'. diff --git a/tests/unit-tests/discounts/discounts.php b/tests/unit-tests/discounts/discounts.php index 7bf6bf56d95..850235154fc 100644 --- a/tests/unit-tests/discounts/discounts.php +++ b/tests/unit-tests/discounts/discounts.php @@ -6,8 +6,9 @@ */ class WC_Tests_Discounts extends WC_Unit_Test_Case { - protected function get_items_for_discounts_class( $items ) { - $items = array(); + protected function get_items_for_discounts_class() { + $items = array(); + $precision = pow( 10, wc_get_price_decimals() ); foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) { $item = (object) array( 'key' => '', @@ -25,7 +26,7 @@ class WC_Tests_Discounts extends WC_Unit_Test_Case { ); $item->key = $cart_item_key; $item->quantity = $cart_item['quantity']; - $item->price = $this->add_precision( $cart_item['data']->get_price() ) * $cart_item['quantity']; + $item->price = $cart_item['data']->get_price() * $precision * $cart_item['quantity']; $item->product = $cart_item['data']; $items[ $cart_item_key ] = $item; } diff --git a/tests/unit-tests/totals/totals.php b/tests/unit-tests/totals/totals.php index 41ab46be0a4..5789315110f 100644 --- a/tests/unit-tests/totals/totals.php +++ b/tests/unit-tests/totals/totals.php @@ -113,7 +113,7 @@ class WC_Tests_Totals extends WC_Unit_Test_Case { 'items_total_tax' => 5.40, 'total' => 90.40, 'taxes' => array( - 1 => array( + $this->ids['tax_rate_ids'][0] => array( 'tax_total' => 11.40, 'shipping_tax_total' => 2.00, ),