WooCommerce Code Reference

Checkout extends AbstractCartRoute
in package
Uses DraftOrderTrait, CheckoutTrait

Checkout class.

Table of Contents

IDENTIFIER  = 'checkout'
The route identifier.
SCHEMA_TYPE  = 'checkout'
The routes schema.
SCHEMA_VERSION  = 1
The routes schema version.
$additional_fields_controller  : CheckoutFields
Additional fields controller class instance.
$cart_controller  : CartController
Cart controller class instance.
$cart_item_schema  : CartItemSchema
Schema class for the cart item.
$cart_schema  : CartSchema
Schema class for the cart.
$namespace  : string
Route namespace.
$order_controller  : OrderController
Order controller class instance.
$schema  : AbstractSchema
Schema class instance.
$schema_controller  : SchemaController
Schema Controller instance.
$order  : WC_Order
Holds the current order being processed.
__construct()  : mixed
Constructor.
get_args()  : array<string|int, mixed>
Get method arguments for this REST route.
get_collection_params()  : array<string|int, mixed>
Retrieves the query params for the collections.
get_item_schema()  : array<string|int, mixed>
Get item schema properties.
get_namespace()  : string
Get the namespace for this route.
get_path()  : string
Get the path of this REST route.
get_path_regex()  : string
Get the path of this rest route.
get_response()  : WP_REST_Response
Get the route response based on the type of request.
prepare_item_for_response()  : WP_REST_Response
Prepare a single item for response.
set_namespace()  : mixed
Set the namespace for this route.
add_response_headers()  : WP_REST_Response
Add nonce headers to a response object.
cart_updated()  : mixed
Triggered after an update to cart data. Re-calculates totals and updates draft orders (if they already exist) to keep all data in sync.
check_nonce()  : WP_Error|bool
For non-GET endpoints, require and validate a nonce to prevent CSRF attacks.
error_to_response()  : WP_REST_Response
Converts an error to a response object. Based on \WP_REST_Server.
get_cart_token()  : string
Generates a cart token for the response headers.
get_cart_token_expiration()  : int
Gets the expiration of the cart token. Defaults to 48h.
get_cart_token_secret()  : string
Gets the secret for the cart token using wp_salt.
get_context_param()  : array<string|int, mixed>
Retrieves the context param.
get_draft_order()  : WC_Order|null
Uses the draft order ID to return an order object, if valid.
get_draft_order_id()  : int
Gets draft order data from the customer session.
get_response_by_request_method()  : WP_REST_Response
Get the route response based on the type of request.
get_route_delete_response()  : WP_REST_Response
Get route response for DELETE requests.
get_route_error_response()  : WP_Error
Get route response when something went wrong.
get_route_error_response_from_object()  : WP_Error
Get route response when something went wrong.
get_route_post_response()  : WP_REST_Response
Process an order.
get_route_response()  : WP_REST_Response
Convert the cart into a new draft order, or update an existing draft order, and return an updated cart response.
get_route_update_response()  : WP_REST_Response
Get route response for PUT requests.
is_update_request()  : bool
Are we updating data or getting data?
is_valid_draft_order()  : bool
Whether the passed argument is a draft order or an order that is pending/failed and the cart hasn't changed.
load_cart_session()  : mixed
Load the cart session before handling responses.
prepare_links()  : array<string|int, mixed>
Prepare links for the request.
prepare_response_for_collection()  : array<string|int, mixed>|mixed
Prepares a response for insertion into a collection.
requires_nonce()  : bool
Checks if a nonce is required for the route.
set_draft_order_id()  : mixed
Updates draft order data in the customer session.
add_data_to_error_object()  : mixed
Adds additional data to the \WP_Error object.
create_customer_account()  : int
Create a new account for a customer.
create_or_update_draft_order()  : mixed
Create or update a draft order based on the cart.
get_request_payment_data()  : array<string|int, mixed>
Gets and formats payment request data.
get_request_payment_method()  : WC_Payment_Gateway|null
Gets the chosen payment method from the request.
get_request_payment_method_id()  : string
Gets the chosen payment method ID from the request.
get_request_payment_method_title()  : string
Gets the chosen payment method title from the request.
map_create_account_error()  : Exception
Convert an account creation error to an exception.
persist_additional_fields_for_order()  : mixed
Persist additional fields for the order after validating them.
process_customer()  : mixed
Order processing relating to customer account.
process_payment()  : mixed
Fires an action hook instructing active payment gateways to process the payment for an order and provide a result.
process_without_payment()  : mixed
For orders which do not require payment, just update status.
should_create_customer_account()  : bool
Check request options and store (shop) config to determine if a user account should be created as part of order processing.
update_customer_from_request()  : mixed
Updates the current customer session using data from the request (e.g. address data).
update_order_from_request()  : mixed
Update the current order using the posted values from the request.

