WooCommerce Code Reference

CartUpdateItem extends AbstractCartRoute
in package

CartUpdateItem class.

Table of Contents

IDENTIFIER  = 'cart-update-item'
The route identifier.
SCHEMA_TYPE  = ''
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.
__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 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 and the supplied error is a WP_Error. This currently only happens when an item in the cart is out of stock, partially out of stock, can only be bought individually, or when the item is not purchasable.
get_route_post_response()  : WP_REST_Response
Handle the request and return a valid response for this endpoint.
get_route_response()  : WP_REST_Response
Get route response for GET requests.
get_route_update_response()  : WP_REST_Response
Get route response for PUT 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.
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.
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.
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.
without_default_state()  : array<string|int, mixed>
Returns a location without the state the customer session fills in by default.

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 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
since
11.2.2
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_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_Error — WP Error object.

get_route_error_response_from_object()

Get route response when something went wrong and the supplied error is a WP_Error. This currently only happens when an item in the cart is out of stock, partially out of stock, can only be bought individually, or when the item is not purchasable.

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

The WP_Error object containing the error.

$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()

Handle the request and return a valid response for this endpoint.

protected get_route_post_response(WP_REST_Request $request) : WP_REST_Response

.

Parameters
$request : WP_REST_Request

Request object.

Return values
WP_REST_Response —

get_route_response()

Get route response for GET requests.

protected get_route_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_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 —

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
throws
RuntimeException

When a previous cart session load failed.

throws
Throwable

When the cart cannot be loaded or normalized.

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
since
11.3.1
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
since
11.3.1
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 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 —

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 —

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 —

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 —

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.

Return values
array<string|int, mixed> —