WooCommerce Code Reference

CartItemSchema extends ItemSchema
in package
Uses ProductItemTrait

CartItemSchema class.

Table of Contents

EXTENDING_KEY  = 'extensions'
Extending key that gets added to endpoint.
IDENTIFIER  = 'cart-item'
The schema item identifier.
$controller  : SchemaController
Schema Controller instance.
$extend  : ExtendSchema
Rest extend instance.
$image_attachment_schema  : ImageAttachmentSchema
Image attachment schema instance.
$title  : string
The schema item name.
__construct()  : mixed
Constructor.
get_endpoint_args_for_item_schema()  : array<string|int, mixed>
Retrieves an array of endpoint arguments from the item schema for the controller.
get_item_response()  : array<string|int, mixed>
Convert a WooCommerce cart item to an object suitable for the response.
get_item_schema()  : array<string|int, mixed>
Returns the full item schema.
get_properties()  : array<string|int, mixed>
Return schema properties.
get_public_item_schema()  : array<string|int, mixed>
Returns the public schema.
filter_valid_attribute()  : bool
Returns true if the given attribute is valid.
filter_variation_attribute()  : bool
Returns true if the given attribute is valid and used for variations.
force_schema_readonly()  : array<string|int, mixed>
Force all schema properties to be readonly.
format_item_data_element()  : array<string|int, mixed>
Remove HTML tags from cart item data and set the `hidden` property to `__experimental_woocommerce_blocks_hidden`.
format_variation_data()  : array<string|int, mixed>
Format variation data, for example convert slugs such as attribute_pa_size to Size.
get_attributes()  : array<string|int, mixed>
Get list of product attributes and attribute terms.
get_extended_data()  : object
Returns extended data for a specific endpoint.
get_extended_schema()  : array<string|int, mixed>
Returns extended schema for a specific endpoint.
get_images()  : array<string|int, mixed>
Get list of product images.
get_item_data()  : array<string|int, mixed>
Format cart item data removing any HTML tag.
get_item_responses_from_schema()  : array<string|int, mixed>
Apply a schema get_item_response callback to an array of items and return the result.
get_low_stock_remaining()  : int|null
If a product has low stock, return the remaining stock amount for display.
get_price_function_from_tax_display_mode()  : string
WooCommerce can return prices including or excluding tax; choose the correct method based on tax display mode.
get_price_range()  : object|null
Get price range from certain product types.
get_recursive_sanitize_callback()  : function
Gets a function that sanitizes recursively.
get_recursive_schema_property_defaults()  : array<string|int, mixed>
Gets an array of schema defaults recursively.
get_recursive_validate_callback()  : function
Gets a function that validates recursively.
get_remaining_stock()  : int|null
Gets remaining stock amount for a product.
get_store_currency_properties()  : array<string|int, mixed>
Returns consistent currency schema used across endpoints for prices.
get_tax_display_mode()  : string
WooCommerce can return prices including or excluding tax; choose the correct method based on tax display mode.
get_term_list()  : array<string|int, mixed>
Returns a list of terms assigned to the product.
get_variations()  : mixed
Get variation IDs and attributes from the DB.
prepare_currency_response()  : array<string|int, mixed>
Adds currency data to an array of monetary values.
prepare_html_response()  : string|array<string|int, mixed>
Prepares HTML based content, such as post titles and content, for the API response.
prepare_money_response()  : string
Convert monetary values from WooCommerce to string based integers, using the smallest unit of a currency.
prepare_product_attribute_taxonomy_value()  : object
Prepare an attribute term for the response.
prepare_product_attribute_value()  : object
Prepare an attribute term for the response.
prepare_product_price_response()  : array<string|int, mixed>
Get an array of pricing data.
remove_arg_options()  : mixed
Recursive removal of arg_options.

Constants

Properties

Methods

get_endpoint_args_for_item_schema()

