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.
- 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.
- 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.
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 —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
Parameters
- $request : WP_REST_Request
-
Request object.
- $payment_result : PaymentResult
-
Payment result object.
Tags
Return values
mixed —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).
