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.
- $has_cart_token : bool|null
- True when this route has been requested with a valid cart token.
- $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|null
- Holds the current order being processed. Null until `create_or_update_draft_order()` materialises it (either by reusing the session's pending/failed order or by creating a new one from the cart).
- __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_document_object_from_rest_request() : DocumentObject
- Returns a document object from a REST request.
- 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.
- validate_callback() : true|WP_Error
- Validation callback for the checkout route.
- add_response_headers() : WP_REST_Response
- Add nonce headers to a response object.
- cart_updated() : mixed
- Triggered after a successful update to cart data.
- 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.
- forget_draft_order_if_not_saved() : void
- Removes the order from the session when it is the draft order and its last save failed.
- get_cart_session_error_response() : WP_REST_Response
- Convert a failure during cart session loading into a client-safe error response.
- get_cart_token() : string
- Generates a cart token for the response headers.
- 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|WP_Error
- Process an order.
- get_route_response() : WP_REST_Response
- Return a checkout response for GET requests.
- get_route_update_response() : WP_REST_Response|WP_Error
- Get route response for PUT/PATCH requests.
- has_cart_token() : bool
- Checks if the request has a valid cart token.
- is_update_request() : bool
- Are we updating data or getting data?
- is_valid_draft_order() : bool
- Whether the argument can serve as the session's draft order: a checkout-draft, or a pending/failed order the shopper can still pay as placed (its cart hash matches, the cart keeps its counted coupons, a priced order that needs shipping still ships to the customer's destination, and its tax location has not moved).
- load_cart_session() : mixed
- Load the cart session before handling responses.
- order_ships_to_customer_destination() : bool
- Whether the order's shipping lines are still rated for the session customer's destination, which the cart hash does not cover. Compared through the pay-for-order guard's helper, reading a state the session filled in by default as empty; the taxable location is checked by order_taxed_at_customer_location().
- order_taxed_at_customer_location() : bool
- Whether the order would still be taxed for the same location once it takes the session customer's addresses, which the cart hash does not cover either. The sync re-prices tax from the order's own addresses, so the old tax is never charged, but the new address skips the checks checkout makes (allowed countries, state codes).
- 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.
- build_draft_route_response() : WP_REST_Response
- Build a checkout response for a session with no order in flight.
- cart_keeps_counted_coupons() : bool
- Whether a sync from the cart would keep the coupons the order counted. The cart hash does not cover coupons, and a sync that swapped or dropped a counted coupon would leave it counted with no order carrying it. An order that counted nothing, e.g. a failed one, may follow the cart. When the cart holds a coupon an extension supplies without a stored record, such as the one WooCommerce Subscriptions puts in place of a parent order's coupons, the order may follow the cart as long as the cart adds no stored coupon the order lacks. Compares formatted codes, not `_coupons_hash`, which only the Store API sync writes. The flag is read through the data store, because the order object may be one held since before its usage was counted.
- create_or_update_draft_order() : mixed
- Create or update a draft order based on the cart.
- get_full_billing_address() : array<string|int, mixed>
- Gets the customer's billing address with the request's billing address applied on top.
- get_order_or_throw() : WC_Order
- Returns the order being processed, throwing if it hasn't been materialised yet.
- 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.
- get_validated_address() : array<string|int, mixed>
- Gets the country, state and postcode the address will end up with, then sanitizes and validates them.
- is_stored_coupon_code() : bool
- Whether a coupon with this code is stored: one wc_get_coupon_id_by_code() finds, or a coupon post with the code in any status. wc_get_coupon_id_by_code() only finds published coupons, so a coupon trashed after the order was placed would otherwise pass for an extension's code.
- locations_match() : bool
- Whether two locations match, reading a state the customer session filled in by default as empty.
- lock_order_or_throw() : void
- Give this request exclusive use of an order, or confirm that it still has it, and refuse the request otherwise.
- order_awaits_first_address() : bool
- Whether the order has no address yet and was not created through the Store API, e.g. one a merchant made for the shopper to fill in. It is taxed at the shop's base address and has no shipping zone yet, so, as in OrderController::validate_location_change(), its first address is allowed.
- order_moved_past_payment() : bool
- Whether the order has moved beyond the point of awaiting payment.
- persist_additional_fields_for_customer() : void
- Persist additional fields for the customer session.
- 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_order() : WP_REST_Response|WP_Error
- Process an order based on optimistic save approach to minimize the number of order saves.
- 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.
- recover_order_that_took_payment() : void
- Records a failure that happened after payment was taken and reports the checkout as successful, so the shopper is sent to the order confirmation rather than back to retry.
- refresh_order() : WC_Order
- Re-reads the order so its status reflects whatever the gateway persisted, falling back to the in-memory order when it can no longer be read.
- reset_request_state() : void
- Clear the order held from a previous request. One route instance can serve several requests in the same PHP process, so each request must resolve its own order.
- resolve_and_persist_additional_fields() : void
- Resolve the additional checkout fields from the request and persist each one via the supplied callback. Fields hidden by conditional logic that were still posted are cleared (passed with an empty value).
- 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.
- throw_if_persisted_session_changed() : void
- Refuse to use a session that another request changed after this request loaded it.
- update_customer_address_field() : mixed
- Updates a customer address field.
- 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.
- update_session_from_request() : void
- Persist the PATCH request's payment method and additional fields to the customer session. Counterpart to `update_order_from_request` for the no-order PATCH path.
- validate_order_totals() : void
- Reject the order if what the customer is going to be charged at is higher from what they saw.
- validate_order_was_saved() : void
- Stops the request when the order could not be saved, removing a draft order from the session so a later request does not resume it.
- validate_user_can_place_order() : mixed
- This validates if the order can be placed regarding settings in WooCommerce > Settings > Accounts & Privacy If registration during checkout is disabled, guest checkout is disabled and the user is not logged in, prevent checkout.
- without_default_state() : array<string|int, mixed>
- Returns a location without the state the customer session fills in by default.
Constants
IDENTIFIER
The route identifier.
public
string
IDENTIFIER
= 'checkout'
SCHEMA_TYPE
The routes schema.
public
string
SCHEMA_TYPE
= 'checkout'
SCHEMA_VERSION
The routes schema version.
public
int
SCHEMA_VERSION
= 1
Properties
$additional_fields_controller
Additional fields controller class instance.
protected
CheckoutFields
$additional_fields_controller
$cart_controller
Cart controller class instance.
protected
CartController
$cart_controller
$cart_item_schema
Schema class for the cart item.
protected
CartItemSchema
$cart_item_schema
$cart_schema
Schema class for the cart.
protected
CartSchema
$cart_schema
$has_cart_token
True when this route has been requested with a valid cart token.
protected
bool|null
$has_cart_token
= null
$namespace
Route namespace.
protected
string
$namespace
= 'wc/store/v1'
$order_controller
Order controller class instance.
protected
OrderController
$order_controller
$schema
Schema class instance.
protected
AbstractSchema
$schema
$schema_controller
Schema Controller instance.
protected
SchemaController
$schema_controller
$order
Holds the current order being processed. Null until `create_or_update_draft_order()` materialises it (either by reusing the session's pending/failed order or by creating a new one from the cart).
private
WC_Order|null
$order
= null
Methods
__construct()
Constructor.
public
__construct(SchemaController $schema_controller, AbstractSchema $schema) : mixed
Parameters
- $schema_controller : SchemaController
-
Schema Controller instance.
- $schema : AbstractSchema
-
Schema class for this route.
Return values
mixed —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_document_object_from_rest_request()
Returns a document object from a REST request.
public
get_document_object_from_rest_request(WP_REST_Request $request) : DocumentObject
Parameters
- $request : WP_REST_Request
-
The REST request.
Return values
DocumentObject — The document object or null if experimental blocks are not enabled.get_item_schema()
Get item schema properties.
public
get_item_schema() : array<string|int, mixed>
Return values
array<string|int, mixed> —get_namespace()
Get the namespace for this route.
public
get_namespace() : string
Return values
string —get_path()
Get the path of this REST route.
public
get_path() : string
Return values
string —get_path_regex()
Get the path of this rest route.
public
static get_path_regex() : string
Return values
string —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.
Tags
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 —validate_callback()
Validation callback for the checkout route.
public
validate_callback(WP_REST_Request $request) : true|WP_Error
This runs after individual field validation_callbacks have been called.
Parameters
- $request : WP_REST_Request
-
Request object.
Return values
true|WP_Error —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 a successful update to cart data.
protected
cart_updated(WP_REST_Request $request) : mixed
Cart routes no longer synchronize draft orders. The method remains so route subclasses can react to updates. Checkout synchronizes the authoritative cart state immediately before placing an order.
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_Response — List of associative arrays with code and message keys.forget_draft_order_if_not_saved()
Removes the order from the session when it is the draft order and its last save failed.
protected
forget_draft_order_if_not_saved(WC_Order $order) : void
A failed save can leave the order partly written, and resuming it would reuse that state.
Parameters
- $order : WC_Order
-
Order object.
Tags
Return values
void —get_cart_session_error_response()
Convert a failure during cart session loading into a client-safe error response.
protected
get_cart_session_error_response(Throwable $error) : WP_REST_Response
Error details are returned only to managers when debug details are enabled.
Parameters
- $error : Throwable
-
The error that occurred while loading the cart session.
Return values
WP_REST_Response — The error response to return to the client.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_draft_order()
Uses the draft order ID to return an order object, if valid.
protected
get_draft_order() : WC_Order|null
Return values
WC_Order|null —get_draft_order_id()
Gets draft order data from the customer session.
protected
get_draft_order_id() : int
Return values
int —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
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_Error — WP 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_Error — WP Error object.get_route_post_response()
Process an order.
protected
get_route_post_response(WP_REST_Request<string|int, array<string, mixed>> $request) : WP_REST_Response|WP_Error
Parameters
- $request : WP_REST_Request<string|int, array<string, mixed>>
-
Request object.
Tags
Return values
WP_REST_Response|WP_Error —get_route_response()
Return a checkout response for GET requests.
protected
get_route_response(WP_REST_Request $request) : WP_REST_Response
If a pending/failed order from a previous payment attempt is in the customer
session, reuse it (the failed-payment retry path). Otherwise build a no-order
response directly from cart + customer + request.
Parameters
- $request : WP_REST_Request
-
Request object.
Tags
Return values
WP_REST_Response —get_route_update_response()
Get route response for PUT/PATCH requests.
protected
get_route_update_response(WP_REST_Request $request) : WP_REST_Response|WP_Error
Branches on whether a pending/failed order already exists in the customer's session:
- Order in session (failed-payment retry): update the existing order via
create_or_update_draft_order()+update_order_from_request(). Same shape as the POST flow. - No order in session (fresh checkout form interaction): persist request
state to the customer session via
update_session_from_request()and return a no-order response built from cart + customer + request.
Draft order creation is deferred to POST (place-order time) to avoid
orphaned wc-checkout-draft rows from form interactions that never
complete. POSTs do not flow through this method — see
get_route_post_response().
Parameters
- $request : WP_REST_Request
-
Request object.
Tags
Return values
WP_REST_Response|WP_Error —has_cart_token()
Checks if the request has a valid cart token.
protected
has_cart_token(WP_REST_Request $request) : bool
Reads the outer HTTP header, not $request one, to avoid conflicting cart tokens on a batch request.
Parameters
- $request : WP_REST_Request
-
Request object. Unused here; kept for subclasses.
Return values
bool —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 argument can serve as the session's draft order: a checkout-draft, or a pending/failed order the shopper can still pay as placed (its cart hash matches, the cart keeps its counted coupons, a priced order that needs shipping still ships to the customer's destination, and its tax location has not moved).
protected
is_valid_draft_order(WC_Order $order_object) : bool
Parameters
- $order_object : WC_Order
-
Order object to check.
Return values
bool — Whether 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.
Tags
Return values
mixed —order_ships_to_customer_destination()
Whether the order's shipping lines are still rated for the session customer's destination, which the cart hash does not cover. Compared through the pay-for-order guard's helper, reading a state the session filled in by default as empty; the taxable location is checked by order_taxed_at_customer_location().
protected
order_ships_to_customer_destination(WC_Order $order_object) : bool
Parameters
- $order_object : WC_Order
-
Order object to check.
Tags
Return values
bool — True when the destinations match or the cart needs no shipping; false without a customer, or when only one of the order and the customer has a shipping country.order_taxed_at_customer_location()
Whether the order would still be taxed for the same location once it takes the session customer's addresses, which the cart hash does not cover either. The sync re-prices tax from the order's own addresses, so the old tax is never charged, but the new address skips the checks checkout makes (allowed countries, state codes).
protected
order_taxed_at_customer_location(WC_Order $order_object) : bool
Refusing reuse sends the change through checkout instead. Same before-and-after comparison as the pay-for-order guard, OrderController::validate_location_change(), except that a state the session filled in by default is read as empty; its exemption for an order awaiting its first address is applied by is_valid_draft_order().
Parameters
- $order_object : WC_Order
-
Order object to check.
Tags
Return values
bool — True when the tax location is unchanged or the store collects no tax; false without a customer, or when either filtered tax location is not an array.prepare_links()
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>|mixed — Response 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
Return values
mixed —build_draft_route_response()
Build a checkout response for a session with no order in flight.
private
build_draft_route_response(WP_REST_Request $request) : WP_REST_Response
Parameters
- $request : WP_REST_Request
-
Request object.
Tags
Return values
WP_REST_Response —cart_keeps_counted_coupons()
Whether a sync from the cart would keep the coupons the order counted. The cart hash does not cover coupons, and a sync that swapped or dropped a counted coupon would leave it counted with no order carrying it. An order that counted nothing, e.g. a failed one, may follow the cart. When the cart holds a coupon an extension supplies without a stored record, such as the one WooCommerce Subscriptions puts in place of a parent order's coupons, the order may follow the cart as long as the cart adds no stored coupon the order lacks. Compares formatted codes, not `_coupons_hash`, which only the Store API sync writes. The flag is read through the data store, because the order object may be one held since before its usage was counted.
private
cart_keeps_counted_coupons(WC_Order $order_object) : bool
Parameters
- $order_object : WC_Order
-
Order object to check.
Return values
bool —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
Return values
mixed —get_full_billing_address()
Gets the customer's billing address with the request's billing address applied on top.
private
get_full_billing_address(WC_Customer $customer, WP_REST_Request $request) : array<string|int, mixed>
Used as the shipping address when the order ships to billing, so no shipping value left in the session reaches the order.
Parameters
- $customer : WC_Customer
-
Customer object.
- $request : WP_REST_Request
-
Full details about the request.
Tags
Return values
array<string|int, mixed> —get_order_or_throw()
Returns the order being processed, throwing if it hasn't been materialised yet.
private
get_order_or_throw() : WC_Order
Use the returned WC_Order (rather than $this->order) for type-safe access in
the rest of the calling method.
Tags
Return values
WC_Order —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()
Gets the chosen payment method from the request.
private
get_request_payment_method(WP_REST_Request $request) : WC_Payment_Gateway|null
Parameters
- $request : WP_REST_Request
-
Request object.
Tags
Return values
WC_Payment_Gateway|null —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
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
Return values
string —get_validated_address()
Gets the country, state and postcode the address will end up with, then sanitizes and validates them.
private
get_validated_address(WP_REST_Request $request, string $param, array<string|int, mixed> $posted, array<string|int, mixed> $stored) : array<string|int, mixed>
A partial address can leave out any of the three, so the ones it leaves out come from the session and all of them are checked together, against the final country. Other fields keep the request's own validation.
Parameters
- $request : WP_REST_Request
-
Full details about the request.
- $param : string
-
Request parameter the address came from: billing_address or shipping_address.
- $posted : array<string|int, mixed>
-
Address fields from the request.
- $stored : array<string|int, mixed>
-
Address the session customer holds.
Tags
Return values
array<string|int, mixed> — The country, state and postcode, sanitized.is_stored_coupon_code()
Whether a coupon with this code is stored: one wc_get_coupon_id_by_code() finds, or a coupon post with the code in any status. wc_get_coupon_id_by_code() only finds published coupons, so a coupon trashed after the order was placed would otherwise pass for an extension's code.
private
is_stored_coupon_code(string $code) : bool
Parameters
- $code : string
-
Coupon code as applied to the cart.
Return values
bool —locations_match()
Whether two locations match, reading a state the customer session filled in by default as empty.
private
locations_match(array<string|int, mixed> $order_location, array<string|int, mixed> $customer_location) : bool
Parameters
- $order_location : array<string|int, mixed>
-
Location the order was priced for.
- $customer_location : array<string|int, mixed>
-
Location the session customer would give it.
Return values
bool —lock_order_or_throw()
Give this request exclusive use of an order, or confirm that it still has it, and refuse the request otherwise.
private
lock_order_or_throw(int $order_id) : void
Checkout and other order writers must hold the same lock so an order cannot change between validation and placement.
Parameters
- $order_id : int
-
Order ID.
Tags
Return values
void —order_awaits_first_address()
Whether the order has no address yet and was not created through the Store API, e.g. one a merchant made for the shopper to fill in. It is taxed at the shop's base address and has no shipping zone yet, so, as in OrderController::validate_location_change(), its first address is allowed.
private
order_awaits_first_address(WC_Order $order_object) : bool
Parameters
- $order_object : WC_Order
-
Order object to check.
Return values
bool —order_moved_past_payment()
Whether the order has moved beyond the point of awaiting payment.
private
order_moved_past_payment(WC_Order $order) : bool
An order reaches a gateway awaiting payment or as a draft, so any other status means something moved it on. This is about the status, not the money: an order parked on-hold for review counts, because sending the shopper back to place it again would be wrong.
The statuses a site declares payable count as awaiting payment, so a custom status an extension parks the order in before the gateway runs is not mistaken for a paid one. Not needs_payment(): that folds in the order total, and a fully discounted order that failed would then keep its stock and coupon holds.
Parameters
- $order : WC_Order
-
Order object.
Return values
bool —persist_additional_fields_for_customer()
Persist additional fields for the customer session.
private
persist_additional_fields_for_customer(WP_REST_Request $request) : void
Counterpart to persist_additional_fields_for_order for routes that operate
without a persisted order (e.g. the deferred-draft PATCH path).
Parameters
- $request : WP_REST_Request
-
Full details about the request.
Tags
Return values
void —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
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
Return values
mixed —process_order()
Process an order based on optimistic save approach to minimize the number of order saves.
private
process_order(WP_REST_Request<string|int, array<string, mixed>> $request) : WP_REST_Response|WP_Error
- Obtain Draft Order
- Process Request
- Process Customer
- Validate Order
- Process Payment
Parameters
- $request : WP_REST_Request<string|int, array<string, mixed>>
-
Request object.
Tags
Return values
WP_REST_Response|WP_Error —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
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
Deliberately no recovery of the kind process_payment() does: nothing was charged, so a failure costs the shopper only a retry, and claiming success would be the worse outcome.
Parameters
- $request : WP_REST_Request
-
Request object.
- $payment_result : PaymentResult
-
Payment result object.
Tags
Return values
mixed —recover_order_that_took_payment()
Records a failure that happened after payment was taken and reports the checkout as successful, so the shopper is sent to the order confirmation rather than back to retry.
private
recover_order_that_took_payment(WC_Order $order, Throwable $error, PaymentResult $payment_result) : void
Parameters
- $order : WC_Order
-
Order object.
- $error : Throwable
-
The failure raised after payment was taken.
- $payment_result : PaymentResult
-
Payment result object.
Return values
void —refresh_order()
Re-reads the order so its status reflects whatever the gateway persisted, falling back to the in-memory order when it can no longer be read.
private
refresh_order(WC_Order $order) : WC_Order
Parameters
- $order : WC_Order
-
Order object.
Return values
WC_Order —reset_request_state()
Clear the order held from a previous request. One route instance can serve several requests in the same PHP process, so each request must resolve its own order.
private
reset_request_state() : void
Return values
void —resolve_and_persist_additional_fields()
Resolve the additional checkout fields from the request and persist each one via the supplied callback. Fields hidden by conditional logic that were still posted are cleared (passed with an empty value).
private
resolve_and_persist_additional_fields(WP_REST_Request $request, callable $persist) : void
Parameters
- $request : WP_REST_Request
-
Full details about the request.
- $persist : callable
-
Callback invoked as
$persist( string $key, mixed $value )for each field.
Tags
Return values
void —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
bool — True if a new user account should be created.throw_if_persisted_session_changed()
Refuse to use a session that another request changed after this request loaded it.
private
throw_if_persisted_session_changed() : void
Mutations made by this request are allowed because the session handler compares the latest persisted data with the persisted data that initialized this request, not with its current in-memory data.
Tags
Return values
void —update_customer_address_field()
Updates a customer address field.
private
update_customer_address_field(WC_Customer $customer, string $key, mixed $value, string $address_type) : mixed
Parameters
- $customer : WC_Customer
-
The customer to update.
- $key : string
-
The key of the field to update.
- $value : mixed
-
The value to update the field to.
- $address_type : string
-
The type of address to update (billing|shipping).
Return values
mixed —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[, bool $persist = true ]) : mixed
Called only with a real, persisted order — either the place-order POST flow or
the rare failed-payment PATCH retry flow where get_draft_order() resolved to
an existing pending/failed order from the customer's session. Fresh-session
PATCHes never call this method; they go through the no-order draft path.
Parameters
- $request : WP_REST_Request
-
Full details about the request.
- $persist : bool = true
-
Whether to persist the changes right away (defaults to true).
Tags
Return values
mixed —update_session_from_request()
Persist the PATCH request's payment method and additional fields to the customer session. Counterpart to `update_order_from_request` for the no-order PATCH path.
private
update_session_from_request(WP_REST_Request $request) : void
Parameters
- $request : WP_REST_Request
-
Request object.
Tags
Return values
void —validate_order_totals()
Reject the order if what the customer is going to be charged at is higher from what they saw.
private
validate_order_totals(WP_REST_Request $request) : void
Runs on POST /checkout only, before the draft order is materialised, so a mismatch leaves no order behind. The check only runs when the client sends the total it displayed; flows that cannot know the final total up front (e.g. some express payment methods) omit it and are unaffected.
Parameters
- $request : WP_REST_Request
-
Request object.
Tags
Return values
void —validate_order_was_saved()
Stops the request when the order could not be saved, removing a draft order from the session so a later request does not resume it.
private
validate_order_was_saved() : void
Tags
Return values
void —validate_user_can_place_order()
This validates if the order can be placed regarding settings in WooCommerce > Settings > Accounts & Privacy If registration during checkout is disabled, guest checkout is disabled and the user is not logged in, prevent checkout.
private
validate_user_can_place_order() : mixed
Tags
Return values
mixed —without_default_state()
Returns a location without the state the customer session fills in by default.
private
without_default_state(array<string|int, mixed> $location) : array<string|int, mixed>
The session gives a customer with no billing state the default customer location's state, whatever their country (WC_Customer_Data_Store_Session::set_defaults()), and saves it. A shopper who left an optional state empty has it on later requests while the order they placed has none. On another country, where it is not one of that country's states, such a state is read as empty so the same address still compares as equal.
Parameters
- $location : array<string|int, mixed>
-
Location with country and state keys.
