WC_Abstract_Order::calculate_totals()publicWC 2.2

Calculate totals by looking at the contents of the order. Stores the totals and returns the orders final total.

Метод класса: WC_Abstract_Order{}

Возвращает

float. calculated grand total.

Использование

$WC_Abstract_Order = new WC_Abstract_Order();
$WC_Abstract_Order->calculate_totals( $and_taxes );
$and_taxes(true|false)
Calc taxes if true.
По умолчанию: true

Список изменений

С версии 2.2 Введена.

Код WC_Abstract_Order::calculate_totals() WC 8.7.0

public function calculate_totals( $and_taxes = true ) {
	do_action( 'woocommerce_order_before_calculate_totals', $and_taxes, $this );

	$fees_total        = 0;
	$shipping_total    = 0;
	$cart_subtotal_tax = 0;
	$cart_total_tax    = 0;

	$cart_subtotal = $this->get_cart_subtotal_for_order();
	$cart_total    = (float) $this->get_cart_total_for_order();

	// Sum shipping costs.
	foreach ( $this->get_shipping_methods() as $shipping ) {
		$shipping_total += NumberUtil::round( $shipping->get_total(), wc_get_price_decimals() );
	}

	$this->set_shipping_total( $shipping_total );

	// Sum fee costs.
	foreach ( $this->get_fees() as $item ) {
		$fee_total = (float) $item->get_total();

		if ( 0 > $fee_total ) {
			$max_discount = NumberUtil::round( $cart_total + $fees_total + $shipping_total, wc_get_price_decimals() ) * -1;

			if ( $fee_total < $max_discount && 0 > $max_discount ) {
				$item->set_total( $max_discount );
			}
		}
		$fees_total += (float) $item->get_total();
	}

	// Calculate taxes for items, shipping, discounts. Note; this also triggers save().
	if ( $and_taxes ) {
		$this->calculate_taxes();
	}

	// Sum taxes again so we can work out how much tax was discounted. This uses original values, not those possibly rounded to 2dp.
	foreach ( $this->get_items() as $item ) {
		$taxes = $item->get_taxes();

		foreach ( $taxes['total'] as $tax_rate_id => $tax ) {
			$cart_total_tax += (float) $tax;
		}

		foreach ( $taxes['subtotal'] as $tax_rate_id => $tax ) {
			$cart_subtotal_tax += (float) $tax;
		}
	}

	$this->set_discount_total( NumberUtil::round( $cart_subtotal - $cart_total, wc_get_price_decimals() ) );
	$this->set_discount_tax( wc_round_tax_total( $cart_subtotal_tax - $cart_total_tax ) );
	$this->set_total( NumberUtil::round( $cart_total + $fees_total + (float) $this->get_shipping_total() + (float) $this->get_cart_tax() + (float) $this->get_shipping_tax(), wc_get_price_decimals() ) );

	do_action( 'woocommerce_order_after_calculate_totals', $and_taxes, $this );

	$this->save();

	return $this->get_total();
}