Constants

Properties

Methods

get_args()

Get method arguments for this REST route.

public get_args() : array<string|int, mixed>
Return values
array<string|int, mixed>An array of endpoints.

get_collection_params()

Retrieves the query params for the collections.

public get_collection_params() : array<string|int, mixed>
Return values
array<string|int, mixed>Query parameters for the collection.

get_response()

Get the route response based on the type of request.

public get_response(WP_REST_Request $request) : WP_REST_Response
Parameters
$request : WP_REST_Request

Request object.

Return values
WP_REST_Response

prepare_item_for_response()

Prepare a single item for response.

public prepare_item_for_response(mixed $item, WP_REST_Request $request) : WP_REST_Response
Parameters
$item : mixed

Item to format to schema.

$request : WP_REST_Request

Request object.

Return values
WP_REST_Response$response Response data.

set_namespace()

Set the namespace for this route.

public set_namespace(string $namespace) : mixed
Parameters
$namespace : string

Given namespace.

Return values
mixed

add_response_headers()

Add nonce headers to a response object.

protected add_response_headers(WP_REST_Response $response) : WP_REST_Response
Parameters
$response : WP_REST_Response

The response object.

Return values
WP_REST_Response

cart_updated()

Triggered after an update to cart data. Re-calculates totals and updates draft orders (if they already exist) to keep all data in sync.

protected cart_updated(WP_REST_Request $request) : mixed
Parameters
$request : WP_REST_Request

Request object.

Return values
mixed

check_nonce()

For non-GET endpoints, require and validate a nonce to prevent CSRF attacks.

protected check_nonce(WP_REST_Request $request) : WP_Error|bool

Nonces will mismatch if the logged in session cookie is different! If using a client to test, set this cookie to match the logged in cookie in your browser.

Parameters
$request : WP_REST_Request

Request object.

Return values
WP_Error|bool

error_to_response()

Converts an error to a response object. Based on \WP_REST_Server.

protected error_to_response(WP_Error $error) : WP_REST_Response
Parameters
$error : WP_Error

WP_Error instance.

Return values
WP_REST_ResponseList of associative arrays with code and message keys.

get_cart_token()

Generates a cart token for the response headers.

protected get_cart_token() : string

Current namespace is used as the token Issuer. *

Return values
string

get_context_param()

Retrieves the context param.

protected get_context_param([array<string|int, mixed> $args = array() ]) : array<string|int, mixed>

Ensures consistent descriptions between endpoints, and populates enum from schema.

Parameters
$args : array<string|int, mixed> = array()

Optional. Additional arguments for context parameter. Default empty array.

Return values
array<string|int, mixed>Context parameter details.

get_response_by_request_method()

Get the route response based on the type of request.

protected get_response_by_request_method(WP_REST_Request $request) : WP_REST_Response
Parameters
$request : WP_REST_Request

Request object.

Return values
WP_REST_Response

get_route_delete_response()

Get route response for DELETE requests.

protected get_route_delete_response(WP_REST_Request $request) : WP_REST_Response

When implemented, should return a \WP_REST_Response.

Parameters
$request : WP_REST_Request

Request object.

