2019-10-14 15:14:13 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* This ongoing trait will have shared calculation logic between WC_Abstract_Order and WC_Cart_Totals classes.
|
|
|
|
*
|
2020-08-05 16:36:24 +00:00
|
|
|
* @package WooCommerce\Traits
|
2019-10-14 15:14:13 +00:00
|
|
|
* @version 3.9.0
|
|
|
|
*/
|
|
|
|
|
2020-10-01 08:57:12 +00:00
|
|
|
use Automattic\WooCommerce\Utilities\NumberUtil;
|
|
|
|
|
2019-10-14 15:14:13 +00:00
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Trait WC_Item_Totals.
|
|
|
|
*
|
2019-10-14 18:53:16 +00:00
|
|
|
* Right now this do not have much, but plan is to eventually move all shared calculation logic between Orders and Cart in this file.
|
|
|
|
*
|
2019-10-14 15:14:13 +00:00
|
|
|
* @since 3.9.0
|
|
|
|
*/
|
|
|
|
trait WC_Item_Totals {
|
|
|
|
|
|
|
|
/**
|
2019-10-14 18:53:16 +00:00
|
|
|
* Line items to calculate. Define in child class.
|
2019-10-14 15:14:13 +00:00
|
|
|
*
|
|
|
|
* @since 3.9.0
|
2019-10-14 18:53:16 +00:00
|
|
|
* @param string $field Field name to calculate upon.
|
|
|
|
*
|
|
|
|
* @return array having `total`|`subtotal` property.
|
2019-10-14 15:14:13 +00:00
|
|
|
*/
|
2019-10-14 18:53:16 +00:00
|
|
|
abstract protected function get_values_for_total( $field );
|
2019-10-14 15:14:13 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return rounded total based on settings. Will be used by Cart and Orders.
|
|
|
|
*
|
|
|
|
* @since 3.9.0
|
|
|
|
*
|
2019-11-25 20:00:45 +00:00
|
|
|
* @param array $values Values to round. Should be with precision.
|
2019-10-14 15:14:13 +00:00
|
|
|
*
|
|
|
|
* @return float|int Appropriately rounded value.
|
|
|
|
*/
|
2019-11-25 20:00:45 +00:00
|
|
|
public static function get_rounded_items_total( $values ) {
|
2019-10-14 15:14:13 +00:00
|
|
|
return array_sum(
|
|
|
|
array_map(
|
2019-11-25 20:00:45 +00:00
|
|
|
array( self::class, 'round_item_subtotal' ),
|
|
|
|
$values
|
2019-10-14 15:14:13 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Apply rounding to item subtotal before summing.
|
|
|
|
*
|
|
|
|
* @since 3.9.0
|
|
|
|
* @param float $value Item subtotal value.
|
|
|
|
* @return float
|
|
|
|
*/
|
2019-11-25 20:00:45 +00:00
|
|
|
public static function round_item_subtotal( $value ) {
|
|
|
|
if ( ! self::round_at_subtotal() ) {
|
2020-10-01 08:57:12 +00:00
|
|
|
$value = NumberUtil::round( $value );
|
2019-10-14 15:14:13 +00:00
|
|
|
}
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Should always round at subtotal?
|
|
|
|
*
|
|
|
|
* @since 3.9.0
|
|
|
|
* @return bool
|
|
|
|
*/
|
2019-11-25 20:00:45 +00:00
|
|
|
protected static function round_at_subtotal() {
|
2019-10-14 15:14:13 +00:00
|
|
|
return 'yes' === get_option( 'woocommerce_tax_round_at_subtotal' );
|
|
|
|
}
|
2019-11-25 20:00:45 +00:00
|
|
|
|
2019-11-26 23:13:23 +00:00
|
|
|
/**
|
|
|
|
* Apply rounding to an array of taxes before summing. Rounds to store DP setting, ignoring precision.
|
|
|
|
*
|
|
|
|
* @since 3.2.6
|
|
|
|
* @param float $value Tax value.
|
|
|
|
* @param bool $in_cents Whether precision of value is in cents.
|
|
|
|
* @return float
|
|
|
|
*/
|
|
|
|
protected static function round_line_tax( $value, $in_cents = true ) {
|
|
|
|
if ( ! self::round_at_subtotal() ) {
|
|
|
|
$value = wc_round_tax_total( $value, $in_cents ? 0 : null );
|
|
|
|
}
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
|
2019-10-14 15:14:13 +00:00
|
|
|
}
|