WooCommerce Code Reference

Search results

WC_Coupon extends WC_Legacy_Coupon

Coupon class.

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

Table of Contents

E_WC_COUPON_INVALID_FILTERED 100
E_WC_COUPON_INVALID_REMOVED 101
E_WC_COUPON_NOT_YOURS_REMOVED 102
E_WC_COUPON_ALREADY_APPLIED 103
E_WC_COUPON_ALREADY_APPLIED_INDIV_USE_ONLY 104
E_WC_COUPON_NOT_EXIST 105
E_WC_COUPON_USAGE_LIMIT_REACHED 106
E_WC_COUPON_EXPIRED 107
E_WC_COUPON_MIN_SPEND_LIMIT_NOT_MET 108
E_WC_COUPON_NOT_APPLICABLE 109
E_WC_COUPON_NOT_VALID_SALE_ITEMS 110
E_WC_COUPON_PLEASE_ENTER 111
E_WC_COUPON_MAX_SPEND_LIMIT_MET 112
E_WC_COUPON_EXCLUDED_PRODUCTS 113
E_WC_COUPON_EXCLUDED_CATEGORIES 114
WC_COUPON_SUCCESS 200
WC_COUPON_REMOVED 201
$data Data array, with defaults. array
$cache_group Cache group. string
$id ID for this object. int
$data Core data for this object. Name value pairs (name + default value). array
$changes Core data changes for this object. array
$object_read This is false until the object is read from the DB. bool
$object_type This is the name of this object type. string
$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
$cache_group Stores meta in cache for future reads. string
$meta_data Stores additional meta data. array
__construct() Coupon constructor. Loads coupon data. mixed
read_object_from_database() If the object has an ID, read using the data store. mixed
is_type() Checks the coupon type. bool
get_hook_prefix() Prefix for action and filter hooks on data. string
get_code() Get coupon code. string
get_description() Get coupon description. string
get_discount_type() Get discount type. string
get_amount() Get coupon amount. float
get_date_expires() Get coupon expiration date. WC_DateTime|null
get_date_created() Get date_created WC_DateTime|null
get_date_modified() Get date_modified WC_DateTime|null
get_usage_count() Get coupon usage count. int
get_individual_use() Get the "indvidual use" checkbox status. bool
get_product_ids() Get product IDs this coupon can apply to. array
get_excluded_product_ids() Get product IDs that this coupon should not apply to. array
get_usage_limit() Get coupon usage limit. int
get_usage_limit_per_user() Get coupon usage limit per customer (for a single customer) int
get_limit_usage_to_x_items() Usage limited to certain amount of items int|null
get_free_shipping() If this coupon grants free shipping or not. bool
get_product_categories() Get product categories this coupon can apply to. array
get_excluded_product_categories() Get product categories this coupon cannot not apply to. array
get_exclude_sale_items() If this coupon should exclude items on sale. bool
get_minimum_amount() Get minimum spend amount. float
get_maximum_amount() Get maximum spend amount. float
get_email_restrictions() Get emails to check customer usage restrictions. array
get_used_by() Get records of all users who have used the current coupon. array
get_virtual() If the filter is added through the woocommerce_get_shop_coupon_data filter, it's virtual and not in the DB. bool
get_discount_amount() Get discount amount for a cart item. float
set_code() Set coupon code. mixed
set_description() Set coupon description. mixed
set_discount_type() Set discount type. mixed
set_amount() Set amount. mixed
set_date_expires() Set expiration date. mixed
set_date_created() Set date_created mixed
set_date_modified() Set date_modified mixed
set_usage_count() Set how many times this coupon has been used. mixed
set_individual_use() Set if this coupon can only be used once. mixed
set_product_ids() Set the product IDs this coupon can be used with. mixed
set_excluded_product_ids() Set the product IDs this coupon cannot be used with. mixed
set_usage_limit() Set the amount of times this coupon can be used. mixed
set_usage_limit_per_user() Set the amount of times this coupon can be used per user. mixed
set_limit_usage_to_x_items() Set usage limit to x number of items. mixed
set_free_shipping() Set if this coupon enables free shipping or not. mixed
set_product_categories() Set the product category IDs this coupon can be used with. mixed
set_excluded_product_categories() Set the product category IDs this coupon cannot be used with. mixed
set_exclude_sale_items() Set if this coupon should excluded sale items or not. mixed
set_minimum_amount() Set the minimum spend amount. mixed
set_maximum_amount() Set the maximum spend amount. mixed
set_email_restrictions() Set email restrictions. mixed
set_used_by() Set which users have used this coupon. mixed
set_virtual() Set coupon virtual state. mixed
read_manual_coupon() Developers can programmatically return coupons. This function will read those values into our WC_Coupon class. mixed
increase_usage_count() Increase usage count for current coupon. mixed
decrease_usage_count() Decrease usage count for current coupon. mixed
get_error_message() Returns the error_message string. string
is_valid() Check if a coupon is valid for the cart. bool
is_valid_for_cart() Check if a coupon is valid. bool
is_valid_for_product() Check if a coupon is valid for a product. bool
add_coupon_message() Converts one of the WC_Coupon message/error codes to a message string and. mixed
get_coupon_message() Map one of the WC_Coupon message codes to a message string. string
get_coupon_error() Map one of the WC_Coupon error codes to a message string. string
get_generic_coupon_error() Map one of the WC_Coupon error codes to an error string. string
__isset() Magic __isset method for backwards compatibility. Legacy properties which could be accessed directly in the past. bool
__get() Magic __get method for backwards compatibility. Maps legacy vars to new getters. mixed
format_array() Format loaded data as array. array
apply_before_tax() Check if coupon needs applying before tax. bool
enable_free_shipping() Check if a coupon enables free shipping. bool
exclude_sale_items() Check if a coupon excludes sale items. bool
inc_usage_count() Increase usage count for current coupon. mixed
dcr_usage_count() Decrease usage count for current coupon. mixed
__construct() Default constructor. 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
apply_changes() Merge changes with data and clear. mixed
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