Tags
throws
RouteException

On error.

Return values
WP_REST_Response

get_route_error_response()

Get route response when something went wrong.

protected get_route_error_response(string $error_code, string $error_message[, int $http_status_code = 500 ][, array<string|int, mixed> $additional_data = [] ]) : WP_Error
Parameters
$error_code : string

String based error code.

$error_message : string

User facing error message.

$http_status_code : int = 500

HTTP status. Defaults to 500.

$additional_data : array<string|int, mixed> = []

Extra data (key value pairs) to expose in the error response.

Return values
WP_ErrorWP Error object.

get_route_error_response_from_object()

Get route response when something went wrong.

protected get_route_error_response_from_object(WP_Error $error_object[, int $http_status_code = 500 ][, array<string|int, mixed> $additional_data = [] ]) : WP_Error
Parameters
$error_object : WP_Error

User facing error message.

$http_status_code : int = 500

HTTP status. Defaults to 500.

$additional_data : array<string|int, mixed> = []

Extra data (key value pairs) to expose in the error response.

Return values
WP_ErrorWP Error object.

get_route_post_response()

Process an order.

protected get_route_post_response(WP_REST_Request $request) : WP_REST_Response
  1. Obtain Draft Order
  2. Process Request
  3. Process Customer
  4. Validate Order
  5. Process Payment
Parameters
$request : WP_REST_Request

Request object.

Tags
throws
RouteException

On error.

throws
InvalidStockLevelsInCartException

On error.

Return values
WP_REST_Response

get_route_response()

Convert the cart into a new draft order, or update an existing draft order, and return an updated cart response.

protected get_route_response(WP_REST_Request $request) : WP_REST_Response
Parameters
$request : WP_REST_Request

Request object.

Tags
throws
RouteException

On error.

Return values
WP_REST_Response

get_route_update_response()

Get route response for PUT requests.

protected get_route_update_response(WP_REST_Request $request) : WP_REST_Response

When implemented, should return a \WP_REST_Response.

Parameters
$request : WP_REST_Request

Request object.

Tags
throws
RouteException

On error.

Return values
WP_REST_Response

is_update_request()

Are we updating data or getting data?

protected is_update_request(WP_REST_Request $request) : bool
Parameters
$request : WP_REST_Request

Request object.

Return values
bool

is_valid_draft_order()

Whether the passed argument is a draft order or an order that is pending/failed and the cart hasn't changed.

protected is_valid_draft_order(WC_Order $order_object) : bool
Parameters
$order_object : WC_Order

Order object to check.

Return values
boolWhether the order is valid as a draft order.

load_cart_session()

Load the cart session before handling responses.

protected load_cart_session(WP_REST_Request $request) : mixed
Parameters
$request : WP_REST_Request

Request object.

Return values
mixed

Prepare links for the request.

protected prepare_links(mixed $item, WP_REST_Request $request) : array<string|int, mixed>
Parameters
$item : mixed

Item to prepare.

$request : WP_REST_Request

Request object.

Return values
array<string|int, mixed>

prepare_response_for_collection()

Prepares a response for insertion into a collection.

protected prepare_response_for_collection(WP_REST_Response $response) : array<string|int, mixed>|mixed
Parameters
$response : WP_REST_Response

Response object.

Return values
array<string|int, mixed>|mixedResponse data, ready for insertion into collection data.

requires_nonce()

Checks if a nonce is required for the route.

protected requires_nonce(WP_REST_Request $request) : bool
Parameters
$request : WP_REST_Request

Request.

Return values
bool

set_draft_order_id()

Updates draft order data in the customer session.

protected set_draft_order_id(int $order_id) : mixed
Parameters
$order_id : int

Draft order ID.

Return values
mixed

add_data_to_error_object()

Adds additional data to the \WP_Error object.

private add_data_to_error_object(WP_Error $error, array<string|int, mixed> $data, int $http_status_code[, bool $include_cart = false ]) : mixed
Parameters
$error : WP_Error

The error object to add the cart to.

