WooCommerce Code Reference

AbstractController extends WP_REST_Controller
in package

Abstract REST Controller for WooCommerce REST API V4.

Provides common functionality for all V4 route controllers including schema generation, error handling, and hook management.

Tags
since
10.2.0

Table of Contents

CANNOT_CREATE  = 'cannot_create'
CANNOT_DELETE  = 'cannot_delete'
CANNOT_TRASH  = 'cannot_trash'
INVALID_ID  = 'invalid_id'
Shared error codes.
RESOURCE_EXISTS  = 'resource_exists'
TRASH_NOT_SUPPORTED  = 'trash_not_supported'
$namespace  : string
Route namespace.
$rest_base  : string
Route base.
$schema  : array<string|int, mixed>
Cache for the item schema populated after calling get_item_schema().
get_collection_params()  : array<string|int, mixed>
Add default context collection params and filter the result. This does not inherit from WP_REST_Controller::get_collection_params because some endpoints do not paginate results.
get_item_schema()  : array<string|int, mixed>
Get item schema, conforming to JSON Schema. Extended by routes.
prepare_item_for_response()  : WP_REST_Response|WP_Error
Prepares the item for the REST response. Controllers do not need to override this method as they can define a get_item_response method to prepare items. This method will take care of filter hooks.
get_authentication_error_by_method()  : WP_Error|false
Returns an authentication error for a given HTTP verb.
get_error_prefix()  : string
Get the error prefix for errors.
get_hook_prefix()  : string
Get the hook prefix for actions and filters.
get_item_response()  : array<string|int, mixed>
Get the item response.
get_query_schema()  : array<string|int, mixed>
Get the collection args schema.
get_route_error_by_code()  : WP_Error
Get an error response for a given error code.
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.
get_schema()  : array<string|int, mixed>
Get the schema for the current resource. This use consumed by the AbstractController to generate the item schema after running various hooks on the response.
prepare_links()  : array<string|int, mixed>
Prepare links for the request.

Constants

Properties

Methods

get_collection_params()

Add default context collection params and filter the result. This does not inherit from WP_REST_Controller::get_collection_params because some endpoints do not paginate results.

public get_collection_params() : array<string|int, mixed>
Return values
array<string|int, mixed>

get_item_schema()

Get item schema, conforming to JSON Schema. Extended by routes.

public get_item_schema() : array<string|int, mixed>
Tags
since
10.2.0
Return values
array<string|int, mixed>The item schema.

prepare_item_for_response()

Prepares the item for the REST response. Controllers do not need to override this method as they can define a get_item_response method to prepare items. This method will take care of filter hooks.

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

WordPress representation of the item.

$request : WP_REST_Request

Request object.

Tags
since
10.2.0
Return values
WP_REST_Response|WP_ErrorResponse object on success, or WP_Error object on failure.

get_authentication_error_by_method()

Returns an authentication error for a given HTTP verb.

protected get_authentication_error_by_method(string $method) : WP_Error|false
Parameters
$method : string

HTTP method.

Return values
WP_Error|falseWP Error object or false if no error is found.

get_error_prefix()

Get the error prefix for errors.

protected get_error_prefix() : string

Example: woocommerce_rest_api_v4_orders_

Tags
since
10.2.0
Return values
stringThe error prefix.

get_hook_prefix()

Get the hook prefix for actions and filters.

protected get_hook_prefix() : string

Example: woocommerce_rest_api_v4_orders_

Tags
since
10.2.0
Return values
stringThe hook prefix.

get_item_response()

Get the item response.

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

WooCommerce representation of the item.

$request : WP_REST_Request

Request object.

Tags
since
10.2.0
Return values
array<string|int, mixed>The item response.

get_route_error_by_code()

Get an error response for a given error code.

protected get_route_error_by_code(string $error_code) : WP_Error
Parameters
$error_code : string

The error code.

Return values
WP_ErrorWP Error object.

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 = WP_Http::BAD_REQUEST ][, array<string|int, mixed> $additional_data = array() ]) : WP_Error
Parameters
$error_code : string

String based error code.

$error_message : string

User facing error message.

$http_status_code : int = WP_Http::BAD_REQUEST

HTTP status. Defaults to 400.

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

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

Tags
since
10.2.0
Return values
WP_ErrorWP Error object.

get_route_error_response_from_object()

Get route response when something went wrong and the supplied error is a WP_Error.

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

The WP_Error object containing the error.

$http_status_code : int = WP_Http::BAD_REQUEST

HTTP status. Defaults to 400.

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

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

Tags
since
10.2.0
Return values
WP_ErrorWP Error object.

get_schema()

Get the schema for the current resource. This use consumed by the AbstractController to generate the item schema after running various hooks on the response.

protected abstract get_schema() : array<string|int, mixed>

This should return the full schema object, not just the properties.

Return values
array<string|int, mixed>The full item schema.

Prepare links for the request.

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

WordPress representation of the item.

$request : WP_REST_Request

Request object.

$response : WP_REST_Response

Response object.

Return values
array<string|int, mixed>