WooCommerce Code Reference

Search results

WC_Order_Item_Product extends WC_Order_Item

Order item product class.

Implemented by classes using the same CRUD(s) pattern.

Table of Contents

$extra_data Order Data array. This is the core order data exposed in APIs since 3.0.0. array
$data Core data for this object. Name value pairs (name + default value). array
$cache_group Stores meta in cache for future reads. string
$meta_type Meta type. This should match up with the types available at https://developer.wordpress.org/reference/functions/add_metadata/. string
$object_type This is the name of this object type. string
$id ID for this object. int
$changes Core data changes for this object. array
$object_read This is false until the object is read from the DB. bool
$extra_data Extra data for this object. Name value pairs (name + default value). array
$default_data Set to _data on construct so we can track and reset data if needed. array
$data_store Contains a reference to the data store for this class. object
$meta_data Stores additional meta data. array
set_quantity() Set quantity. mixed
set_tax_class() Set tax class. mixed
set_product_id() Set Product ID mixed
set_variation_id() Set variation ID. mixed
set_subtotal() Line subtotal (before discounts). mixed
set_total() Line total (after discounts). mixed
set_subtotal_tax() Line subtotal tax (before discounts). mixed
set_total_tax() Line total tax (after discounts). mixed
set_taxes() Set line taxes and totals for passed in taxes. mixed
set_variation() Set variation data (stored as meta data - write only). mixed
set_product() Set properties based on passed in product object. mixed
set_backorder_meta() Set meta data for backordered products. mixed
get_type() Get order item type. string
get_product_id() Get product ID. int
get_variation_id() Get variation ID. int
get_quantity() Get quantity. int
get_tax_class() Get tax class. string
get_subtotal() Get subtotal. string
get_subtotal_tax() Get subtotal tax. string
get_total() Get total. string
get_total_tax() Get total tax. string
get_taxes() Get taxes. array
get_product() Get the associated product. WC_Product|bool
get_item_download_url() Get the Download URL. string
get_item_downloads() Get any associated downloadable files. array
get_tax_status() Get tax status. string
offsetGet() OffsetGet for ArrayAccess/Backwards compatibility. mixed
offsetSet() OffsetSet for ArrayAccess/Backwards compatibility. mixed
offsetExists() OffsetExists for ArrayAccess. bool
__construct() Default constructor. mixed
apply_changes() Merge changes with data and clear. mixed
get_order_id() Get order ID this meta belongs to. int
get_name() Get order item name. string
get_type() Get order item type. Overridden by child classes. string
get_quantity() Get quantity. int
get_tax_status() Get tax status. string
get_tax_class() Get tax class. string
get_order() Get parent order object. WC_Order
set_order_id() Set order ID. mixed
set_name() Set order item name. mixed
is_type() Type checking. bool
calculate_taxes() Calculate item taxes. bool
get_formatted_meta_data() Expands things like term slugs before return. array
offsetSet() OffsetSet for ArrayAccess. mixed
offsetUnset() OffsetUnset for ArrayAccess. mixed
offsetExists() OffsetExists for ArrayAccess. bool
offsetGet() OffsetGet for ArrayAccess. mixed
__sleep() Only store the object ID to avoid serializing the data object instance. array
__wakeup() Re-run the constructor with the object ID. mixed
__clone() When the object is cloned, make sure meta is duplicated correctly. mixed
get_data_store() Get the data store. object
get_id() Returns the unique ID for this object. int
delete() Delete an object, set the ID to 0, and return result. bool
save() Save should create or update based on object existence. int
__toString() Change data to JSON format. string
get_data() Returns all data for this object. array
get_data_keys() Returns array of expected data keys for this object. array
get_extra_data_keys() Returns all "extra" data keys for an object (for sub objects like product types). array
filter_null_meta() Filter null meta values from array. bool
get_meta_data() Get All Meta Data. array
is_internal_meta_key() Check if the key is an internal one. bool
get_meta() Get Meta Data by Key. mixed
meta_exists() See if meta data exists, since get_meta always returns a '' or array(). bool
set_meta_data() Set all meta data from array. mixed
add_meta_data() Add meta data. mixed
update_meta_data() Update meta data by key or ID, if provided. mixed
delete_meta_data() Delete meta data. mixed
delete_meta_data_by_mid() Delete meta data. mixed
maybe_read_meta_data() Read meta data if null. mixed
read_meta_data() Read Meta Data from the database. Ignore any internal properties. mixed
save_meta_data() Update Meta Data in the database. mixed
set_id() Set ID. mixed
set_defaults() Set all props to default values. mixed
set_object_read() Set object read property. mixed
get_object_read() Get object read property. bool
set_props() Set a collection of props in one go, collect any errors, and return the result. bool|WP_Error
set_prop() Sets a prop for a setter method. mixed
get_changes() Return data changes only. array
get_hook_prefix() Prefix for action and filter hooks on data. string
get_prop() Gets a prop for a getter method. mixed
set_date_prop() Sets a date prop whilst handling formatting and datetime objects. mixed
error() When invalid data is found, throw an exception unless reading from the DB. mixed