Constants

E_WC_COUPON_INVALID_FILTERED

mixed $E_WC_COUPON_INVALID_FILTERED = 100

E_WC_COUPON_INVALID_REMOVED

mixed $E_WC_COUPON_INVALID_REMOVED = 101

E_WC_COUPON_NOT_YOURS_REMOVED

mixed $E_WC_COUPON_NOT_YOURS_REMOVED = 102

E_WC_COUPON_ALREADY_APPLIED

mixed $E_WC_COUPON_ALREADY_APPLIED = 103

E_WC_COUPON_ALREADY_APPLIED_INDIV_USE_ONLY

mixed $E_WC_COUPON_ALREADY_APPLIED_INDIV_USE_ONLY = 104

E_WC_COUPON_USAGE_LIMIT_REACHED

mixed $E_WC_COUPON_USAGE_LIMIT_REACHED = 106

E_WC_COUPON_MIN_SPEND_LIMIT_NOT_MET

mixed $E_WC_COUPON_MIN_SPEND_LIMIT_NOT_MET = 108

E_WC_COUPON_NOT_VALID_SALE_ITEMS

mixed $E_WC_COUPON_NOT_VALID_SALE_ITEMS = 110

E_WC_COUPON_MAX_SPEND_LIMIT_MET

mixed $E_WC_COUPON_MAX_SPEND_LIMIT_MET = 112

E_WC_COUPON_EXCLUDED_PRODUCTS

mixed $E_WC_COUPON_EXCLUDED_PRODUCTS = 113

E_WC_COUPON_EXCLUDED_CATEGORIES

mixed $E_WC_COUPON_EXCLUDED_CATEGORIES = 114

Properties

$data

Data array, with defaults.

protected array $data = array('code' => '', 'amount' => 0, 'date_created' => \null, 'date_modified' => \null, 'date_expires' => \null, 'discount_type' => 'fixed_cart', 'description' => '', 'usage_count' => 0, 'individual_use' => \false, 'product_ids' => array(), 'excluded_product_ids' => array(), 'usage_limit' => 0, 'usage_limit_per_user' => 0, 'limit_usage_to_x_items' => \null, 'free_shipping' => \false, 'product_categories' => array(), 'excluded_product_categories' => array(), 'exclude_sale_items' => \false, 'minimum_amount' => '', 'maximum_amount' => '', 'email_restrictions' => array(), 'used_by' => array(), 'virtual' => \false)

$data

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

