Automattic\WooCommerce\StoreApi\Schemas\V1\Agentic

CheckoutSessionSchema::format_totalsprotectedWC 1.0

Format totals array.

Method of the class: CheckoutSessionSchema{}

No Hooks.

Returns

Array. Totals array.

Usage

// protected - for code of main (parent) or child class
$result = $this->format_totals( $cart );
$cart(WC_Cart) (required)
Cart object.

CheckoutSessionSchema::format_totals() code WC 10.7.0

protected function format_totals( $cart ) {
	$totals = [];

	// Items base amount.
	$items_base = 0;
	foreach ( $cart->get_cart() as $cart_item ) {
		$product     = $cart_item['data'];
		$items_base += $product->get_price() * $cart_item['quantity'];
	}
	$totals[] = [
		'type'         => TotalType::ITEMS_BASE_AMOUNT,
		'display_text' => __( 'Items Base Amount', 'woocommerce' ),
		'amount'       => $this->amount_to_cents( $items_base ),
	];

	// Items discount.
	$discount = $cart->get_cart_discount_total();
	$totals[] = [
		'type'         => TotalType::ITEMS_DISCOUNT,
		'display_text' => __( 'Items Discount', 'woocommerce' ),
		'amount'       => $this->amount_to_cents( $discount ),
	];

	// Subtotal.
	$totals[] = [
		'type'         => TotalType::SUBTOTAL,
		'display_text' => __( 'Subtotal', 'woocommerce' ),
		'amount'       => $this->amount_to_cents( $cart->get_subtotal() - $discount ),
	];

	// Fulfillment (shipping).
	$totals[] = [
		'type'         => TotalType::FULFILLMENT,
		'display_text' => __( 'Shipping', 'woocommerce' ),
		'amount'       => $this->amount_to_cents( $cart->get_shipping_total() ),
	];

	// Tax.
	$totals[] = [
		'type'         => TotalType::TAX,
		'display_text' => __( 'Tax', 'woocommerce' ),
		'amount'       => $this->amount_to_cents( $cart->get_total_tax() ),
	];

	// Total.
	$totals[] = [
		'type'         => TotalType::TOTAL,
		'display_text' => __( 'Total', 'woocommerce' ),
		'amount'       => $this->amount_to_cents( $cart->get_total( 'edit' ) ),
	];

	return $totals;
}