Properties

$extra_data

Order Data array. This is the core order data exposed in APIs since 3.0.0.

protected array $extra_data = array('product_id' => 0, 'variation_id' => 0, 'quantity' => 1, 'tax_class' => '', 'subtotal' => 0, 'subtotal_tax' => 0, 'total' => 0, 'total_tax' => 0, 'taxes' => array('subtotal' => array(), 'total' => array()))

$data

Core data for this object. Name value pairs (name + default value).

protected array $data = array()

$cache_group

Stores meta in cache for future reads.

protected string $cache_group = ''
A group must be set to to enable caching.

$meta_type

Meta type. This should match up with the types available at https://developer.wordpress.org/reference/functions/add_metadata/.

protected string $meta_type = 'order_item'
WP defines 'post', 'user', 'comment', and 'term'.

$object_type

This is the name of this object type.

protected string $object_type = 'data'

$changes

Core data changes for this object.

protected array $changes = array()

$object_read

This is false until the object is read from the DB.

protected bool $object_read = \false

$extra_data

Extra data for this object. Name value pairs (name + default value).

protected array $extra_data = array()
Used as a standard way for sub classes (like product types) to add additional information to an inherited class.

$default_data

Set to _data on construct so we can track and reset data if needed.

protected array $default_data = array()

$data_store

Contains a reference to the data store for this class.

protected object $data_store

$meta_data

Stores additional meta data.

protected array $meta_data = \null

Methods

set_quantity()

Set quantity.

public set_quantity( $value : int ) : mixed
Parameters
$value : int

Quantity.

Return values
mixed

set_tax_class()

Set tax class.

public set_tax_class( $value : string ) : mixed
Parameters
$value : string

Tax class.

Return values
mixed

set_product_id()

Set Product ID

public set_product_id( $value : int ) : mixed
Parameters
$value : int

Product ID.

Return values
mixed

set_variation_id()

Set variation ID.

public set_variation_id( $value : int ) : mixed
Parameters
$value : int

Variation ID.

Return values
mixed

set_subtotal()

Line subtotal (before discounts).

public set_subtotal( $value : string ) : mixed
Parameters
$value : string

Subtotal.

Return values
mixed

set_total()

Line total (after discounts).

public set_total( $value : string ) : mixed
Parameters
$value : string

Total.

Return values
mixed

set_subtotal_tax()

Line subtotal tax (before discounts).

public set_subtotal_tax( $value : string ) : mixed
Parameters
$value : string

Subtotal tax.

Return values
mixed

set_total_tax()

Line total tax (after discounts).

public set_total_tax( $value : string ) : mixed
Parameters
$value : string

Total tax.

Return values
mixed

set_taxes()

Set line taxes and totals for passed in taxes.

public set_taxes( $raw_tax_data : array ) : mixed
Parameters
$raw_tax_data : array

Raw tax data.

Return values
mixed

set_variation()

Set variation data (stored as meta data - write only).

public set_variation( [ $data : array = array() ] ) : mixed
Parameters
$data : array = array()

Key/Value pairs.

Return values
mixed

set_backorder_meta()

Set meta data for backordered products.

