WooCommerce Code Reference

WC_REST_Refunds_Controller extends WC_REST_Order_Refunds_Controller
in package

REST API Order Refunds controller class.

Tags
extends

WC_REST_Order_Refunds_Controller

Table of Contents

$hierarchical  : bool
If object is hierarchical.
$namespace  : string
Endpoint namespace.
$post_type  : string
Post type.
$public  : string
Controls visibility on frontend.
$request  : array<string|int, mixed>
Stores the request.
$rest_base  : string
Route base.
$_fields  : null|array<string|int, mixed>
Used to cache computed return fields.
$_request  : null|WP_REST_Request
Used to verify if cached fields are for correct request object.
__construct()  : mixed
Order refunds actions.
batch_items()  : array<string|int, mixed>
Bulk create, update and delete items.
batch_items_permissions_check()  : bool|WP_Error
Check if a given request has access batch create, update and delete items.
create_item()  : WP_Error|WP_REST_Response
Create a single item.
create_item_permissions_check()  : WP_Error|bool
Check if a given request has access to create an item.
delete_item()  : WP_REST_Response|WP_Error
Delete a single item.
delete_item_permissions_check()  : bool|WP_Error
Check if a given request has access to delete an item.
fetch_fields_using_getters()  : array<string|int, mixed>
Get fields for an object if getter is defined.
get_collection_params()  : array<string|int, mixed>
Get the query params for collections of attachments.
get_endpoint_args_for_item_schema()  : array<string|int, mixed>
Compatibility functions for WP 5.5, since custom types are not supported anymore.
get_fields_for_response()  : array<string|int, mixed>
Gets an array of fields to be included on the response.
get_item()  : WP_Error|WP_REST_Response
Get a single item.
get_item_permissions_check()  : WP_Error|bool
Check if a given request has access to read an item.
get_item_schema()  : array<string|int, mixed>
Get the refund schema, conforming to JSON Schema.
get_items()  : WP_Error|WP_REST_Response
Get a collection of posts.
get_items_permissions_check()  : WP_Error|bool
Check if a given request has access to read items.
get_public_batch_schema()  : array<string|int, mixed>
Get the batch schema, conforming to JSON Schema.
get_public_preview_schema()  : array<string|int, mixed>
Get the public schema for the refund preview endpoint.
prepare_object_for_response()  : WP_Error|WP_REST_Response
Prepare a single order output for response.
preview_refund()  : WP_REST_Response|WP_Error
Preview a refund without creating it.
register_routes()  : void
Register the routes for order refunds.
update_item()  : WP_Error|WP_REST_Response
Update a single post.
update_item_permissions_check()  : WP_Error|bool
Check if a given request has access to update an item.
validate_setting_checkbox_field()  : string|WP_Error
Validate checkbox based settings.
validate_setting_image_width_field()  : string|WP_Error
Validate image_width based settings.
validate_setting_multiselect_field()  : array<string|int, mixed>|WP_Error
Validate multiselect based settings.
validate_setting_radio_field()  : string|WP_Error
Validate radio based settings.
validate_setting_select_field()  : string|WP_Error
Validate select based settings.
validate_setting_text_field()  : string
Validate a text value for a text based setting.
validate_setting_textarea_field()  : string
Validate textarea based settings.
add_additional_fields_schema()  : array<string|int, mixed>
Add the schema from additional fields to an schema array.
add_meta_query()  : array<string|int, mixed>
Add meta query.
add_post_meta_fields()  : bool|WP_Error
Add post meta fields.
adjust_wp_5_5_datatype_compatibility()  : mixed
Change datatypes `date-time` to string, and `mixed` to composite of all built in types. This is required for maintaining forward compatibility with WP 5.5 since custom post types are not supported anymore.
check_batch_limit()  : bool|WP_Error
Check batch limit.
delete_post()  : mixed
Delete post.
filter_writable_props()  : bool
Only return writable props from schema.
get_allowed_query_vars()  : array<string|int, mixed>
Get all the WP Query vars that are allowed for the API request.
get_formatted_item_data()  : array<string|int, mixed>
Get formatted item data.
get_meta_data_for_response()  : array<string|int, mixed>
Limit the contents of the meta_data property based on certain request parameters.
get_normalized_rest_base()  : string
Get normalized rest base.
get_object()  : object
Get object.
get_objects()  : array<string|int, mixed>
Get objects.
get_order_item_data()  : array<string|int, mixed>
Expands an order item to get its data.
get_order_statuses()  : array<string|int, mixed>
Get order statuses without prefixes.
get_permalink()  : string
Get object permalink.
get_product_id()  : int
Gets the product ID from the SKU or posted ID.
item_is_null()  : bool
Helper method to check if the resource ID associated with the provided item is null.
maybe_set_item_meta_data()  : mixed
Maybe set item meta if posted.
maybe_set_item_prop()  : mixed
Maybe set an item prop if the value was posted.
maybe_set_item_props()  : mixed
Maybe set item props if the values were posted.
prepare_coupon_lines()  : WC_Order_Item_Coupon
Create or update an order coupon.
prepare_fee_lines()  : WC_Order_Item_Fee
Create or update an order fee.
prepare_items_query()  : array<string|int, mixed>
Determine the allowed query_vars for a get_items() response and prepare for WP_Query.
prepare_line_items()  : WC_Order_Item_Product
Create or update a line item.
prepare_links()  : array<string|int, mixed>
Prepare links for the request.
prepare_object_for_database()  : WP_Error|WC_Data
Prepares one object for create or update operation.
prepare_object_for_response_core()  : array<string|int, mixed>
Core method to prepare a single order object for response (doesn't fire hooks, execute rest_ensure_response, or add links).
prepare_objects_query()  : array<string|int, mixed>
Prepare objects query.
prepare_shipping_lines()  : WC_Order_Item_Shipping
Create or update an order shipping method.
save_object()  : WC_Data|WP_Error
Save an object data.
set_item()  : mixed
Wrapper method to create/update order items.
update_address()  : mixed
Update address.
update_post_meta_fields()  : bool|WP_Error
Update post meta fields.
add_cogs_related_schema()  : array<string|int, mixed>
Add the Cost of Goods Sold related fields to the schema.
add_preview_additional_fields()  : array<string|int, mixed>
Populate fields registered for the preview object type into a response.
create_refund_with_computed_totals()  : WP_Error|WC_Data
Create a refund with server-computed per-line totals (compute_totals mode).
filter_internal_meta_keys()  : array<string|int, mixed>
With HPOS, few internal meta keys such as _billing_address_index, _shipping_address_index are not considered internal anymore (since most internal keys were flattened into dedicated columns).
get_data_utils()  : DataUtils
Get the shared refund calculation engine.
get_preview_fields_for_response()  : array<string|int, string>
Get the preview fields a request asks for, mirroring core's get_fields_for_response() against the preview schema instead of the controller's item schema.
get_preview_line_items_arg_schema()  : array<string|int, mixed>
Get the argument schema for the preview route's line_items parameter.
merge_meta_item_with_formatted_meta_display_attributes()  : array<string|int, mixed>
Merge the `$formatted_meta_data` `display_key` and `display_value` attribute values into the corresponding {@link WC_Meta_Data}. Returns the merged array.
normalize_line_item()  : array<string|int, mixed>|WP_Error
Normalize one compute_totals line item to the shared engine's shape.
prefix_error_code()  : WP_Error
Prefix a shared-engine error code with `woocommerce_rest_`.

Properties

Methods

batch_items()

Bulk create, update and delete items.

public batch_items(WP_REST_Request $request) : array<string|int, mixed>
Parameters
$request : WP_REST_Request

Full details about the request.

Return values
array<string|int, mixed>Of WP_Error or WP_REST_Response.

batch_items_permissions_check()

Check if a given request has access batch create, update and delete items.

public batch_items_permissions_check(WP_REST_Request $request) : bool|WP_Error
Parameters
$request : WP_REST_Request

Full details about the request.

Return values
bool|WP_Error

fetch_fields_using_getters()

Get fields for an object if getter is defined.

public fetch_fields_using_getters(object $object, string $context, array<string|int, mixed> $fields) : array<string|int, mixed>
Parameters
$object : object

Object we are fetching response for.

$context : string

Context of the request. Can be view or edit.

$fields : array<string|int, mixed>

List of fields to fetch.

Return values
array<string|int, mixed>Data fetched from getters.

get_endpoint_args_for_item_schema()

Compatibility functions for WP 5.5, since custom types are not supported anymore.

public get_endpoint_args_for_item_schema([string $method = WP_REST_Server::CREATABLE ]) : array<string|int, mixed>

See @link https://core.trac.wordpress.org/changeset/48306

Parameters
$method : string = WP_REST_Server::CREATABLE

Optional. HTTP method of the request.

Return values
array<string|int, mixed>Endpoint arguments.

get_fields_for_response()

Gets an array of fields to be included on the response.

public get_fields_for_response(WP_REST_Request $request) : array<string|int, mixed>

Included fields are based on item schema and _fields= request argument. Updated from WordPress 5.3, included into this class to support old versions.

Parameters
$request : WP_REST_Request

Full details about the request.

Tags
since
3.5.0
Return values
array<string|int, mixed>Fields to be included in the response.

preview_refund()

Preview a refund without creating it.

public preview_refund(WP_REST_Request $request) : WP_REST_Response|WP_Error

Returns server-computed refund totals and per-line breakdowns for the requested line items, using the same calculation engine as the wc/v4 refunds endpoints, so clients do not have to replicate tax, rounding, and currency-precision logic.

Parameters
$request : WP_REST_Request

Full details about the request.

Tags
phpstan-param

WP_REST_Request<array<string, mixed>> $request

since
11.1.0
Return values
WP_REST_Response|WP_Error

validate_setting_checkbox_field()

Validate checkbox based settings.

public validate_setting_checkbox_field(string $value, array<string|int, mixed> $setting) : string|WP_Error
Parameters
$value : string

Value.

$setting : array<string|int, mixed>

Setting.

Tags
since
3.0.0
Return values
string|WP_Error

validate_setting_image_width_field()

Validate image_width based settings.

public validate_setting_image_width_field(array<string|int, mixed> $values, array<string|int, mixed> $setting) : string|WP_Error
Parameters
$values : array<string|int, mixed>

Values.

$setting : array<string|int, mixed>

Setting.

Tags
since
3.0.0
Return values
string|WP_Error

validate_setting_multiselect_field()

Validate multiselect based settings.

public validate_setting_multiselect_field(array<string|int, mixed> $values, array<string|int, mixed> $setting) : array<string|int, mixed>|WP_Error
Parameters
$values : array<string|int, mixed>

Values.

$setting : array<string|int, mixed>

Setting.

Tags
since
3.0.0
Return values
array<string|int, mixed>|WP_Error

validate_setting_radio_field()

Validate radio based settings.

public validate_setting_radio_field(string $value, array<string|int, mixed> $setting) : string|WP_Error
Parameters
$value : string

Value.

$setting : array<string|int, mixed>

Setting.

Tags
since
3.0.0
Return values
string|WP_Error

validate_setting_select_field()

Validate select based settings.

public validate_setting_select_field(string $value, array<string|int, mixed> $setting) : string|WP_Error
Parameters
$value : string

Value.

$setting : array<string|int, mixed>

Setting.

Tags
since
3.0.0
Return values
string|WP_Error

validate_setting_text_field()

Validate a text value for a text based setting.

public validate_setting_text_field(string $value, array<string|int, mixed> $setting) : string
Parameters
$value : string

Value.

$setting : array<string|int, mixed>

Setting.

Tags
since
3.0.0
Return values
string

validate_setting_textarea_field()

Validate textarea based settings.

public validate_setting_textarea_field(string $value, array<string|int, mixed> $setting) : string
Parameters
$value : string

Value.

$setting : array<string|int, mixed>

Setting.

Tags
since
3.0.0
since
9.0.0

No longer allows storing IFRAME, which was allowed for "ShareThis" integration no longer found in core.

Return values
string

add_additional_fields_schema()

Add the schema from additional fields to an schema array.

protected add_additional_fields_schema(array<string|int, mixed> $schema) : array<string|int, mixed>

The type of object is inferred from the passed schema.

Parameters
$schema : array<string|int, mixed>

Schema array.

Return values
array<string|int, mixed>

add_meta_query()

Add meta query.

protected add_meta_query(array<string|int, mixed> $args, array<string|int, mixed> $meta_query) : array<string|int, mixed>
Parameters
$args : array<string|int, mixed>

Query args.

$meta_query : array<string|int, mixed>

Meta query.

Tags
since
3.0.0
Return values
array<string|int, mixed>

adjust_wp_5_5_datatype_compatibility()

Change datatypes `date-time` to string, and `mixed` to composite of all built in types. This is required for maintaining forward compatibility with WP 5.5 since custom post types are not supported anymore.

protected adjust_wp_5_5_datatype_compatibility(array<string|int, mixed> $endpoint_args) : mixed

See @link https://core.trac.wordpress.org/changeset/48306

We still use the 'mixed' type, since if we convert to composite type everywhere, it won't work in 5.4 anymore because they require to define the full schema.

Parameters
$endpoint_args : array<string|int, mixed>

Schema with datatypes to convert.

Return values
mixedSchema with converted datatype.

get_meta_data_for_response()

Limit the contents of the meta_data property based on certain request parameters.

protected get_meta_data_for_response(WP_REST_Request $request, array<string|int, mixed> $meta_data) : array<string|int, mixed>

Note that if both include_meta and exclude_meta are present in the request, include_meta will take precedence.

Parameters
$request : WP_REST_Request

The request.

$meta_data : array<string|int, mixed>

All of the meta data for an object.

Return values
array<string|int, mixed>

item_is_null()

Helper method to check if the resource ID associated with the provided item is null.

protected item_is_null(array<string|int, mixed> $item) : bool

Items can be deleted by setting the resource ID to null.

Parameters
$item : array<string|int, mixed>

Item provided in the request body.

Return values
boolTrue if the item resource ID is null, false otherwise.

prepare_coupon_lines()

Create or update an order coupon.

protected prepare_coupon_lines(array<string|int, mixed> $posted[, string $action = 'create' ][, object $item = null ]) : WC_Order_Item_Coupon
Parameters
$posted : array<string|int, mixed>

Item data.

$action : string = 'create'

'create' to add coupon or 'update' to update it.

$item : object = null

Passed when updating an item. Null during creation.

Tags
throws
WC_REST_Exception

Invalid data, server error.

Return values
WC_Order_Item_Coupon

prepare_fee_lines()

Create or update an order fee.

protected prepare_fee_lines(array<string|int, mixed> $posted[, string $action = 'create' ][, object $item = null ]) : WC_Order_Item_Fee
Parameters
$posted : array<string|int, mixed>

Item data.

$action : string = 'create'

'create' to add fee or 'update' to update it.

$item : object = null

Passed when updating an item. Null during creation.

Tags
throws
WC_REST_Exception

Invalid data, server error.

Return values
WC_Order_Item_Fee

prepare_items_query()

Determine the allowed query_vars for a get_items() response and prepare for WP_Query.

protected prepare_items_query([array<string|int, mixed> $prepared_args = array() ][, WP_REST_Request $request = null ]) : array<string|int, mixed>
Parameters
$prepared_args : array<string|int, mixed> = array()

Prepared arguments.

$request : WP_REST_Request = null

Request object.

Return values
array<string|int, mixed>$query_args

prepare_line_items()

Create or update a line item.

protected prepare_line_items(array<string|int, mixed> $posted[, string $action = 'create' ][, object $item = null ]) : WC_Order_Item_Product
Parameters
$posted : array<string|int, mixed>

Line item data.

$action : string = 'create'

'create' to add line item or 'update' to update it.

$item : object = null

Passed when updating an item. Null during creation.

Tags
throws
WC_Data_Exception

Invalid product data.

throws
WC_REST_Exception

Invalid data, server error.

Return values
WC_Order_Item_Product

prepare_object_for_database()

Prepares one object for create or update operation.

protected prepare_object_for_database(WP_REST_Request $request[, bool $creating = false ]) : WP_Error|WC_Data
Parameters
$request : WP_REST_Request

Request object.

$creating : bool = false

If is creating a new object.

Tags
since
3.0.0
Return values
WP_Error|WC_DataThe prepared item, or WP_Error object on failure.

prepare_object_for_response_core()

Core method to prepare a single order object for response (doesn't fire hooks, execute rest_ensure_response, or add links).

protected prepare_object_for_response_core(WC_Data $order, WP_REST_Request $request) : array<string|int, mixed>
Parameters
$order : WC_Data

Object data.

$request : WP_REST_Request

Request object.

Tags
since
9.5.0
Return values
array<string|int, mixed>Prepared response data.

prepare_shipping_lines()

Create or update an order shipping method.

protected prepare_shipping_lines(array<string|int, mixed> $posted[, string $action = 'create' ][, object $item = null ]) : WC_Order_Item_Shipping
Parameters
$posted : array<string|int, mixed>

$shipping Item data.

$action : string = 'create'

'create' to add shipping or 'update' to update it.

$item : object = null

Passed when updating an item. Null during creation.

Tags
throws
WC_REST_Exception

Invalid data, server error.

Return values
WC_Order_Item_Shipping

set_item()

Wrapper method to create/update order items.

protected set_item(WC_Order $order, string $item_type, array<string|int, mixed> $posted) : mixed

When updating, the item ID provided is checked to ensure it is associated with the order.

Parameters
$order : WC_Order

order object.

$item_type : string

The item type.

$posted : array<string|int, mixed>

item provided in the request body.

Tags
throws
WC_REST_Exception

If item ID is not associated with order.

Return values
mixed

add_preview_additional_fields()

Populate fields registered for the preview object type into a response.

private add_preview_additional_fields(array<string|int, mixed> $preview, WP_REST_Request $request) : array<string|int, mixed>

The stock add_additional_fields_to_object() resolves the object type from this controller's item schema (order_refund), so it would populate the wrong field set; the preview publishes its schema as order_refund_preview and must populate the fields registered for that type. Mirrors core's _fields handling: callbacks for fields the request excludes are not executed, so extension callbacks do not run for responses that will not carry their field. Runs before the response filter so filters see the complete payload.

Parameters
$preview : array<string|int, mixed>

Preview response data.

$request : WP_REST_Request

The request.

Tags
phpstan-param

WP_REST_Request<array<string, mixed>> $request

Return values
array<string|int, mixed>

create_refund_with_computed_totals()

Create a refund with server-computed per-line totals (compute_totals mode).

private create_refund_with_computed_totals(WP_REST_Request $request) : WP_Error|WC_Data

Mirrors the wc/v4 refund creation pipeline: line items may omit refund_total (computed from quantity at the order's stored unit price, tax-inclusive, clamped to the remaining refundable amount), input is validated against the order's refund history, and the refund amount is derived from the line items unless an explicit amount override is supplied. Validation follows the same rules as the wc/v4 creation endpoint; error codes are prefixed with woocommerce_rest_ at this v3 boundary like the rest of the v3 surface.

Parameters
$request : WP_REST_Request

Request object.

Tags
phpstan-param

WP_REST_Request<array<string, mixed>> $request

since
11.1.0
Return values
WP_Error|WC_DataThe created refund, or WP_Error object on failure.

filter_internal_meta_keys()

With HPOS, few internal meta keys such as _billing_address_index, _shipping_address_index are not considered internal anymore (since most internal keys were flattened into dedicated columns).

private filter_internal_meta_keys(array<string|int, mixed> $meta_data) : array<string|int, mixed>

This function helps in filtering out any remaining internal meta keys with HPOS is enabled.

Parameters
$meta_data : array<string|int, mixed>

Order meta data.

Return values
array<string|int, mixed>Filtered order meta data.

get_data_utils()

Get the shared refund calculation engine.

private get_data_utils() : DataUtils

DataUtils is the calculation/validation engine shared with the wc/v4 refunds endpoints (the V4 segment in its namespace is historical); using it here keeps wc/v3 and wc/v4 refund math identical.

Return values
DataUtils

get_preview_fields_for_response()

Get the preview fields a request asks for, mirroring core's get_fields_for_response() against the preview schema instead of the controller's item schema.

private get_preview_fields_for_response(WP_REST_Request $request) : array<string|int, string>
Parameters
$request : WP_REST_Request

The request.

Tags
phpstan-param

WP_REST_Request<array<string, mixed>> $request

Return values
array<string|int, string>

get_preview_line_items_arg_schema()

Get the argument schema for the preview route's line_items parameter.

private get_preview_line_items_arg_schema() : array<string|int, mixed>

Shared with the wc/v4 preview endpoint (including the line_item_id key naming) so clients can send the same payload to both API versions and the accepted shape cannot drift between them.

Note the two deliberate differences from this controller's create endpoint: the preview keys lines by line_item_id where the create uses id, and the preview's refund_total is tax-inclusive where the create's classic refund_total is net with taxes supplied separately via refund_tax (the compute_totals create shares the preview's tax-inclusive semantics).

Return values
array<string|int, mixed>

merge_meta_item_with_formatted_meta_display_attributes()

Merge the `$formatted_meta_data` `display_key` and `display_value` attribute values into the corresponding {@link WC_Meta_Data}. Returns the merged array.

private merge_meta_item_with_formatted_meta_display_attributes(WC_Meta_Data $meta_item, array<string|int, mixed> $formatted_meta_data) : array<string|int, mixed>
Parameters
$meta_item : WC_Meta_Data

An object from {@link}.

$formatted_meta_data : array<string|int, mixed>

An object result from {@link}. The keys are the IDs of {@link}.

Return values
array<string|int, mixed>

normalize_line_item()

Normalize one compute_totals line item to the shared engine's shape.

private normalize_line_item(array<string|int, mixed> $line_item) : array<string|int, mixed>|WP_Error

Maps the create endpoint's public id key to the engine's line_item_id and validates/normalizes the scalar types. The REST schema cannot validate the line_items subtree (the property is readonly for backward compatibility), so without this check malformed values such as an array refund_total would reach the calculation engine and fail with a TypeError instead of a 400 response. Uses the same error codes as the engine's own validation, and casts numeric strings to their proper types.

Parameters
$line_item : array<string|int, mixed>

Line item in the public request shape (id keys).

Tags
since
11.1.0
Return values
array<string|int, mixed>|WP_ErrorThe normalized line item, or WP_Error on an invalid type.

prefix_error_code()

Prefix a shared-engine error code with `woocommerce_rest_`.

private prefix_error_code(WP_Error $error) : WP_Error

DataUtils emits unprefixed codes (the wc/v4 convention). The wc/v3 surface uses woocommerce_rest_*, so errors crossing into a v3 response are renamed at this boundary. Codes that already carry the prefix pass through unchanged, and the message and data (including the HTTP status) are kept. An unprefixed engine error whose data carries no HTTP status is backfilled with 400, the same default the wc/v4 envelope applies, so it is not served as a 500. An already-prefixed code returns untouched above and so misses that backfill, which leaves no gap: every prefixed error reaching this endpoint is built by normalize_line_item() with an explicit status.

Parameters
$error : WP_Error

The error whose code should be prefixed.

Return values
WP_Error