WooCommerce Code Reference

WC_Abstract_Legacy_Product extends WC_Data
in package

Legacy Abstract Product

Legacy and deprecated functions are here to keep the WC_Abstract_Product clean. This class will be removed in future versions.

Tags
version
3.0.0
category

Abstract Class

author

WooThemes

Table of Contents

$cache_group  : string
Stores meta in cache for future reads.
$changes  : array
Core data changes for this object.
$data  : array
Core data for this object. Name value pairs (name + default value).
$data_store  : object
Contains a reference to the data store for this class.
$default_data  : array
Set to _data on construct so we can track and reset data if needed.
$extra_data  : array
Extra data for this object. Name value pairs (name + default value).
$id  : int
ID for this object.
$meta_data  : array
Stores additional meta data.
$object_read  : bool
This is false until the object is read from the DB.
$object_type  : string
This is the name of this object type.
__clone()  : mixed
When the object is cloned, make sure meta is duplicated correctly.
__construct()  : mixed
Default constructor.
__get()  : mixed
Magic __get method for backwards compatibility. Maps legacy vars to new getters.
__isset()  : bool
Magic __isset method for backwards compatibility. Legacy properties which could be accessed directly in the past.
__sleep()  : array
Only store the object ID to avoid serializing the data object instance.
__toString()  : string
Change data to JSON format.
__wakeup()  : mixed
Re-run the constructor with the object ID.
add_meta_data()  : mixed
Add meta data.
adjust_price()  : mixed
Adjust a products price dynamically.
apply_changes()  : mixed
Merge changes with data and clear.
check_stock_status()  : mixed
Check if the stock status needs changing.
delete()  : bool
Delete an object, set the ID to 0, and return result.
delete_meta_data()  : mixed
Delete meta data.
delete_meta_data_by_mid()  : mixed
Delete meta data.
enable_dimensions_display()  : bool
Returns whether or not we are showing dimensions on the product page.
get_categories()  : string
Returns the product categories.
get_changes()  : array
Return data changes only.
get_child()  : WC_Product|WC_Product_variation
Returns the child product.
get_cross_sells()  : array
Returns the cross sell product ids.
get_data()  : array
Returns all data for this object.
get_data_keys()  : array
Returns array of expected data keys for this object.
get_data_store()  : object
Get the data store.
get_display_price()  : string
Returns the price including or excluding tax, based on the 'woocommerce_tax_display_shop' setting.
get_extra_data_keys()  : array
Returns all "extra" data keys for an object (for sub objects like product types).
get_files()  : array
Same as get_downloads in CRUD.
get_formatted_variation_attributes()  : string
Get formatted variation data with WC < 2.4 back compat and proper formatting of text-based attribute names.
get_gallery_attachment_ids()  : array
Returns the gallery attachment ids.
get_id()  : int
Returns the unique ID for this object.
get_matching_variation()  : mixed
Match a variation to a given set of attributes using a WP_Query.
get_meta()  : mixed
Get Meta Data by Key.
get_meta_data()  : array
Get All Meta Data.
get_object_read()  : bool
Get object read property.
get_parent()  : int
Get the parent of the post.
get_post_data()  : WP_Post
Get the product's post data.
get_price_excluding_tax()  : string
Returns the price (excluding tax) - ignores tax_class filters since the price may *include* tax and thus needs subtracting.
get_price_html_from_text()  : string
Functions for getting parts of a price, in html, used by get_price_html.
get_price_html_from_to()  : string
Functions for getting parts of a price, in html, used by get_price_html.
get_price_including_tax()  : string
Returns the price (including tax). Uses customer tax rates. Can work for a specific $qty for more accurate taxes.
get_rating_html()  : string
Returns the product rating in html format.
get_related()  : array
Get and return related products.
get_tags()  : array
Returns the product tags.
get_total_stock()  : int
Get total stock - This is the stock of parent and children combined.
get_upsells()  : array
Returns the upsell product ids.
get_variation_default_attributes()  : array
If set, get the default attributes for a variable product.
get_variation_description()  : string
Get product variation description.
get_variation_id()  : int
Get variation ID.
grouped_product_sync()  : mixed
has_all_attributes_set()  : bool
Check if all variation's attributes are set.
has_default_attributes()  : bool
Check if variable product has default attributes set.
increase_stock()  : int
Increase stock level of the product.
list_attributes()  : mixed
Lists a table of attributes for the product page.
meta_exists()  : bool
See if meta data exists, since get_meta always returns a '' or array().
parent_is_visible()  : bool
Returns whether or not the variations parent is visible.
read_meta_data()  : mixed
Read Meta Data from the database. Ignore any internal properties.
reduce_stock()  : int
Reduce stock level of the product.
save()  : int
Save should create or update based on object existence.
save_meta_data()  : mixed
Update Meta Data in the database.
set_defaults()  : mixed
Set all props to default values.
set_id()  : mixed
Set ID.
set_meta_data()  : mixed
Set all meta data from array.
set_object_read()  : mixed
Set object read property.
set_props()  : bool|WP_Error
Set a collection of props in one go, collect any errors, and return the result.
set_stock()  : int
Set stock level of the product.
sync_attributes()  : mixed
Sync the variable product's attributes with the variations.
sync_average_rating()  : mixed
Sync product rating. Can be called statically.
sync_rating_count()  : mixed
Sync product rating count. Can be called statically.
update_meta_data()  : mixed
Update meta data by key or ID, if provided.
variable_product_sync()  : mixed
Sync variable product prices with the children lowest/highest prices.
build_related_query()  : mixed
Builds the related posts query.
error()  : mixed
When invalid data is found, throw an exception unless reading from the DB.
filter_null_meta()  : bool
Filter null meta values from array.
get_hook_prefix()  : string
Prefix for action and filter hooks on data.
get_prop()  : mixed
Gets a prop for a getter method.
get_related_terms()  : array
Retrieves related product terms.
is_internal_meta_key()  : bool
Check if the key is an internal one.
maybe_read_meta_data()  : mixed
Read meta data if null.
set_date_prop()  : mixed
Sets a date prop whilst handling formatting and datetime objects.
set_prop()  : mixed
Sets a prop for a setter method.

