Improved how taxes are applied

If by default the taxes are based on the shipping address and the current order doesn't have any, it would use the billing address rather than using the Shopping base location.

It's basically what WooCommerce is [doing already in Javascript](https://github.com/woocommerce/woocommerce/blob/master/assets/js/admin/meta-boxes-order.js#L526-L575). This kind of checks should be done in the backend, never in the client side.
This commit is contained in:
Cesar Rodas 2017-06-01 15:51:55 -04:00
parent d161ba0216
commit c96c62f380
2 changed files with 53 additions and 0 deletions

View File

@ -1011,11 +1011,19 @@ abstract class WC_Abstract_Order extends WC_Abstract_Legacy_Order {
/**
* Calculate taxes for all line items and shipping, and store the totals and tax rows.
*
* If by default the taxes are based on the shipping address and the current order doesn't
* have any, it would use the billing address rather than using the Shopping base location.
*
* Will use the base country unless customer addresses are set.
* @param $args array Added in 3.0.0 to pass things like location.
*/
public function calculate_taxes( $args = array() ) {
$tax_based_on = get_option( 'woocommerce_tax_based_on' );
if ( 'shipping' === $tax_based_on && ! $this->get_shipping_country() ) {
$tax_based_on = 'billing';
}
$args = wp_parse_args( $args, array(
'country' => 'billing' === $tax_based_on ? $this->get_billing_country() : $this->get_shipping_country(),
'state' => 'billing' === $tax_based_on ? $this->get_billing_state() : $this->get_shipping_state(),

View File

@ -647,6 +647,51 @@ class WC_Tests_CRUD_Orders extends WC_Unit_Test_Case {
update_option( 'woocommerce_calc_taxes', 'no' );
}
function test_calculate_taxes_issue_with_addresses() {
global $wpdb;
update_option( 'woocommerce_calc_taxes', 'yes' );
$wpdb->query( "DELETE FROM {$wpdb->prefix}woocommerce_tax_rates" );
$wpdb->query( "DELETE FROM {$wpdb->prefix}woocommerce_tax_rate_locations" );
$taxes = array();
$taxes[] = WC_Tax::_insert_tax_rate( array(
'tax_rate_country' => 'US',
'tax_rate_state' => '',
'tax_rate' => '20.0000',
'tax_rate_name' => 'TAX',
'tax_rate_priority' => '1',
'tax_rate_compound' => '0',
'tax_rate_shipping' => '1',
'tax_rate_order' => '1',
'tax_rate_class' => '',
) );
$taxes[] = WC_Tax::_insert_tax_rate( array(
'tax_rate_country' => 'PY',
'tax_rate_state' => '',
'tax_rate' => '10.0000',
'tax_rate_name' => 'TAX',
'tax_rate_priority' => '1',
'tax_rate_compound' => '0',
'tax_rate_shipping' => '1',
'tax_rate_order' => '1',
'tax_rate_class' => '',
) );
update_option( 'woocommerce_default_country', 'PY:Central' );
update_option( 'woocommerce_tax_based_on', 'shipping' );
$order = new WC_Order;
$order->set_billing_country( 'US' );
$order->set_billing_state( 'CA' );
$order->add_product( WC_Helper_Product::create_simple_product(), 4 );
$order->calculate_taxes();
$tax = $order->get_taxes();
$this->assertEquals( 1, count( $tax ) );
$this->assertEquals( 'US-TAX-1', current( $tax )->get_name() );
}
/**
* Test: calculate_totals
*/