Fixed object subtotal retrieved in Discounts class for orders when prices include tax

This commit is contained in:
Leanza Francesco 2022-02-08 10:11:34 +01:00
parent f3927c786a
commit eac4750cb0
2 changed files with 24 additions and 1 deletions

View File

@ -445,6 +445,16 @@ abstract class WC_Abstract_Order extends WC_Abstract_Legacy_Order {
return apply_filters( 'woocommerce_order_get_subtotal', (float) $subtotal, $this );
}
/**
* Gets order subtotal tax.
*
* @return float
*/
public function get_subtotal_tax() {
$subtotal = NumberUtil::round( $this->get_cart_subtotal_tax_for_order(), wc_get_price_decimals() );
return apply_filters( 'woocommerce_order_get_subtotal_tax', (float) $subtotal, $this );
}
/**
* Get taxes, merged by code, formatted ready for output.
*
@ -1695,6 +1705,19 @@ abstract class WC_Abstract_Order extends WC_Abstract_Legacy_Order {
)
);
}
/**
* Helper function.
* If you add all items in this order in cart again, this would be the cart subtotal tax (assuming all other settings are same).
*
* @return float Cart subtotal tax.
*/
protected function get_cart_subtotal_tax_for_order() {
return wc_remove_number_precision(
$this->get_rounded_items_total(
$this->get_values_for_total( 'subtotal_tax' )
)
);
}
/**
* Helper function.

View File

@ -948,7 +948,7 @@ class WC_Discounts {
if ( $this->object->get_prices_include_tax() ) {
// Add tax to tax-exclusive subtotal.
$subtotal = $subtotal + wc_add_number_precision( NumberUtil::round( $this->object->get_total_tax(), wc_get_price_decimals() ) );
$subtotal = $subtotal + wc_add_number_precision( NumberUtil::round( $this->object->get_subtotal_tax(), wc_get_price_decimals() ) );
}
return $subtotal;