Automattic\WooCommerce\StoreApi\Schemas\V1\Agentic
CheckoutSessionSchema::format_line_items_from_order
Format line items from order.
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_order( $order );
- $order(WC_Order) (required)
- Order object.
CheckoutSessionSchema::format_line_items_from_order() CheckoutSessionSchema::format line items from order code WC 10.7.0
protected function format_line_items_from_order( $order ) {
$items = [];
foreach ( $order->get_items() as $item_id => $item ) {
$quantity = $item->get_quantity();
$base_amount = $this->amount_to_cents( $item->get_subtotal() );
$discount = $this->amount_to_cents( $item->get_subtotal() - $item->get_total() );
$subtotal = $base_amount - $discount;
$tax = $this->amount_to_cents( $item->get_total_tax() );
$total = $subtotal + $tax;
// Use product_id from the order item, with variation_id as fallback.
$item_product_id = $item->get_variation_id() ? $item->get_variation_id() : $item->get_product_id();
$items[] = [
'id' => (string) $item_id,
'item' => [
'id' => (string) $item_product_id,
'quantity' => $quantity,
],
'base_amount' => $base_amount,
'discount' => $discount,
'subtotal' => $subtotal,
'tax' => $tax,
'total' => $total,
];
}
return $items;
}