Fix tests and tax merges

This commit is contained in:
Mike Jolley 2017-08-18 15:05:01 +01:00
parent b854c1775c
commit 1c8ad67a9e
5 changed files with 39 additions and 43 deletions

View File

@ -483,10 +483,15 @@ final class WC_Cart_Totals {
* @param array $taxes Taxes to combine.
* @return array
*/
protected function combine_item_taxes( $taxes ) {
protected function combine_item_taxes( $item_taxes ) {
$merged_taxes = array();
foreach ( $taxes as $tax ) {
$merged_taxes = $merged_taxes + $tax;
foreach ( $item_taxes as $taxes ) {
foreach ( $taxes as $tax_id => $tax_amount ) {
if ( ! isset( $merged_taxes[ $tax_id ] ) ) {
$merged_taxes[ $tax_id ] = 0;
}
$merged_taxes[ $tax_id ] += $tax_amount;
}
}
return $merged_taxes;
}

View File

@ -302,7 +302,7 @@ class WC_Cart extends WC_Legacy_Cart {
* @return float
*/
public function get_fee_tax() {
return apply_filters( 'woocommerce_cart_' . __METHOD__, $this->totals['fee_total'] );
return apply_filters( 'woocommerce_cart_' . __METHOD__, $this->totals['fee_tax'] );
}
/**
@ -1259,10 +1259,14 @@ class WC_Cart extends WC_Legacy_Cart {
$this->shipping_methods = $this->needs_shipping() ? $this->get_chosen_shipping_methods( WC()->shipping->calculate_shipping( $this->get_shipping_packages() ) ) : array();
$shipping_taxes = wp_list_pluck( $this->shipping_methods, 'taxes' );
$merged_taxes = array();
foreach ( $shipping_taxes as $tax ) {
$merged_taxes = $merged_taxes + $tax;
$merged_taxes = array();
foreach ( $shipping_taxes as $taxes ) {
foreach ( $taxes as $tax_id => $tax_amount ) {
if ( ! isset( $merged_taxes[ $tax_id ] ) ) {
$merged_taxes[ $tax_id ] = 0;
}
$merged_taxes[ $tax_id ] += $tax_amount;
}
}
$this->set_shipping_total( array_sum( wp_list_pluck( $this->shipping_methods, 'cost' ) ) );
@ -1738,39 +1742,13 @@ class WC_Cart extends WC_Legacy_Cart {
* Calculate fees.
*/
public function calculate_fees() {
// Reset fees before calculation.
$this->fees = array();
$this->set_fee_total( 0 );
$this->set_fee_tax( 0 );
$this->set_fee_taxes( array() );
$this->fees = array();
// Fire an action where developers can add their fees.
do_action( 'woocommerce_cart_calculate_fees', $this );
// If fees were added, total them and calculate tax.
if ( ! empty( $this->fees ) ) {
$fees_total = 0;
$fees_taxes = array();
foreach ( $this->fees as $fee_key => $fee ) {
$fees_total += $fee->amount;
if ( $fee->taxable ) {
$tax_rates = WC_Tax::get_rates( $fee->tax_class );
$fee_taxes = WC_Tax::calc_tax( $fee->amount, $tax_rates, false );
if ( ! empty( $fee_taxes ) ) {
$this->fees[ $fee_key ]->tax = array_sum( $fee_taxes );
$this->fees[ $fee_key ]->tax_data = $fee_taxes;
$fees_taxes = wc_array_merge_recursive_numeric( $fees_taxes, $fee_taxes );
}
}
}
$this->set_fee_total( $fees_total );
$this->set_fee_tax( array_sum( $fees_taxes ) );
$this->set_fee_taxes( $fees_taxes );
}
}
/**

View File

@ -313,12 +313,12 @@ class WC_Checkout {
$order->set_customer_user_agent( wc_get_user_agent() );
$order->set_customer_note( isset( $data['order_comments'] ) ? $data['order_comments'] : '' );
$order->set_payment_method( isset( $available_gateways[ $data['payment_method'] ] ) ? $available_gateways[ $data['payment_method'] ] : $data['payment_method'] );
$order->set_shipping_total( WC()->cart->shipping_total );
$order->set_shipping_total( WC()->cart->get_shipping_total() );
$order->set_discount_total( WC()->cart->get_discount_total() );
$order->set_discount_tax( WC()->cart->get_discount_tax_total() );
$order->set_cart_tax( WC()->cart->tax_total );
$order->set_shipping_tax( WC()->cart->shipping_tax_total );
$order->set_total( WC()->cart->total );
$order->set_discount_tax( WC()->cart->get_discount_tax() );
$order->set_cart_tax( WC()->cart->get_cart_contents_tax() + WC()->cart->get_fee_tax() );
$order->set_shipping_tax( WC()->cart->get_shipping_tax() );
$order->set_total( WC()->cart->get_total( 'edit' ) );
$this->create_order_line_items( $order, WC()->cart );
$this->create_order_fee_lines( $order, WC()->cart );
$this->create_order_shipping_lines( $order, WC()->session->get( 'chosen_shipping_methods' ), WC()->shipping->get_packages() );

View File

@ -75,7 +75,7 @@ abstract class WC_Legacy_Cart {
case 'subtotal_ex_tax' :
return $this->get_subtotal();
case 'tax_total' :
return array_sum( wc_array_merge_recursive_numeric( $this->get_cart_contents_taxes(), $this->get_fee_taxes() ) ); // Should be fees + cart items.
return $this->get_fee_tax() + $this->get_cart_contents_tax();
case 'taxes' :
return $this->get_taxes();
case 'shipping_taxes' :

View File

@ -137,13 +137,26 @@ class WC_Tests_Totals extends WC_Unit_Test_Case {
public function test_cart_totals() {
$cart = WC()->cart;
$this->assertEquals( 40.00, $cart->fee_total );
$this->assertEquals( 40.00, $cart->get_fee_total() );
$this->assertEquals( 27.00, $cart->get_cart_contents_total() );
$this->assertEquals( 90.40, $cart->get_total( 'edit' ) );
$this->assertEquals( 30.00, $cart->get_subtotal() );
$this->assertEquals( 6.00, $cart->get_subtotal_tax() );
$this->assertEquals( 5.40, $cart->get_cart_contents_tax() );
$this->assertEquals( 5.40, array_sum( $cart->get_cart_contents_taxes() ) );
$this->assertEquals( 6.00, $cart->get_fee_tax() );
$this->assertEquals( 6.00, array_sum( $cart->get_fee_taxes() ) );
$this->assertEquals( 3, $cart->get_discount_total() );
$this->assertEquals( 0.60, $cart->get_discount_tax() );
$this->assertEquals( 10, $cart->get_shipping_total() );
$this->assertEquals( 2, $cart->get_shipping_tax() );
$this->assertEquals( 27.00, $cart->cart_contents_total );
$this->assertEquals( 90.40, $cart->total );
$this->assertEquals( 36.00, $cart->subtotal );
$this->assertEquals( 30.00, $cart->subtotal_ex_tax );
$this->assertEquals( 11.40, $cart->tax_total );
$this->assertEquals( 2.40, $cart->discount_cart );
$this->assertEquals( 3, $cart->discount_cart );
$this->assertEquals( 0.60, $cart->discount_cart_tax );
$this->assertEquals( 40.00, $cart->fee_total );
$this->assertEquals( 10, $cart->shipping_total );