public set_backorder_meta( ) : mixed
Return values
mixed

get_product_id()

Get product ID.

public get_product_id( [ $context : string = 'view' ] ) : int
Parameters
$context : string = 'view'

What the value is for. Valid values are 'view' and 'edit'.

Return values
int

get_variation_id()

Get variation ID.

public get_variation_id( [ $context : string = 'view' ] ) : int
Parameters
$context : string = 'view'

What the value is for. Valid values are 'view' and 'edit'.

Return values
int

get_quantity()

Get quantity.

public get_quantity( [ $context : string = 'view' ] ) : int
Parameters
$context : string = 'view'

What the value is for. Valid values are 'view' and 'edit'.

Return values
int

get_tax_class()

Get tax class.

public get_tax_class( [ $context : string = 'view' ] ) : string
Parameters
$context : string = 'view'

What the value is for. Valid values are 'view' and 'edit'.

Return values
string

get_subtotal()

Get subtotal.

public get_subtotal( [ $context : string = 'view' ] ) : string
Parameters
$context : string = 'view'

What the value is for. Valid values are 'view' and 'edit'.

Return values
string

get_subtotal_tax()

Get subtotal tax.

public get_subtotal_tax( [ $context : string = 'view' ] ) : string
Parameters
$context : string = 'view'

What the value is for. Valid values are 'view' and 'edit'.

Return values
string

get_total()

Get total.

public get_total( [ $context : string = 'view' ] ) : string
Parameters
$context : string = 'view'

What the value is for. Valid values are 'view' and 'edit'.

Return values
string

get_total_tax()

Get total tax.

public get_total_tax( [ $context : string = 'view' ] ) : string
Parameters
$context : string = 'view'

What the value is for. Valid values are 'view' and 'edit'.

Return values
string

get_taxes()

Get taxes.

public get_taxes( [ $context : string = 'view' ] ) : array
Parameters
$context : string = 'view'

What the value is for. Valid values are 'view' and 'edit'.

Return values
array

get_item_download_url()

Get the Download URL.

public get_item_download_url( $download_id : int ) : string
Parameters
$download_id : int

Download ID.

Return values
string

get_item_downloads()

Get any associated downloadable files.

public get_item_downloads( ) : array
Return values
array

offsetGet()

OffsetGet for ArrayAccess/Backwards compatibility.

public offsetGet( $offset : string ) : mixed
Parameters
$offset : string

Offset.

Return values
mixed

offsetSet()

OffsetSet for ArrayAccess/Backwards compatibility.

public offsetSet( $offset : string , $value : mixed ) : mixed
Parameters
$offset : string

Offset.

$value : mixed

Value.

Tags
deprecated
Return values
mixed

offsetExists()

OffsetExists for ArrayAccess.

public offsetExists( $offset : string ) : bool
Parameters
$offset : string

Offset.

Return values
bool

__construct()

Default constructor.

public __construct( $read : int|object|array ) : mixed
Parameters
$read : int|object|array

ID to load from the DB (optional) or already queried data.

Return values
mixed

apply_changes()

Merge changes with data and clear.

public apply_changes( ) : mixed
Tags
since
Return values
mixed

get_order_id()

Get order ID this meta belongs to.

public get_order_id( [ $context : string = 'view' ] ) : int
Parameters
$context : string = 'view'

What the value is for. Valid values are 'view' and 'edit'.

Return values
int

get_name()

Get order item name.

public get_name( [ $context : string = 'view' ] ) : string
Parameters
$context : string = 'view'

What the value is for. Valid values are 'view' and 'edit'.

Return values
string

get_type()

Get order item type. Overridden by child classes.

public get_type( ) : string
Return values
string

get_tax_status()

Get tax status.

public get_tax_status( ) : string
Return values
string

get_tax_class()

Get tax class.

public get_tax_class( ) : string
Return values
string

set_order_id()

Set order ID.

public set_order_id( $value : int ) : mixed
Parameters
$value : int

Order ID.

Return values
mixed

set_name()

Set order item name.

public set_name( $value : string ) : mixed
Parameters
$value : string

Item name.