Properties

$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.

Tags
since
3.0.0

Methods

__construct()

Default constructor.

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

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

Return values
mixed

__get()

Magic __get method for backwards compatibility. Maps legacy vars to new getters.

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

Key name.

Return values
mixed

__isset()

Magic __isset method for backwards compatibility. Legacy properties which could be accessed directly in the past.

public __isset(string $key) : bool
Parameters
$key : string

Key name.

Return values
bool

add_meta_data()

Add meta data.

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

Meta key.

$value : string|array

Meta value.

$unique : bool = false

Should this be a unique key?.

Tags
since
2.6.0
Return values
mixed

check_stock_status()

Check if the stock status needs changing.

public check_stock_status() : mixed
Tags
deprecated
3.0.0

Sync is done automatically on read/save, so calling this should not be needed any more.

Return values
mixed

delete()

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

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

Should the date be deleted permanently.

Tags
since
2.6.0
Return values
boolresult

delete_meta_data()

Delete meta data.

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

Meta key.

Tags
since
2.6.0
Return values
mixed

delete_meta_data_by_mid()

Delete meta data.

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

Meta ID.

Tags
since
2.6.0
Return values
mixed

enable_dimensions_display()

Returns whether or not we are showing dimensions on the product page.

public enable_dimensions_display() : bool
Tags
deprecated
3.0.0

Unused.

Return values
bool

get_categories()

Returns the product categories.

public get_categories([string $sep = ', ' ][, string $before = '' ][, string $after = '' ]) : string
Parameters
$sep : string = ', '

(default: ', ').

$before : string = ''

(default: '').

$after : string = ''

(default: '').

Tags
deprecated
3.0.0
Return values
string

get_display_price()

Returns the price including or excluding tax, based on the 'woocommerce_tax_display_shop' setting.

public get_display_price([string $price = '' ][, int $qty = 1 ]) : string
Parameters
$price : string = ''

to calculate, left blank to just use get_price()

$qty : int = 1

passed on to get_price_including_tax() or get_price_excluding_tax()

Tags
deprecated
3.0.0

Use wc_get_price_to_display instead.

Return values
string

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
3.0.0
Return values
array

get_formatted_variation_attributes()

Get formatted variation data with WC < 2.4 back compat and proper formatting of text-based attribute names.

public get_formatted_variation_attributes([bool $flat = false ]) : string
Parameters
$flat : bool = false
Tags
deprecated
3.0.0
Return values
string

get_matching_variation()

Match a variation to a given set of attributes using a WP_Query.

public get_matching_variation([array $match_attributes = array() ]) : mixed
Parameters
$match_attributes : array = array()
Tags
deprecated
3.0.0

in favour of Product data store's find_matching_product_variation.

Return values
mixed

get_meta()

Get Meta Data by Key.

