WC_REST_Product_Variations_V2_Controller
extends WC_REST_Products_V2_Controller
in package
REST API variations controller class.
Tags
Table of Contents
- $hierarchical : bool
- If object is hierarchical.
- $namespace : string
- Endpoint namespace.
- $post_type : string
- Post type.
- $public : string
- Controls visibility on frontend.
- $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
- Initialize product 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.
- clear_transients() : mixed
- Clear caches here so in sync with any new variations.
- 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() : bool|WP_Error|WP_REST_Response
- Delete a variation.
- 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 Variation's 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.
- prepare_object_for_response() : WP_REST_Response
- Prepare a single variation output for response.
- register_routes() : mixed
- Register the routes for products.
- 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.
- api_get_meta_data() : array<string|int, mixed>
- Fetch meta data.
- api_get_price_html() : string
- Fetch price HTML.
- api_get_related_ids() : array<string|int, mixed>
- Fetch related IDs.
- check_batch_limit() : bool|WP_Error
- Check batch limit.
- delete_post() : mixed
- Delete post.
- get_allowed_query_vars() : array<string|int, mixed>
- Get all the WP Query vars that are allowed for the API request.
- get_attribute_options() : array<string|int, mixed>
- Get attribute options.
- get_attribute_taxonomy_label() : string
- Get attribute taxonomy label.
- get_attribute_taxonomy_name() : string
- Get product attribute taxonomy name.
- get_attributes() : array<string|int, mixed>
- Get the attributes for a product or product variation.
- get_default_attributes() : array<string|int, mixed>
- Get default attributes.
- get_downloads() : array<string|int, mixed>
- Get the downloads for a product or product variation.
- get_images() : array<string|int, mixed>
- Get the images for a product or product variation.
- 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() : WC_Data
- Get object.
- get_objects() : array<string|int, mixed>
- Get objects.
- get_permalink() : string
- Get object permalink.
- get_product_data() : array<string|int, mixed>
- Get product data.
- get_taxonomy_terms() : array<string|int, mixed>
- Get taxonomy terms.
- prepare_items_query() : array<string|int, mixed>
- Determine the allowed query_vars for a get_items() response and prepare for WP_Query.
- prepare_links() : array<string|int, mixed>
- Prepare links for the request.
- prepare_object_for_database() : WP_Error|WC_Data
- Prepare a single variation for create or update.
- prepare_objects_query() : array<string|int, mixed>
- Prepare objects query.
- save_default_attributes() : WC_Product
- Save default attributes.
- save_downloadable_files() : WC_Product
- Save downloadable files.
- save_object() : WC_Data|WP_Error
- Save an object data.
- save_product_shipping_data() : WC_Product
- Save product shipping data.
- save_taxonomy_terms() : WC_Product
- Save taxonomy terms.
- set_product_images() : WC_Product
- Set product images.
- update_post_meta_fields() : bool|WP_Error
- Update post meta fields.
Properties
$hierarchical
If object is hierarchical.
protected
bool
$hierarchical
= alse
$namespace
Endpoint namespace.
protected
string
$namespace
= 'wc/v2'
$post_type
Post type.
protected
string
$post_type
= 'product_variation'
$public
Controls visibility on frontend.
protected
string
$public
= alse
$rest_base
Route base.
protected
string
$rest_base
= 'products/(?P<product_id>[\d]+)/variations'
$_fields
Used to cache computed return fields.
private
null|array<string|int, mixed>
$_fields
=
ull
$_request
Used to verify if cached fields are for correct request object.
private
null|WP_REST_Request
$_request
=
ull
Methods
__construct()
Initialize product actions.
public
__construct() : mixed
Return values
mixed —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.
Tags
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 —clear_transients()
Clear caches here so in sync with any new variations.
public
clear_transients(WC_Data $object) : mixed
Parameters
- $object : WC_Data
-
Object data.
Return values
mixed —create_item()
Create a single item.
public
create_item(WP_REST_Request $request) : WP_Error|WP_REST_Response
Parameters
- $request : WP_REST_Request
-
Full details about the request.
Return values
WP_Error|WP_REST_Response —create_item_permissions_check()
Check if a given request has access to create an item.
public
create_item_permissions_check(WP_REST_Request $request) : WP_Error|bool
Parameters
- $request : WP_REST_Request
-
Full details about the request.
Return values
WP_Error|bool —delete_item()
Delete a variation.
public
delete_item(WP_REST_Request $request) : bool|WP_Error|WP_REST_Response
Parameters
- $request : WP_REST_Request
-
Full details about the request.
Return values
bool|WP_Error|WP_REST_Response —delete_item_permissions_check()
Check if a given request has access to delete an item.
public
delete_item_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
oredit
. - $fields : array<string|int, mixed>
-
List of fields to fetch.
Return values
array<string|int, mixed> — Data fetched from getters.get_collection_params()
Get the query params for collections of attachments.
public
get_collection_params() : array<string|int, mixed>
Return values
array<string|int, mixed> —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
Return values
array<string|int, mixed> — Fields to be included in the response.get_item()
Get a single item.
public
get_item(WP_REST_Request $request) : WP_Error|WP_REST_Response
Parameters
- $request : WP_REST_Request
-
Full details about the request.
Return values
WP_Error|WP_REST_Response —get_item_permissions_check()
Check if a given request has access to read an item.
public
get_item_permissions_check(WP_REST_Request $request) : WP_Error|bool
Parameters
- $request : WP_REST_Request
-
Full details about the request.
Return values
WP_Error|bool —get_item_schema()
Get the Variation's schema, conforming to JSON Schema.
public
get_item_schema() : array<string|int, mixed>
Return values
array<string|int, mixed> —get_items()
Get a collection of posts.
public
get_items(WP_REST_Request $request) : WP_Error|WP_REST_Response
Parameters
- $request : WP_REST_Request
-
Full details about the request.
Return values
WP_Error|WP_REST_Response —get_items_permissions_check()
Check if a given request has access to read items.
public
get_items_permissions_check(WP_REST_Request $request) : WP_Error|bool
Parameters
- $request : WP_REST_Request
-
Full details about the request.
Return values
WP_Error|bool —get_public_batch_schema()
Get the batch schema, conforming to JSON Schema.
public
get_public_batch_schema() : array<string|int, mixed>
Return values
array<string|int, mixed> —prepare_object_for_response()
Prepare a single variation output for response.
public
prepare_object_for_response(WC_Data $object, WP_REST_Request $request) : WP_REST_Response
Parameters
- $object : WC_Data
-
Object data.
- $request : WP_REST_Request
-
Request object.
Tags
Return values
WP_REST_Response —register_routes()
Register the routes for products.
public
register_routes() : mixed
Return values
mixed —update_item()
Update a single post.
public
update_item(WP_REST_Request $request) : WP_Error|WP_REST_Response
Parameters
- $request : WP_REST_Request
-
Full details about the request.
Return values
WP_Error|WP_REST_Response —update_item_permissions_check()
Check if a given request has access to update an item.
public
update_item_permissions_check(WP_REST_Request $request) : WP_Error|bool
Parameters
- $request : WP_REST_Request
-
Full details about the request.
Return values
WP_Error|bool —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
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
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
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
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
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
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
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
Return values
array<string|int, mixed> —add_post_meta_fields()
Add post meta fields.
protected
add_post_meta_fields(WP_Post $post, WP_REST_Request $request) : bool|WP_Error
Parameters
- $post : WP_Post
-
Post Object.
- $request : WP_REST_Request
-
WP_REST_Request Object.
Return values
bool|WP_Error —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
mixed — Schema with converted datatype.api_get_meta_data()
Fetch meta data.
protected
api_get_meta_data(WC_Product $product, string $context) : array<string|int, mixed>
Parameters
- $product : WC_Product
-
Product object.
- $context : string
-
Context of request, can be
view
oredit
.
Return values
array<string|int, mixed> —api_get_price_html()
Fetch price HTML.
protected
api_get_price_html(WC_Product $product, string $context) : string
Parameters
- $product : WC_Product
-
Product object.
- $context : string
-
Context of request, can be
view
oredit
.
Return values
string —api_get_related_ids()
Fetch related IDs.
protected
api_get_related_ids(WC_Product $product, string $context) : array<string|int, mixed>
Parameters
- $product : WC_Product
-
Product object.
- $context : string
-
Context of request, can be
view
oredit
.
Return values
array<string|int, mixed> —check_batch_limit()
Check batch limit.
protected
check_batch_limit(array<string|int, mixed> $items) : bool|WP_Error
Parameters
- $items : array<string|int, mixed>
-
Request items.
Return values
bool|WP_Error —delete_post()
Delete post.
protected
delete_post(WP_Post $post) : mixed
Parameters
- $post : WP_Post
-
Post object.
Return values
mixed —get_allowed_query_vars()
Get all the WP Query vars that are allowed for the API request.
protected
get_allowed_query_vars() : array<string|int, mixed>
Return values
array<string|int, mixed> —get_attribute_options()
Get attribute options.
protected
get_attribute_options(int $product_id, array<string|int, mixed> $attribute) : array<string|int, mixed>
Parameters
- $product_id : int
-
Product ID.
- $attribute : array<string|int, mixed>
-
Attribute data.
Return values
array<string|int, mixed> —get_attribute_taxonomy_label()
Get attribute taxonomy label.
protected
get_attribute_taxonomy_label(string $name) : string
Parameters
- $name : string
-
Taxonomy name.
Tags
Return values
string —get_attribute_taxonomy_name()
Get product attribute taxonomy name.
protected
get_attribute_taxonomy_name(string $slug, WC_Product $product) : string
Parameters
- $slug : string
-
Taxonomy name.
- $product : WC_Product
-
Product data.
Tags
Return values
string —get_attributes()
Get the attributes for a product or product variation.
protected
get_attributes(WC_Product|WC_Product_Variation $product) : array<string|int, mixed>
Parameters
- $product : WC_Product|WC_Product_Variation
-
Product instance.
Return values
array<string|int, mixed> —get_default_attributes()
Get default attributes.
protected
get_default_attributes(WC_Product $product) : array<string|int, mixed>
Parameters
- $product : WC_Product
-
Product instance.
Return values
array<string|int, mixed> —get_downloads()
Get the downloads for a product or product variation.
protected
get_downloads(WC_Product|WC_Product_Variation $product) : array<string|int, mixed>
Parameters
- $product : WC_Product|WC_Product_Variation
-
Product instance.
Return values
array<string|int, mixed> —get_images()
Get the images for a product or product variation.
protected
get_images(WC_Product|WC_Product_Variation $product) : array<string|int, mixed>
Parameters
- $product : WC_Product|WC_Product_Variation
-
Product instance.
Return values
array<string|int, mixed> —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> —get_normalized_rest_base()
Get normalized rest base.
protected
get_normalized_rest_base() : string
Return values
string —get_object()
Get object.
protected
get_object(int $id) : WC_Data
Parameters
- $id : int
-
Object ID.
Tags
Return values
WC_Data —get_objects()
Get objects.
protected
get_objects(array<string|int, mixed> $query_args) : array<string|int, mixed>
Parameters
- $query_args : array<string|int, mixed>
-
Query args.
Tags
Return values
array<string|int, mixed> —get_permalink()
Get object permalink.
protected
get_permalink(object $object) : string
Parameters
- $object : object
-
Object.
Return values
string —get_product_data()
Get product data.
protected
get_product_data(WC_Product $product[, string $context = 'view' ]) : array<string|int, mixed>
Parameters
- $product : WC_Product
-
Product instance.
- $context : string = 'view'
-
Request context. Options: 'view' and 'edit'.
Return values
array<string|int, mixed> —get_taxonomy_terms()
Get taxonomy terms.
protected
get_taxonomy_terms(WC_Product $product[, string $taxonomy = 'cat' ]) : array<string|int, mixed>
Parameters
- $product : WC_Product
-
Product instance.
- $taxonomy : string = 'cat'
-
Taxonomy slug.
Return values
array<string|int, mixed> —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_argsprepare_links()
Prepare links for the request.
protected
prepare_links(WC_Data $object, WP_REST_Request $request) : array<string|int, mixed>
Parameters
- $object : WC_Data
-
Object data.
- $request : WP_REST_Request
-
Request object.
Return values
array<string|int, mixed> — Links for the given post.prepare_object_for_database()
Prepare a single variation for create or update.
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.
Return values
WP_Error|WC_Data —prepare_objects_query()
Prepare objects query.
protected
prepare_objects_query(WP_REST_Request $request) : array<string|int, mixed>
Parameters
- $request : WP_REST_Request
-
Full details about the request.
Tags
Return values
array<string|int, mixed> —save_default_attributes()
Save default attributes.
protected
save_default_attributes(WC_Product $product, WP_REST_Request $request) : WC_Product
Parameters
- $product : WC_Product
-
Product instance.
- $request : WP_REST_Request
-
Request data.
Tags
Return values
WC_Product —save_downloadable_files()
Save downloadable files.
protected
save_downloadable_files(WC_Product $product, array<string|int, mixed> $downloads, int $deprecated) : WC_Product
Parameters
- $product : WC_Product
-
Product instance.
- $downloads : array<string|int, mixed>
-
Downloads data.
- $deprecated : int
-
Deprecated since 3.0.
Return values
WC_Product —save_object()
Save an object data.
protected
save_object(WP_REST_Request $request[, bool $creating = false ]) : WC_Data|WP_Error
Parameters
- $request : WP_REST_Request
-
Full details about the request.
- $creating : bool = false
-
If is creating a new object.
Tags
Return values
WC_Data|WP_Error —save_product_shipping_data()
Save product shipping data.
protected
save_product_shipping_data(WC_Product $product, array<string|int, mixed> $data) : WC_Product
Parameters
- $product : WC_Product
-
Product instance.
- $data : array<string|int, mixed>
-
Shipping data.
Return values
WC_Product —save_taxonomy_terms()
Save taxonomy terms.
protected
save_taxonomy_terms(WC_Product $product, array<string|int, mixed> $terms[, string $taxonomy = 'cat' ]) : WC_Product
Parameters
- $product : WC_Product
-
Product instance.
- $terms : array<string|int, mixed>
-
Terms data.
- $taxonomy : string = 'cat'
-
Taxonomy name.
Return values
WC_Product —set_product_images()
Set product images.
protected
set_product_images(WC_Product $product, array<string|int, mixed> $images) : WC_Product
Parameters
- $product : WC_Product
-
Product instance.
- $images : array<string|int, mixed>
-
Images data.
Tags
Return values
WC_Product —update_post_meta_fields()
Update post meta fields.
protected
update_post_meta_fields(WP_Post $post, WP_REST_Request $request) : bool|WP_Error
Parameters
- $post : WP_Post
-
Post object.
- $request : WP_REST_Request
-
Request object.