$data : array<string|int, mixed>

The data to add to the error object.

$http_status_code : int

The HTTP status code this error should return.

$include_cart : bool = false

Whether the cart should be included in the error data.

Tags
returns

\WP_Error The \WP_Error with the cart added.

Return values
mixed

create_customer_account()

Create a new account for a customer.

private create_customer_account(string $user_email, string $first_name, string $last_name) : int

The account is created with a generated username. The customer is sent an email notifying them about the account and containing a link to set their (initial) password.

Intended as a replacement for wc_create_new_customer in WC core.

Parameters
$user_email : string

The email address to use for the new account.

$first_name : string

The first name to use for the new account.

$last_name : string

The last name to use for the new account.

Tags
throws
Exception

If an error is encountered when creating the user account.

Return values
intUser id if successful

create_or_update_draft_order()

Create or update a draft order based on the cart.

private create_or_update_draft_order(WP_REST_Request $request) : mixed
Parameters
$request : WP_REST_Request

Full details about the request.

Tags
throws
RouteException

On error.

Return values
mixed

get_request_payment_data()

Gets and formats payment request data.

private get_request_payment_data(WP_REST_Request $request) : array<string|int, mixed>
Parameters
$request : WP_REST_Request

Request object.

Return values
array<string|int, mixed>

get_request_payment_method_id()

Gets the chosen payment method ID from the request.

private get_request_payment_method_id(WP_REST_Request $request) : string
Parameters
$request : WP_REST_Request

Request object.

Tags
throws
RouteException

On error.

Return values
string

get_request_payment_method_title()

Gets the chosen payment method title from the request.

private get_request_payment_method_title(WP_REST_Request $request) : string
Parameters
$request : WP_REST_Request

Request object.

Tags
throws
RouteException

On error.

Return values
string

map_create_account_error()

Convert an account creation error to an exception.

private map_create_account_error(WP_Error $error) : Exception
Parameters
$error : WP_Error

An error object.

Return values
Exception

persist_additional_fields_for_order()

Persist additional fields for the order after validating them.

private persist_additional_fields_for_order(WP_REST_Request $request) : mixed
Parameters
$request : WP_REST_Request

Full details about the request.

Tags
throws
RouteException

On error.

Return values
mixed

process_customer()

Order processing relating to customer account.

private process_customer(WP_REST_Request $request) : mixed

Creates a customer account as needed (based on request & store settings) and updates the order with the new customer ID. Updates the order with user details (e.g. address).

Parameters
$request : WP_REST_Request

Request object.

Tags
throws
RouteException

API error object with error details.

Return values
mixed

process_payment()

Fires an action hook instructing active payment gateways to process the payment for an order and provide a result.

private process_payment(WP_REST_Request $request, PaymentResult $payment_result) : mixed
Parameters
$request : WP_REST_Request

Request object.

$payment_result : PaymentResult

Payment result object.

Tags
throws
RouteException

On error.

Return values
mixed

process_without_payment()

For orders which do not require payment, just update status.

private process_without_payment(WP_REST_Request $request, PaymentResult $payment_result) : mixed
Parameters
$request : WP_REST_Request

Request object.

$payment_result : PaymentResult

Payment result object.

Return values
mixed

should_create_customer_account()

Check request options and store (shop) config to determine if a user account should be created as part of order processing.

private should_create_customer_account(WP_REST_Request $request) : bool
Parameters
$request : WP_REST_Request

The current request object being handled.

Return values
boolTrue if a new user account should be created.

update_customer_from_request()

Updates the current customer session using data from the request (e.g. address data).

private update_customer_from_request(WP_REST_Request $request) : mixed

Address session data is synced to the order itself later on by OrderController::update_order_from_cart()

Parameters
$request : WP_REST_Request

Full details about the request.

Return values
mixed

update_order_from_request()

Update the current order using the posted values from the request.

private update_order_from_request(WP_REST_Request $request) : mixed
Parameters
$request : WP_REST_Request

Full details about the request.

Return values
mixed