public get_meta([string $key = '' ][, bool $single = true ][, string $context = '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
2.6.0
Return values
mixed

get_price_excluding_tax()

Returns the price (excluding tax) - ignores tax_class filters since the price may *include* tax and thus needs subtracting.

public get_price_excluding_tax([int $qty = 1 ][, string $price = '' ]) : string

Uses store base tax rates. Can work for a specific $qty for more accurate taxes.

Parameters
$qty : int = 1
$price : string = ''

to calculate, left blank to just use get_price()

Tags
deprecated
3.0.0

Use wc_get_price_excluding_tax instead.

Return values
string

get_price_html_from_text()

Functions for getting parts of a price, in html, used by get_price_html.

public get_price_html_from_text() : string
Tags
deprecated
3.0.0
Return values
string

get_price_html_from_to()

Functions for getting parts of a price, in html, used by get_price_html.

public get_price_html_from_to(string $from, mixed $to) : string
Parameters
$from : string

String or float to wrap with 'from' text

$to : mixed

String or float to wrap with 'to' text

Tags
deprecated
3.0.0

Use wc_format_sale_price instead.

Return values
string

get_price_including_tax()

Returns the price (including tax). Uses customer tax rates. Can work for a specific $qty for more accurate taxes.

public get_price_including_tax([int $qty = 1 ][, string $price = '' ]) : string
Parameters
$qty : int = 1
$price : string = ''

to calculate, left blank to just use get_price()

Tags
deprecated
3.0.0

Use wc_get_price_including_tax instead.

Return values
string

get_rating_html()

Returns the product rating in html format.

public get_rating_html([string $rating = null ]) : string
Parameters
$rating : string = null

(default: '')

Tags
deprecated
3.0.0
Return values
string

Get and return related products.

public get_related([int $limit = 5 ]) : array
Parameters
$limit : int = 5
Tags
deprecated
3.0.0

Use wc_get_related_products instead.

Return values
array

get_tags()

Returns the product tags.

public get_tags([string $sep = ', ' ][, string $before = '' ][, string $after = '' ]) : array
Parameters
$sep : string = ', '

(default: ', ').

$before : string = ''

(default: '').

$after : string = ''

(default: '').

Tags
deprecated
3.0.0
Return values
array

get_variation_default_attributes()

If set, get the default attributes for a variable product.

public get_variation_default_attributes() : array
Tags
deprecated
3.0.0
Return values
array

increase_stock()

Increase stock level of the product.

public increase_stock([int $amount = 1 ]) : int
Parameters
$amount : int = 1

Amount to increase by. Default 1.

Tags
deprecated
3.0.0
Return values
intnew stock level

list_attributes()

Lists a table of attributes for the product page.

public list_attributes() : mixed
Tags
deprecated
3.0.0

Use wc_display_product_attributes instead.

Return values
mixed

meta_exists()

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

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

Meta Key.

Tags
since
3.0.0
Return values
bool

read_meta_data()

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

public read_meta_data([bool $force_read = 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
2.6.0
Return values
mixed

reduce_stock()

Reduce stock level of the product.

public reduce_stock([int $amount = 1 ]) : int
Parameters
$amount : int = 1

Amount to reduce by. Default: 1

Tags
deprecated
3.0.0
Return values
intnew stock level

set_meta_data()

Set all meta data from array.

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

Key/Value pairs.

Tags
since
2.6.0
Return values
mixed

set_object_read()

Set object read property.

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

Should read?.

Tags
since
3.0.0
Return values
mixed

set_props()

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

public set_props(array $props[, string $context = '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
3.0.0
Return values
bool|WP_Error

set_stock()

Set stock level of the product.

public set_stock([int $amount = null ][, string $mode = 'set' ]) : int
Parameters
$amount : int = null
$mode : string = 'set'
Tags
deprecated
3.0.0
Return values
int

sync_attributes()

Sync the variable product's attributes with the variations.

public static sync_attributes( $product[, bool $children = false ]) : mixed
Parameters
$product :
$children : bool = false
Return values
mixed

sync_average_rating()

Sync product rating. Can be called statically.

public static sync_average_rating(int $post_id) : mixed
Parameters
$post_id : int
Tags
deprecated
3.0.0
Return values
mixed

sync_rating_count()

Sync product rating count. Can be called statically.

public static sync_rating_count(int $post_id) : mixed
Parameters
$post_id : int
Tags
deprecated
3.0.0
Return values
mixed

update_meta_data()

Update meta data by key or ID, if provided.

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

Meta key.

$value : string|array

Meta value.

$meta_id : int

Meta ID.

Tags
since
2.6.0
Return values
mixed

variable_product_sync()

Sync variable product prices with the children lowest/highest prices.

public variable_product_sync(int $product_id) : mixed
Parameters
$product_id : int
Tags
deprecated
3.0.0

not used in core.

Return values
mixed

Builds the related posts query.

protected build_related_query( $cats_array,  $tags_array,  $exclude_ids,  $limit) : mixed
Parameters
$cats_array :
$tags_array :
$exclude_ids :
$limit :
Tags
deprecated
3.0.0

Use Product Data Store get_related_products_query instead.

Return values
mixed

error()

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

protected error(string $code, string $message[, int $http_status_code = 400 ][, array $data = 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
WC_Data_Exception

Data Exception.

since
3.0.0
Return values
mixed

filter_null_meta()

Filter null meta values from array.

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

Meta value to check.

Tags
since
3.0.0
Return values
bool

get_prop()

Gets a prop for a getter method.

protected get_prop(string $prop[, string $context = '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
3.0.0
Return values
mixed

Retrieves related product terms.

protected get_related_terms( $term) : array
Parameters
$term :
Tags
deprecated
3.0.0

Use wc_get_product_term_ids instead.

Return values
array

is_internal_meta_key()

Check if the key is an internal one.

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

Key to check.

Tags
since
3.2.0
Return values
booltrue if it's an internal key, false otherwise

set_date_prop()

Sets a date prop whilst handling formatting and datetime objects.

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

Name of prop to set.

$value : string|int

Value of the prop.

Tags
since
3.0.0
Return values
mixed

set_prop()

Sets a prop for a setter method.

protected set_prop(string $prop, mixed $value) : 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
3.0.0
Return values
mixed