Return values
mixed

is_type()

Type checking.

public is_type( $type : string|array ) : bool
Parameters
$type : string|array

Type.

Return values
bool

calculate_taxes()

Calculate item taxes.

public calculate_taxes( [ $calculate_tax_for : array = array() ] ) : bool
Parameters
$calculate_tax_for : array = array()

Location data to get taxes for. Required.

Tags
since
Return values
bool

True if taxes were calculated.

get_formatted_meta_data()

Expands things like term slugs before return.

public get_formatted_meta_data( [ $hideprefix : string = '_' ] [, $include_all : bool = false ] ) : array
Parameters
$hideprefix : string = '_'

Meta data prefix, (default: _).

$include_all : bool = false

Include all meta data, this stop skip items with values already in the product name.

Return values
array

offsetSet()

OffsetSet for ArrayAccess.

public offsetSet( $offset : string , $value : mixed ) : mixed
Parameters
$offset : string

Offset.

$value : mixed

Value.

Return values
mixed

offsetUnset()

OffsetUnset for ArrayAccess.

public offsetUnset( $offset : string ) : mixed
Parameters
$offset : string

Offset.

Return values
mixed

offsetExists()

OffsetExists for ArrayAccess.

public offsetExists( $offset : string ) : bool
Parameters
$offset : string

Offset.

Return values
bool

offsetGet()

OffsetGet for ArrayAccess.

public offsetGet( $offset : string ) : mixed
Parameters
$offset : string

Offset.

Return values
mixed

__sleep()

Only store the object ID to avoid serializing the data object instance.

public __sleep( ) : array
Return values
array

__wakeup()

Re-run the constructor with the object ID.

public __wakeup( ) : mixed

If the object no longer exists, remove the ID.

Return values
mixed

__clone()

When the object is cloned, make sure meta is duplicated correctly.

public __clone( ) : mixed
Tags
since
Return values
mixed

get_data_store()

Get the data store.

public get_data_store( ) : object
Tags
since
Return values
object

get_id()

Returns the unique ID for this object.

public get_id( ) : int
Tags
since
Return values
int

delete()

Delete an object, set the ID to 0, and return result.

public delete( [ $force_delete : bool = false ] ) : bool
Parameters
$force_delete : bool = false

Should the date be deleted permanently.

Tags
since
Return values
bool

result

save()

Save should create or update based on object existence.

public save( ) : int
Tags
since
Return values
int

__toString()

Change data to JSON format.

public __toString( ) : string
Tags
since
Return values
string

Data in JSON format.

get_data()

Returns all data for this object.

public get_data( ) : array
Tags
since
Return values
array

get_data_keys()

Returns array of expected data keys for this object.

public get_data_keys( ) : array
Tags
since
Return values
array

get_extra_data_keys()

Returns all "extra" data keys for an object (for sub objects like product types).

public get_extra_data_keys( ) : array
Tags
since
Return values
array

filter_null_meta()

Filter null meta values from array.

protected filter_null_meta( $meta : mixed ) : bool
Parameters
$meta : mixed

Meta value to check.

Tags
since
Return values
bool

get_meta_data()

Get All Meta Data.

public get_meta_data( ) : array
Tags
since
Return values
array

of objects.

is_internal_meta_key()

Check if the key is an internal one.

protected is_internal_meta_key( $key : string ) : bool
Parameters
$key : string

Key to check.

Tags
since
Return values
bool

true if it's an internal key, false otherwise

get_meta()

Get Meta Data by Key.

public get_meta( [ $key : string = '' ] [, $single : bool = true ] [, $context : string = 'view' ] ) : mixed
Parameters
$key : string = ''

Meta Key.

$single : bool = true

return first found meta with key, or all with $key.

$context : string = 'view'

What the value is for. Valid values are view and edit.

Tags
since
Return values
mixed

meta_exists()

See if meta data exists, since get_meta always returns a '' or array().

public meta_exists( [ $key : string = '' ] ) : bool
Parameters
$key : string = ''

Meta Key.

Tags
since
Return values
bool

set_meta_data()

Set all meta data from array.

public set_meta_data( $data : array ) : mixed
Parameters
$data : array

