Use base taxes instead of current user taxes in wc_get_price_excluding_tax

Modify wc_get_price_excluding_tax so that when there's no user
available from a passed order and the
'woocommerce_adjust_non_base_location_prices' filter returns false,
the shop base location is used for the tax calculation instead of
the location of the currently logged in user.
This commit is contained in:
Nestor Soriano 2021-10-22 11:05:58 +02:00
parent 76c611b2d8
commit d98112e014
No known key found for this signature in database
GPG Key ID: 08110F3518C12CAD
2 changed files with 18 additions and 11 deletions

View File

@ -1083,13 +1083,16 @@ function wc_get_price_excluding_tax( $product, $args = array() ) {
$line_price = $price * $qty;
if ( $product->is_taxable() && wc_prices_include_tax() ) {
$order = ArrayUtil::get_value_or_default( $args, 'order' );
$customer_id = $order ? $order->get_customer_id() : 0;
$customer = $customer_id ? wc_get_container()->get( LegacyProxy::class )->get_instance_of( WC_Customer::class, $customer_id ) : null;
$tax_rates = WC_Tax::get_rates( $product->get_tax_class(), $customer );
$base_tax_rates = WC_Tax::get_base_tax_rates( $product->get_tax_class( 'unfiltered' ) );
$remove_taxes = apply_filters( 'woocommerce_adjust_non_base_location_prices', true ) ? WC_Tax::calc_tax( $line_price, $base_tax_rates, true ) : WC_Tax::calc_tax( $line_price, $tax_rates, true );
$return_price = $line_price - array_sum( $remove_taxes ); // Unrounded since we're dealing with tax inclusive prices. Matches logic in cart-totals class. @see adjust_non_base_location_price.
$order = ArrayUtil::get_value_or_default( $args, 'order' );
$customer_id = $order ? $order->get_customer_id() : 0;
if ( apply_filters( 'woocommerce_adjust_non_base_location_prices', true ) || ! $customer_id ) {
$tax_rates = WC_Tax::get_base_tax_rates( $product->get_tax_class( 'unfiltered' ) );
} else {
$customer = wc_get_container()->get( LegacyProxy::class )->get_instance_of( WC_Customer::class, $customer_id );
$tax_rates = WC_Tax::get_rates( $product->get_tax_class(), $customer );
}
$remove_taxes = WC_Tax::calc_tax( $line_price, $tax_rates, true );
$return_price = $line_price - array_sum( $remove_taxes ); // Unrounded since we're dealing with tax inclusive prices. Matches logic in cart-totals class. @see adjust_non_base_location_price.
} else {
$return_price = $line_price;
}

View File

@ -25,6 +25,7 @@ class WC_Product_Functions_Tests extends \WC_Unit_Test_Case {
*/
public function test_wc_get_price_excluding_tax_passes_order_customer_to_get_rates_if_order_is_available( $pass_order, $customer_id ) {
$customer_passed_to_get_rates = false;
$get_base_rates_invoked = false;
$customer_id_passed_to_wc_customer_constructor = false;
FunctionsMockerHack::add_function_mocks(
@ -40,7 +41,8 @@ class WC_Product_Functions_Tests extends \WC_Unit_Test_Case {
'get_rates' => function( $tax_class, $customer ) use ( &$customer_passed_to_get_rates ) {
$customer_passed_to_get_rates = $customer;
},
'get_base_tax_rates' => function( $tax_class ) {
'get_base_tax_rates' => function( $tax_class ) use ( &$get_base_rates_invoked ) {
$get_base_rates_invoked = true;
return 0;
},
'calc_tax' => function( $price, $rates, $price_includes_tax = false, $deprecated = false ) {
@ -93,15 +95,17 @@ class WC_Product_Functions_Tests extends \WC_Unit_Test_Case {
$this->assertEquals( $order->get_customer_id(), $customer_id_passed_to_wc_customer_constructor );
if ( $customer_id ) {
$this->assertFalse( $get_base_rates_invoked );
$this->assertSame( $customer, $customer_passed_to_get_rates );
} else {
$this->assertNull( $customer_passed_to_get_rates );
$this->assertFalse( $customer_passed_to_get_rates );
$this->assertTrue( $get_base_rates_invoked );
}
} else {
wc_get_price_excluding_tax( $product );
$this->assertFalse( $customer_id_passed_to_wc_customer_constructor );
$this->assertNull( $customer_passed_to_get_rates );
$this->assertFalse( $customer_passed_to_get_rates );
$this->assertTrue( $get_base_rates_invoked );
}
// phpcs:enable Squiz.Commenting