Fix issue when customer is VAT exempt and prices include tax

Ref: https://github.com/woocommerce/woocommerce-eu-vat-number/issues/82

In this scenario, base taxes need to be removed from the item price.
This commit is contained in:
Mike Jolley 2017-10-18 09:31:35 -07:00
parent 2b3f524d35
commit f27c8a226f
1 changed files with 36 additions and 9 deletions

View File

@ -410,6 +410,27 @@ final class WC_Cart_Totals {
return ( $a->sort < $b->sort ) ? -1 : 1;
}
/**
* Ran to remove all base taxes from an item. Used when prices include tax, and the customer is tax exempt.
*
* @since 3.2.2
* @param object $item Item to adjust the prices of.
* @return object
*/
protected function remove_item_base_taxes( $item ) {
if ( $item->price_includes_tax ) {
$base_tax_rates = WC_Tax::get_base_tax_rates( $item->product->get_tax_class( 'unfiltered' ) );
// Work out a new base price without the shop's base tax.
$taxes = WC_Tax::calc_tax( $item->subtotal, $base_tax_rates, true, true );
// Now we have a new item price (excluding TAX).
$item->subtotal = $item->subtotal - array_sum( $taxes );
$item->price_includes_tax = false;
}
return $item;
}
/**
* Only ran if woocommerce_adjust_non_base_location_prices is true.
*
@ -423,6 +444,7 @@ final class WC_Cart_Totals {
* @return object
*/
protected function adjust_non_base_location_price( $item ) {
if ( $item->price_includes_tax ) {
$base_tax_rates = WC_Tax::get_base_tax_rates( $item->product->get_tax_class( 'unfiltered' ) );
if ( $item->tax_rates !== $base_tax_rates ) {
@ -433,6 +455,7 @@ final class WC_Cart_Totals {
$item->subtotal = $item->subtotal - array_sum( $taxes );
$item->price_includes_tax = false;
}
}
return $item;
}
@ -655,9 +678,13 @@ final class WC_Cart_Totals {
*/
protected function calculate_item_subtotals() {
foreach ( $this->items as $item_key => $item ) {
if ( $item->price_includes_tax && apply_filters( 'woocommerce_adjust_non_base_location_prices', true ) ) {
if ( $item->price_includes_tax ) {
if ( $this->cart->get_customer()->get_is_vat_exempt() ) {
$item = $this->remove_item_base_taxes( $item );
} elseif ( apply_filters( 'woocommerce_adjust_non_base_location_prices', true ) ) {
$item = $this->adjust_non_base_location_price( $item );
}
}
$subtotal_taxes = array();