protected array $data = array()

$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

$object_type

This is the name of this object type.

protected string $object_type = 'data'

$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

$cache_group

Stores meta in cache for future reads.

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

$meta_data

Stores additional meta data.

protected array $meta_data = \null

Methods

__construct()

Coupon constructor. Loads coupon data.

public __construct( [ $data : mixed = '' ] ) : mixed
Parameters
$data : mixed = ''

Coupon data, object, ID or code.

Return values
mixed

read_object_from_database()

If the object has an ID, read using the data store.

protected read_object_from_database( ) : mixed
Tags
since
Return values
mixed

is_type()

Checks the coupon type.

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

Array or string of types.

Return values
bool

get_hook_prefix()

Prefix for action and filter hooks on data.

protected get_hook_prefix( ) : string
Tags
since
Return values
string

get_code()

Get coupon code.

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

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

Tags
since
Return values
string

get_description()

Get coupon description.

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

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

Tags
since
Return values
string

get_discount_type()

Get discount type.

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

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

Tags
since
Return values
string

get_amount()

Get coupon amount.

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

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

Tags
since
Return values
float

get_date_expires()

Get coupon expiration date.

public get_date_expires( [ $context : string = 'view' ] ) : WC_DateTime|null
Parameters
$context : string = 'view'

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

Tags
since
Return values
WC_DateTime|null

object if the date is set or null if there is no date.

get_date_created()

Get date_created

public get_date_created( [ $context : string = 'view' ] ) : WC_DateTime|null
Parameters
$context : string = 'view'

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

Tags
since
Return values
WC_DateTime|null

object if the date is set or null if there is no date.

get_date_modified()

Get date_modified

public get_date_modified( [ $context : string = 'view' ] ) : WC_DateTime|null
Parameters
$context : string = 'view'

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

Tags
since
Return values
WC_DateTime|null

object if the date is set or null if there is no date.

get_usage_count()

Get coupon usage count.

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

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

Tags
since
Return values
int

get_individual_use()

Get the "indvidual use" checkbox status.

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

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

Tags
since
Return values
bool

get_product_ids()

Get product IDs this coupon can apply to.

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

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

Tags
since
Return values
array

get_excluded_product_ids()

Get product IDs that this coupon should not apply to.

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

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

Tags
since
Return values
array

get_usage_limit()

Get coupon usage limit.

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

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

Tags
since
Return values
int

get_usage_limit_per_user()

Get coupon usage limit per customer (for a single customer)

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

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

Tags
since
Return values
int

get_limit_usage_to_x_items()

Usage limited to certain amount of items

public get_limit_usage_to_x_items( [ $context : string = 'view' ] ) : int|null
Parameters
$context : string = 'view'

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

Tags
since
Return values
int|null

get_free_shipping()

If this coupon grants free shipping or not.

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

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

Tags
since
Return values
bool

get_product_categories()

Get product categories this coupon can apply to.

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

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

Tags
since
Return values
array

get_excluded_product_categories()

Get product categories this coupon cannot not apply to.

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

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

Tags
since
Return values
array

get_exclude_sale_items()

If this coupon should exclude items on sale.

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

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

Tags
since
Return values
bool

get_minimum_amount()

Get minimum spend amount.

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

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

Tags
since
Return values
float

get_maximum_amount()

Get maximum spend amount.

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

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

Tags
since
Return values
float

get_email_restrictions()

Get emails to check customer usage restrictions.

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

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

Tags
since
Return values
array

get_used_by()

Get records of all users who have used the current coupon.

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

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

Tags
since
Return values
array

get_virtual()

If the filter is added through the woocommerce_get_shop_coupon_data filter, it's virtual and not in the DB.

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

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

Tags
since
Return values
bool

get_discount_amount()

Get discount amount for a cart item.

public get_discount_amount( $discounting_amount : float [, $cart_item : array|null = null ] [, $single : bool = false ] ) : float
Parameters
$discounting_amount : float

Amount the coupon is being applied to.

$cart_item : array|null = null

Cart item being discounted if applicable.

$single : bool = false

True if discounting a single qty item, false if its the line.

Return values
float

Amount this coupon has discounted.

set_code()

Set coupon code.

public set_code( $code : string ) : mixed
Parameters
$code : string

