Add method to get rates based on order.

This method will prioritize getting rates from billing/shipping address instead of `WC()->customer` which in irrevelant in context of editing order from admin screen.
This commit is contained in:
vedanshujain 2020-07-10 00:11:37 +05:30
parent 24a69fd249
commit 00b7b40c37
1 changed files with 17 additions and 2 deletions

View File

@ -1279,7 +1279,7 @@ abstract class WC_Abstract_Order extends WC_Abstract_Legacy_Order {
// If the prices include tax, discounts should be taken off the tax inclusive prices like in the cart.
if ( $this->get_prices_include_tax() && wc_tax_enabled() && 'taxable' === $item->get_tax_status() ) {
$taxes = WC_Tax::calc_tax( $amount, WC_Tax::get_rates_from_location( $item->get_tax_class(), $tax_location ), true );
$taxes = WC_Tax::calc_tax( $amount, $this->get_tax_rates( $item->get_tax_class(), $tax_location ), true );
// Use unrounded taxes so totals will be re-calculated accurately, like in cart.
$amount = $amount - array_sum( $taxes );
@ -1330,7 +1330,7 @@ abstract class WC_Abstract_Order extends WC_Abstract_Legacy_Order {
continue;
}
$taxes = array_sum( WC_Tax::calc_tax( $item_discount_amount, WC_Tax::get_rates_from_location( $item->get_tax_class(), $tax_location ), $this->get_prices_include_tax() ) );
$taxes = array_sum( WC_Tax::calc_tax( $item_discount_amount, $this->get_tax_rates( $item->get_tax_class(), $tax_location ), $this->get_prices_include_tax() ) );
if ( 'yes' !== get_option( 'woocommerce_tax_round_at_subtotal' ) ) {
$taxes = wc_round_tax_total( $taxes );
}
@ -1524,6 +1524,21 @@ abstract class WC_Abstract_Order extends WC_Abstract_Legacy_Order {
return apply_filters( 'woocommerce_order_get_tax_location', $args, $this );
}
/**
* Get tax rates for an order. Use order's shipping or billing address, defaults to base location.
*
* @param string $tax_class Tax class to get rates for.
* @param array $location_args Location to compute rates for. Should be in form: array( country, state, postcode, city).
* @param object $customer Only used to maintain backward compatibility for filter `woocommerce-matched_rates`.
*
* @return mixed|void Tax rates.
*/
protected function get_tax_rates( $tax_class, $location_args = array(), $customer = null ) {
$tax_location = $this->get_tax_location( $location_args );
$tax_location = array( $tax_location['country'], $tax_location['state'], $tax_location['postcode'], $tax_location['city'] );
return WC_Tax::get_rates_from_location( $tax_class, $tax_location, $customer );
}
/**
* Calculate taxes for all line items and shipping, and store the totals and tax rows.
*