Automattic\WooCommerce\StoreApi\Schemas\V1\Agentic
CheckoutSessionSchema::get_item_response
Convert a WooCommerce cart to the Agentic Checkout session format.
Method of the class: CheckoutSessionSchema{}
No Hooks.
Returns
Array. Formatted checkout session data.
Usage
$CheckoutSessionSchema = new CheckoutSessionSchema(); $CheckoutSessionSchema->get_item_response( $checkout_session );
- $checkout_session(AgenticCheckoutSession) (required)
- Checkout session object.
CheckoutSessionSchema::get_item_response() CheckoutSessionSchema::get item response code WC 10.7.0
public function get_item_response( $checkout_session ) {
$cart = $checkout_session->get_cart();
// If validation already went through and we have errors, no need to repeat them.
if ( ! $checkout_session->get_messages()->has_errors() ) {
// Validate the checkout session. Messages will be added to the collection, if any.
AgenticCheckoutUtils::validate( $checkout_session );
}
$completed_order = WC()->session
? wc_get_order( WC()->session->get( SessionKey::AGENTIC_CHECKOUT_COMPLETED_ORDER_ID ) )
: null;
// Get line items from cart, or from completed order if cart is empty.
$cart_items = $cart->get_cart();
$line_items = $completed_order instanceof WC_Order
? $this->format_line_items_from_order( $completed_order )
: $this->format_line_items_from_cart( $cart_items );
$response = [
'id' => $checkout_session->get_id(),
'buyer' => $completed_order instanceof WC_Order
? $this->format_buyer_from_order( $completed_order )
: $this->format_buyer(),
'payment_provider' => $this->format_payment_provider(),
'status' => AgenticCheckoutUtils::calculate_status( $checkout_session ),
'currency' => $completed_order instanceof WC_Order
? strtolower( $completed_order->get_currency() )
: strtolower( get_woocommerce_currency() ),
'line_items' => $line_items,
'fulfillment_address' => $completed_order instanceof WC_Order
? $this->format_fulfillment_address_from_order( $completed_order )
: $this->format_fulfillment_address(),
'fulfillment_options' => $completed_order instanceof WC_Order
? $this->format_fulfillment_options_from_order( $completed_order )
: $this->format_fulfillment_options(),
'fulfillment_option_id' => $completed_order instanceof WC_Order
? $this->get_selected_fulfillment_option_id_from_order( $completed_order )
: $this->get_selected_fulfillment_option_id(),
'totals' => $completed_order instanceof WC_Order
? $this->format_totals_from_order( $completed_order )
: $this->format_totals( $cart ),
'messages' => $checkout_session->get_messages()->get_formatted_messages(),
'links' => $this->get_links(),
];
// Add order data if a completed order exists.
if ( $completed_order instanceof WC_Order ) {
$response['order'] = [
'id' => (string) $completed_order->get_id(),
'checkout_session_id' => $checkout_session->get_id(),
'permalink_url' => $completed_order->get_checkout_order_received_url(),
];
}
return $response;
}