Automattic\WooCommerce\StoreApi\Schemas\V1

CheckoutSchema::get_draft_responsepublicWC 1.0

Build a checkout response for a session with no persisted order.

Session-owned values (payment method, customer note) are read internally so the caller doesn't need to forward them.

Method of the class: CheckoutSchema{}

No Hooks.

Returns

Array.

Usage

$CheckoutSchema = new CheckoutSchema();
$CheckoutSchema->get_draft_response( $cart, $customer );
$cart(WC_Cart) (required)
Cart object.
$customer(WC_Customer) (required)
Customer object (typically wc()->customer).

CheckoutSchema::get_draft_response() code WC 10.9.1

public function get_draft_response( \WC_Cart $cart, \WC_Customer $customer ) {
	// Use the shopper's session-stored selection so a PATCH-time choice survives the next render — but only if the gateway is still enabled, since the slug can outlive the gateway (admin disables it, payment plugin deactivated).
	$session_payment_method = (string) WC()->session->get( 'chosen_payment_method' );
	$enabled_gateways       = PaymentUtils::get_enabled_payment_gateways();
	$payment_method         = ( '' !== $session_payment_method && isset( $enabled_gateways[ $session_payment_method ] ) )
		? $session_payment_method
		: (string) PaymentUtils::get_default_payment_method();

	return [
		'order_id'           => 0,
		'status'             => 'checkout-draft',
		'order_key'          => '',
		'order_number'       => '0',
		'customer_note'      => (string) ( WC()->session->get( 'store_api_customer_note' ) ?? '' ),
		'customer_id'        => $customer->get_id(),
		'billing_address'    => (object) $this->billing_address_schema->get_item_response( $customer ),
		'shipping_address'   => (object) $this->shipping_address_schema->get_item_response( $customer ),
		'payment_method'     => $payment_method,
		'payment_result'     => null,
		'additional_fields'  => (object) $this->get_additional_fields_response( $customer ),
		'__experimentalCart' => (object) $this->cart_schema->get_item_response( $cart ),
		self::EXTENDING_KEY  => $this->get_extended_data( self::IDENTIFIER ),
	];
}