Automattic\WooCommerce\StoreApi\Schemas\V1\Agentic

CheckoutSessionSchema::format_line_items_from_cartprotectedWC 1.0

Format line items from cart.

Method of the class: CheckoutSessionSchema{}

No Hooks.

Returns

Array. Formatted line items.

Usage

// protected - for code of main (parent) or child class
$result = $this->format_line_items_from_cart( $cart_items );
$cart_items(array) (required)
Cart items array.

CheckoutSessionSchema::format_line_items_from_cart() code WC 10.7.0

protected function format_line_items_from_cart( $cart_items ) {
	$items = [];

	foreach ( $cart_items as $cart_item_key => $cart_item ) {
		$product     = $cart_item['data'];
		$quantity    = $cart_item['quantity'];
		$base_amount = $this->amount_to_cents( $product->get_price() * $quantity );
		$discount    = $this->amount_to_cents( $cart_item['line_subtotal'] - $cart_item['line_total'] );
		$subtotal    = $base_amount - $discount;
		$tax         = $this->amount_to_cents( $cart_item['line_tax'] );
		$total       = $subtotal + $tax;

		$items[] = [
			'id'          => (string) $cart_item_key,
			'item'        => [
				'id'       => (string) $product->get_id(),
				'quantity' => $quantity,
			],
			'base_amount' => $base_amount,
			'discount'    => $discount,
			'subtotal'    => $subtotal,
			'tax'         => $tax,
			'total'       => $total,
		];
	}

	return $items;
}