CheckoutTrait
CheckoutTrait
Shared functionality for checkout route.
Table of Contents
- get_document_object_from_rest_request() : DocumentObject
- Returns a document object from a REST request.
- prepare_item_for_response() : WP_REST_Response
- Prepare a single item for response. Handles setting the status based on the payment result.
- 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_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.
- 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_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.
- 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).
- update_order_from_request() : mixed
- Update the current order using the posted values from the request.
- 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.
Methods
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.prepare_item_for_response()
Prepare a single item for response. Handles setting the status based on the payment result.
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.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_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 —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_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 —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 —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 —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