Key/Value pairs.

Tags
since
Return values
mixed

add_meta_data()

Add meta data.

public add_meta_data( $key : string , $value : string|array [, $unique : bool = false ] ) : mixed
Parameters
$key : string

Meta key.

$value : string|array

Meta value.

$unique : bool = false

Should this be a unique key?.

Tags
since
Return values
mixed

update_meta_data()

Update meta data by key or ID, if provided.

public update_meta_data( $key : string , $value : string|array , $meta_id : int ) : mixed
Parameters
$key : string

Meta key.

$value : string|array

Meta value.

$meta_id : int

Meta ID.

Tags
since
Return values
mixed

delete_meta_data()

Delete meta data.

public delete_meta_data( $key : string ) : mixed
Parameters
$key : string

Meta key.

Tags
since
Return values
mixed

delete_meta_data_by_mid()

Delete meta data.

public delete_meta_data_by_mid( $mid : int ) : mixed
Parameters
$mid : int

Meta ID.

Tags
since
Return values
mixed

maybe_read_meta_data()

Read meta data if null.

protected maybe_read_meta_data( ) : mixed
Tags
since
Return values
mixed

read_meta_data()

Read Meta Data from the database. Ignore any internal properties.

public read_meta_data( [ $force_read : bool = false ] ) : mixed

Uses it's own caches because get_metadata does not provide meta_ids.

Parameters
$force_read : bool = false

True to force a new DB read (and update cache).

Tags
since
Return values
mixed

save_meta_data()

Update Meta Data in the database.

public save_meta_data( ) : mixed
Tags
since
Return values
mixed

set_id()

Set ID.

public set_id( $id : int ) : mixed
Parameters
$id : int

ID.

Tags
since
Return values
mixed

set_defaults()

Set all props to default values.

public set_defaults( ) : mixed
Tags
since
Return values
mixed

set_object_read()

Set object read property.

public set_object_read( [ $read : bool = true ] ) : mixed
Parameters
$read : bool = true

Should read?.

Tags
since
Return values
mixed

get_object_read()

Get object read property.

public get_object_read( ) : bool
Tags
since
Return values
bool

set_props()

Set a collection of props in one go, collect any errors, and return the result.

public set_props( $props : array [, $context : string = 'set' ] ) : bool|WP_Error

Only sets using public methods.

Parameters
$props : array

Key value pairs to set. Key is the prop and should map to a setter function name.

$context : string = 'set'

In what context to run this.

Tags
since
Return values
bool|WP_Error

set_prop()

Sets a prop for a setter method.

protected set_prop( $prop : string , $value : mixed ) : mixed

This stores changes in a special array so we can track what needs saving the the DB later.

Parameters
$prop : string

Name of prop to set.

$value : mixed

Value of the prop.

Tags
since
Return values
mixed

get_changes()

Return data changes only.

public get_changes( ) : array
Tags
since
Return values
array

get_hook_prefix()

Prefix for action and filter hooks on data.

protected get_hook_prefix( ) : string
Tags
since
Return values
string

get_prop()

Gets a prop for a getter method.

protected get_prop( $prop : string [, $context : string = 'view' ] ) : mixed

Gets the value from either current pending changes, or the data itself. Context controls what happens to the value before it's returned.

Parameters
$prop : string

Name of prop to get.

$context : string = 'view'

What the value is for. Valid values are view and edit.

Tags
since
Return values
mixed

set_date_prop()

Sets a date prop whilst handling formatting and datetime objects.

protected set_date_prop( $prop : string , $value : string|int ) : mixed
Parameters
$prop : string

Name of prop to set.

$value : string|int

Value of the prop.

Tags
since
Return values
mixed

error()

When invalid data is found, throw an exception unless reading from the DB.

protected error( $code : string , $message : string [, $http_status_code : int = 400 ] [, $data : array = array() ] ) : mixed
Parameters
$code : string

Error code.

$message : string

Error message.

$http_status_code : int = 400

HTTP status code.

$data : array = array()

Extra error data.

Tags
throws

Data Exception.

since
Return values
mixed
Back to the top