Retrieves an array of endpoint arguments from the item schema for the controller.

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

Optional. HTTP method of the request.

Tags
uses
rest_get_endpoint_args_for_schema()
Return values
array<string|int, mixed>Endpoint arguments.

get_item_response()

Convert a WooCommerce cart item to an object suitable for the response.

public get_item_response(array<string|int, mixed> $cart_item) : array<string|int, mixed>
Parameters
$cart_item : array<string|int, mixed>

Cart item array.

Return values
array<string|int, mixed>

filter_valid_attribute()

Returns true if the given attribute is valid.

protected filter_valid_attribute(mixed $attribute) : bool
Parameters
$attribute : mixed

Object or variable to check.

Return values
bool

filter_variation_attribute()

Returns true if the given attribute is valid and used for variations.

protected filter_variation_attribute(mixed $attribute) : bool
Parameters
$attribute : mixed

Object or variable to check.

Return values
bool

force_schema_readonly()

Force all schema properties to be readonly.

protected force_schema_readonly(array<string|int, mixed> $properties) : array<string|int, mixed>
Parameters
$properties : array<string|int, mixed>

Schema.

Return values
array<string|int, mixed>Updated schema.

format_item_data_element()

Remove HTML tags from cart item data and set the `hidden` property to `__experimental_woocommerce_blocks_hidden`.

protected format_item_data_element(array<string|int, mixed> $item_data_element) : array<string|int, mixed>
Parameters
$item_data_element : array<string|int, mixed>

Individual element of a cart item data.

Return values
array<string|int, mixed>

format_variation_data()

Format variation data, for example convert slugs such as attribute_pa_size to Size.

protected format_variation_data(array<string|int, mixed> $variation_data, WC_Product $product) : array<string|int, mixed>
Parameters
$variation_data : array<string|int, mixed>

Array of data from the cart.

$product : WC_Product

Product data.

Return values
array<string|int, mixed>

get_extended_data()

Returns extended data for a specific endpoint.

protected get_extended_data(string $endpoint, array<string|int, mixed> ...$passed_args) : object
Parameters
$endpoint : string

The endpoint identifier.

$passed_args : array<string|int, mixed>

An array of arguments to be passed to callbacks.

Return values
objectthe data that will get added.

get_extended_schema()

Returns extended schema for a specific endpoint.

protected get_extended_schema(string $endpoint, array<string|int, mixed> ...$passed_args) : array<string|int, mixed>
Parameters
$endpoint : string

The endpoint identifer.

$passed_args : array<string|int, mixed>

An array of arguments to be passed to callbacks.

Return values
array<string|int, mixed>the data that will get added.

get_item_data()

Format cart item data removing any HTML tag.

protected get_item_data(array<string|int, mixed> $cart_item) : array<string|int, mixed>
Parameters
$cart_item : array<string|int, mixed>

Cart item array.

Return values
array<string|int, mixed>

get_item_responses_from_schema()

Apply a schema get_item_response callback to an array of items and return the result.

protected get_item_responses_from_schema(AbstractSchema $schema, array<string|int, mixed> $items) : array<string|int, mixed>
Parameters
$schema : AbstractSchema

Schema class instance.

$items : array<string|int, mixed>

Array of items.

Return values
array<string|int, mixed>Array of values from the callback function.

get_price_function_from_tax_display_mode()

WooCommerce can return prices including or excluding tax; choose the correct method based on tax display mode.

protected get_price_function_from_tax_display_mode(string $tax_display_mode) : string
Parameters
$tax_display_mode : string

If returned prices are incl or excl of tax.

Return values
stringFunction name.

get_price_range()

Get price range from certain product types.

protected get_price_range(WC_Product $product[, string $tax_display_mode = '' ]) : object|null
Parameters
$product : WC_Product

Product instance.

$tax_display_mode : string = ''

If returned prices are incl or excl of tax.

Return values
object|null

get_recursive_sanitize_callback()

