Automattic\WooCommerce\StoreApi\Schemas\V1\Agentic

CheckoutSessionSchema::format_totals_from_orderprotectedWC 1.0

Format totals array from order.

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_from_order( $order );
$order(WC_Order) (required)
Order object.

CheckoutSessionSchema::format_totals_from_order() code WC 10.7.0

protected function format_totals_from_order( $order ) {
	$totals = [];

	// Items base amount.
	$items_base = 0;
	foreach ( $order->get_items() as $item ) {
		$product     = $item->get_product();
		$items_base += $product->get_price() * $item->get_quantity();
	}
	$totals[] = [
		'type'         => TotalType::ITEMS_BASE_AMOUNT,
		'display_text' => __( 'Items Base Amount', 'woocommerce' ),
		'amount'       => $this->amount_to_cents( $items_base ),
	];

	// Items discount.
	$discount = $order->get_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( $items_base - $discount ),
	];

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

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

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

	return $totals;
}