Coupon code.

Tags
since
Return values
mixed

set_description()

Set coupon description.

public set_description( $description : string ) : mixed
Parameters
$description : string

Description.

Tags
since
Return values
mixed

set_discount_type()

Set discount type.

public set_discount_type( $discount_type : string ) : mixed
Parameters
$discount_type : string

Discount type.

Tags
since
Return values
mixed

set_amount()

Set amount.

public set_amount( $amount : float ) : mixed
Parameters
$amount : float

Amount.

Tags
since
Return values
mixed

set_date_expires()

Set expiration date.

public set_date_expires( $date : string|int|null ) : mixed
Parameters
$date : string|int|null

UTC timestamp, or ISO 8601 DateTime. If the DateTime string has no timezone or offset, WordPress site timezone will be assumed. Null if there is no date.

Tags
since
Return values
mixed

set_date_created()

Set date_created

public set_date_created( $date : string|int|null ) : mixed
Parameters
$date : string|int|null

UTC timestamp, or ISO 8601 DateTime. If the DateTime string has no timezone or offset, WordPress site timezone will be assumed. Null if there is no date.

Tags
since
Return values
mixed

set_date_modified()

Set date_modified

public set_date_modified( $date : string|int|null ) : mixed
Parameters
$date : string|int|null

UTC timestamp, or ISO 8601 DateTime. If the DateTime string has no timezone or offset, WordPress site timezone will be assumed. Null if there is no date.

Tags
since
Return values
mixed

set_usage_count()

Set how many times this coupon has been used.

public set_usage_count( $usage_count : int ) : mixed
Parameters
$usage_count : int

Usage count.

Tags
since
Return values
mixed

set_individual_use()

Set if this coupon can only be used once.

public set_individual_use( $is_individual_use : bool ) : mixed
Parameters
$is_individual_use : bool

If is for individual use.

Tags
since
Return values
mixed

set_product_ids()

Set the product IDs this coupon can be used with.

public set_product_ids( $product_ids : array ) : mixed
Parameters
$product_ids : array

Products IDs.

Tags
since
Return values
mixed

set_excluded_product_ids()

Set the product IDs this coupon cannot be used with.

public set_excluded_product_ids( $excluded_product_ids : array ) : mixed
Parameters
$excluded_product_ids : array

Exclude product IDs.

Tags
since
Return values
mixed

set_usage_limit()

Set the amount of times this coupon can be used.

public set_usage_limit( $usage_limit : int ) : mixed
Parameters
$usage_limit : int

Usage limit.

Tags
since
Return values
mixed

set_usage_limit_per_user()

Set the amount of times this coupon can be used per user.

public set_usage_limit_per_user( $usage_limit : int ) : mixed
Parameters
$usage_limit : int

Usage limit.

Tags
since
Return values
mixed

set_limit_usage_to_x_items()

Set usage limit to x number of items.

public set_limit_usage_to_x_items( $limit_usage_to_x_items : int|null ) : mixed
Parameters
$limit_usage_to_x_items : int|null

Limit usage to X items.

Tags
since
Return values
mixed

set_free_shipping()

Set if this coupon enables free shipping or not.

public set_free_shipping( $free_shipping : bool ) : mixed
Parameters
$free_shipping : bool

If grant free shipping.

Tags
since
Return values
mixed

set_product_categories()

Set the product category IDs this coupon can be used with.

public set_product_categories( $product_categories : array ) : mixed
Parameters
$product_categories : array

List of product categories.

Tags
since
Return values
mixed

set_excluded_product_categories()

Set the product category IDs this coupon cannot be used with.

public set_excluded_product_categories( $excluded_product_categories : array ) : mixed
Parameters
$excluded_product_categories : array

List of excluded product categories.

Tags
since
Return values
mixed

set_exclude_sale_items()

Set if this coupon should excluded sale items or not.

public set_exclude_sale_items( $exclude_sale_items : bool ) : mixed
Parameters
$exclude_sale_items : bool

If should exclude sale items.

Tags
since
Return values
mixed

set_minimum_amount()

Set the minimum spend amount.

public set_minimum_amount( $amount : float ) : mixed
Parameters
$amount : float

Minium amount.

Tags
since
Return values
mixed

set_maximum_amount()