Gets a function that sanitizes recursively.

protected get_recursive_sanitize_callback(array<string|int, mixed> $properties) : function
Parameters
$properties : array<string|int, mixed>

Schema property data.

Return values
functionAnonymous validation callback.

get_recursive_schema_property_defaults()

Gets an array of schema defaults recursively.

protected get_recursive_schema_property_defaults(array<string|int, mixed> $properties) : array<string|int, mixed>
Parameters
$properties : array<string|int, mixed>

Schema property data.

Return values
array<string|int, mixed>Array of defaults, pulled from arg_options

get_recursive_validate_callback()

Gets a function that validates recursively.

protected get_recursive_validate_callback(array<string|int, mixed> $properties) : function
Parameters
$properties : array<string|int, mixed>

Schema property data.

Return values
functionAnonymous validation callback.

get_store_currency_properties()

Returns consistent currency schema used across endpoints for prices.

protected get_store_currency_properties() : array<string|int, mixed>
Return values
array<string|int, mixed>

get_tax_display_mode()

WooCommerce can return prices including or excluding tax; choose the correct method based on tax display mode.

protected get_tax_display_mode([string $tax_display_mode = '' ]) : string
Parameters
$tax_display_mode : string = ''

Provided tax display mode.

Return values
stringValid tax display mode.

get_term_list()

Returns a list of terms assigned to the product.

protected get_term_list(WC_Product $product[, string $taxonomy = '' ]) : array<string|int, mixed>
Parameters
$product : WC_Product

Product object.

$taxonomy : string = ''

Taxonomy name.

Return values
array<string|int, mixed>Array of terms (id, name, slug).

prepare_currency_response()

Adds currency data to an array of monetary values.

protected prepare_currency_response(array<string|int, mixed> $values) : array<string|int, mixed>
Parameters
$values : array<string|int, mixed>

Monetary amounts.

Return values
array<string|int, mixed>Monetary amounts with currency data appended.

prepare_html_response()

Prepares HTML based content, such as post titles and content, for the API response.

protected prepare_html_response(string|array<string|int, mixed> $response) : string|array<string|int, mixed>
Parameters
$response : string|array<string|int, mixed>

Data to format.

Return values
string|array<string|int, mixed>Formatted data.

prepare_money_response()

Convert monetary values from WooCommerce to string based integers, using the smallest unit of a currency.

protected prepare_money_response(string|float $amount[, int $decimals = 2 ][, int $rounding_mode = PHP_ROUND_HALF_UP ]) : string
Parameters
$amount : string|float

Monetary amount with decimals.

$decimals : int = 2

Number of decimals the amount is formatted with.

$rounding_mode : int = PHP_ROUND_HALF_UP

Defaults to the PHP_ROUND_HALF_UP constant.

Return values
stringThe new amount.

prepare_product_attribute_taxonomy_value()

Prepare an attribute term for the response.

protected prepare_product_attribute_taxonomy_value(WP_Term $term) : object
Parameters
$term : WP_Term

Term object.

Return values
object

prepare_product_attribute_value()

Prepare an attribute term for the response.

protected prepare_product_attribute_value(string $name, int $id[, string $slug = '' ]) : object
Parameters
$name : string

Attribute term name.

$id : int

Attribute term ID.

$slug : string = ''

Attribute term slug.

Return values
object

prepare_product_price_response()

Get an array of pricing data.

protected prepare_product_price_response(WC_Product $product[, string $tax_display_mode = '' ]) : array<string|int, mixed>
Parameters
$product : WC_Product

Product instance.

$tax_display_mode : string = ''

If returned prices are incl or excl of tax.

Return values
array<string|int, mixed>

remove_arg_options()

Recursive removal of arg_options.

protected remove_arg_options(array<string|int, mixed> $properties) : mixed
Parameters
$properties : array<string|int, mixed>

Schema properties.

Return values
mixed