Set the maximum spend amount.

public set_maximum_amount( $amount : float ) : mixed
Parameters
$amount : float

Maximum amount.

Tags
since
Return values
mixed

set_email_restrictions()

Set email restrictions.

public set_email_restrictions( [ $emails : array = array() ] ) : mixed
Parameters
$emails : array = array()

List of emails.

Tags
since
Return values
mixed

set_used_by()

Set which users have used this coupon.

public set_used_by( $used_by : array ) : mixed
Parameters
$used_by : array

List of user IDs.

Tags
since
Return values
mixed

set_virtual()

Set coupon virtual state.

public set_virtual( $virtual : bool ) : mixed
Parameters
$virtual : bool

Whether it is virtual or not.

Tags
since
Return values
mixed

read_manual_coupon()

Developers can programmatically return coupons. This function will read those values into our WC_Coupon class.

public read_manual_coupon( $code : string , $coupon : array ) : mixed
Parameters
$code : string

Coupon code.

$coupon : array

Array of coupon properties.

Tags
since
Return values
mixed

increase_usage_count()

Increase usage count for current coupon.

public increase_usage_count( [ $used_by : string = '' ] [, $order : WC_Order = null ] ) : mixed
Parameters
$used_by : string = ''

Either user ID or billing email.

$order : WC_Order = null

If provided, will clear the coupons held by this order.

Return values
mixed

decrease_usage_count()

Decrease usage count for current coupon.

public decrease_usage_count( [ $used_by : string = '' ] ) : mixed
Parameters
$used_by : string = ''

Either user ID or billing email.

Return values
mixed

get_error_message()

Returns the error_message string.

public get_error_message( ) : string
Return values
string

is_valid()

Check if a coupon is valid for the cart.

public is_valid( ) : bool
Tags
deprecated

In favor of WC_Discounts->is_coupon_valid.

Return values
bool

is_valid_for_cart()

Check if a coupon is valid.

public is_valid_for_cart( ) : bool
Return values
bool

is_valid_for_product()

Check if a coupon is valid for a product.

public is_valid_for_product( $product : WC_Product [, $values : array = array() ] ) : bool
Parameters
$product : WC_Product

Product instance.

$values : array = array()

Values.

Return values
bool

add_coupon_message()

Converts one of the WC_Coupon message/error codes to a message string and.

public add_coupon_message( $msg_code : int ) : mixed

displays the message/error.

Parameters
$msg_code : int

Message/error code.

Return values
mixed

get_coupon_message()

Map one of the WC_Coupon message codes to a message string.

public get_coupon_message( $msg_code : int ) : string
Parameters
$msg_code : int

Message code.

Return values
string

Message/error string.

get_coupon_error()

Map one of the WC_Coupon error codes to a message string.

public get_coupon_error( $err_code : int ) : string
Parameters
$err_code : int

Message/error code.

Return values
string

Message/error string

get_generic_coupon_error()

Map one of the WC_Coupon error codes to an error string.

public static get_generic_coupon_error( $err_code : int ) : string

No coupon instance will be available where a coupon does not exist, so this static method exists.

Parameters
$err_code : int

Error code.

Return values
string

Error string.

__isset()

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

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

__get()

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

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

format_array()

Format loaded data as array.

public format_array( $array : string|array ) : array
Parameters
$array : string|array
Return values
array

apply_before_tax()

Check if coupon needs applying before tax.

public apply_before_tax( ) : bool
Return values
bool

enable_free_shipping()

Check if a coupon enables free shipping.

public enable_free_shipping( ) : bool
Return values
bool

exclude_sale_items()

Check if a coupon excludes sale items.

public exclude_sale_items( ) : bool
Return values
bool

inc_usage_count()

Increase usage count for current coupon.

public inc_usage_count( [ $used_by : string = '' ] ) : mixed
Parameters
$used_by : string = ''

Either user ID or billing email

Return values
mixed

dcr_usage_count()

Decrease usage count for current coupon.

public dcr_usage_count( [ $used_by : string = '' ] ) : mixed
Parameters
$used_by : string = ''

Either user ID or billing email

Return values
mixed

__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

__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

apply_changes()

Merge changes with data and clear.

public apply_changes( ) : mixed
Tags
since
Return values
mixed

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