PHP Class WC_Coupon

The WooCommerce coupons class gets coupon data from storage and checks coupon validity.
Author: WooThemes
Show file Open project: woocommerce/woocommerce Class Usage Examples

Protected Properties

Property Type Description
$cache_group string Cache group.
$data array Data array, with defaults.

Public Methods

Method Description
__construct ( mixed $data = '' ) Coupon constructor. Loads coupon data.
add_coupon_message ( integer $msg_code ) Converts one of the WC_Coupon message/error codes to a message string and.
decrease_usage_count ( string $used_by = '' ) Decrease usage count for current coupon.
get_amount ( string $context = 'view' ) : float Get coupon code.
get_code ( string $context = 'view' ) : string Get coupon code.
get_coupon_error ( integer $err_code ) : string Map one of the WC_Coupon error codes to a message string.
get_coupon_message ( integer $msg_code ) : string Map one of the WC_Coupon message codes to a message string.
get_date_created ( string $context = 'view' ) : integer Get date_created
get_date_expires ( string $context = 'view' ) : integer Get coupon expiration date.
get_date_modified ( string $context = 'view' ) : integer Get date_modified
get_description ( string $context = 'view' ) : string Get coupon description.
get_discount_amount ( float $discounting_amount, array | null $cart_item = null, boolean $single = false ) : float Get discount amount for a cart item.
get_discount_type ( string $context = 'view' ) : string Get discount type.
get_email_restrictions ( string $context = 'view' ) : array Get emails to check customer usage restrictions.
get_error_message ( ) : string Returns the error_message string.
get_exclude_sale_items ( string $context = 'view' ) : boolean If this coupon should exclude items on sale.
get_excluded_product_categories ( string $context = 'view' ) : array Get product categories this coupon cannot not apply to.
get_excluded_product_ids ( string $context = 'view' ) : array Get product IDs that this coupon should not apply to.
get_free_shipping ( string $context = 'view' ) : boolean If this coupon grants free shipping or not.
get_generic_coupon_error ( integer $err_code ) : string | Error Map one of the WC_Coupon error codes to an error string.
get_individual_use ( string $context = 'view' ) : boolean Get the "indvidual use" checkbox status.
get_limit_usage_to_x_items ( string $context = 'view' ) : integer Usage limited to certain amount of items
get_maximum_amount ( string $context = 'view' ) : float Get maximum spend amount.
get_minimum_amount ( string $context = 'view' ) : float Get minium spend amount.
get_product_categories ( string $context = 'view' ) : array Get product categories this coupon can apply to.
get_product_ids ( string $context = 'view' ) : array Get product IDs this coupon can apply to.
get_usage_count ( string $context = 'view' ) : integer Get coupon usage count.
get_usage_limit ( string $context = 'view' ) : integer Get coupon usage limit.
get_usage_limit_per_user ( string $context = 'view' ) : integer Get coupon usage limit per customer (for a single customer)
get_used_by ( string $context = 'view' ) : array Get records of all users who have used the current coupon.
increase_usage_count ( string $used_by = '' ) Increase usage count for current coupon.
is_type ( string $type ) : boolean Checks the coupon type.
is_valid ( ) : boolean Check if a coupon is valid.
is_valid_for_cart ( ) : boolean Check if a coupon is valid.
is_valid_for_product ( WC_Product $product, $values = [] ) : boolean Check if a coupon is valid for a product.
read_manual_coupon ( string $code, array $coupon ) Developers can programically return coupons. This function will read those values into our WC_Coupon class.
set_amount ( float $amount ) Set amount.
set_code ( string $code ) Set coupon code.
set_date_created ( string $timestamp ) Set date_created
set_date_expires ( string $timestamp ) Set expiration date.
set_date_modified ( string $timestamp ) Set date_modified
set_description ( string $description ) Set coupon description.
set_discount_type ( string $discount_type ) Set discount type.
set_email_restrictions ( array $emails = [] ) Set email restrictions.
set_exclude_sale_items ( boolean $exclude_sale_items ) Set if this coupon should excluded sale items or not.
set_excluded_product_categories ( array $excluded_product_categories ) Set the product category IDs this coupon cannot be used with.
set_excluded_product_ids ( array $excluded_product_ids ) Set the product IDs this coupon cannot be used with.
set_free_shipping ( boolean $free_shipping ) Set if this coupon enables free shipping or not.
set_individual_use ( boolean $is_individual_use ) Set if this coupon can only be used once.
set_limit_usage_to_x_items ( integer $limit_usage_to_x_items ) Set usage limit to x number of items.
set_maximum_amount ( float $amount ) Set the maximum spend amount.
set_minimum_amount ( float $amount ) Set the minimum spend amount.
set_product_categories ( array $product_categories ) Set the product category IDs this coupon can be used with.
set_product_ids ( array $product_ids ) Set the product IDs this coupon can be used with.
set_usage_count ( integer $usage_count ) Set how many times this coupon has been used.
set_usage_limit ( integer $usage_limit ) Set the amount of times this coupon can be used.
set_usage_limit_per_user ( integer $usage_limit ) Set the amount of times this coupon can be used per user.
set_used_by ( array $used_by ) Set which users have used this coupon.

Protected Methods

Method Description
get_hook_prefix ( ) : string Prefix for action and filter hooks on data.

Private Methods

Method Description
validate_cart_excluded_items ( ) Cart discounts cannot be added if non-eligble product is found in cart.
validate_cart_excluded_product_categories ( ) Exclude categories from cart.
validate_cart_excluded_product_ids ( ) Exclude products from cart.
validate_cart_excluded_sale_items ( ) Exclude sale items from cart.
validate_excluded_items ( ) All exclusion rules must pass at the same time for a product coupon to be valid.
validate_exists ( ) Ensure coupon exists or throw exception.
validate_expiry_date ( ) Ensure coupon date is valid or throw exception.
validate_maximum_amount ( ) Ensure coupon amount is valid or throw exception.
validate_minimum_amount ( ) Ensure coupon amount is valid or throw exception.
validate_product_categories ( ) Ensure coupon is valid for product categories in the cart is valid or throw exception.
validate_product_ids ( ) Ensure coupon is valid for products in the cart is valid or throw exception.
validate_sale_items ( ) Ensure coupon is valid for sale items in the cart is valid or throw exception.
validate_usage_limit ( ) Ensure coupon usage limit is valid or throw exception.
validate_user_usage_limit ( integer $user_id ) Ensure coupon user usage limit is valid or throw exception.

Method Details

__construct() public method

Coupon constructor. Loads coupon data.
public __construct ( mixed $data = '' )
$data mixed Coupon data, object, ID or code.

add_coupon_message() public method

displays the message/error.
public add_coupon_message ( integer $msg_code )
$msg_code integer Message/error code.

decrease_usage_count() public method

Decrease usage count for current coupon.
public decrease_usage_count ( string $used_by = '' )
$used_by string Either user ID or billing email

get_amount() public method

Get coupon code.
Since: 2.7.0
public get_amount ( string $context = 'view' ) : float
$context string
return float

get_code() public method

Get coupon code.
Since: 2.7.0
public get_code ( string $context = 'view' ) : string
$context string
return string

get_coupon_error() public method

Map one of the WC_Coupon error codes to a message string.
public get_coupon_error ( integer $err_code ) : string
$err_code integer Message/error code.
return string

get_coupon_message() public method

Map one of the WC_Coupon message codes to a message string.
public get_coupon_message ( integer $msg_code ) : string
$msg_code integer
return string

get_date_created() public method

Get date_created
Since: 2.7.0
public get_date_created ( string $context = 'view' ) : integer
$context string
return integer

get_date_expires() public method

Get coupon expiration date.
Since: 2.7.0
public get_date_expires ( string $context = 'view' ) : integer
$context string
return integer

get_date_modified() public method

Get date_modified
Since: 2.7.0
public get_date_modified ( string $context = 'view' ) : integer
$context string
return integer

get_description() public method

Get coupon description.
Since: 2.7.0
public get_description ( string $context = 'view' ) : string
$context string
return string

get_discount_amount() public method

Get discount amount for a cart item.
public get_discount_amount ( float $discounting_amount, array | null $cart_item = null, boolean $single = false ) : float
$discounting_amount float Amount the coupon is being applied to
$cart_item array | null Cart item being discounted if applicable
$single boolean True if discounting a single qty item, false if its the line
return float Amount this coupon has discounted

get_discount_type() public method

Get discount type.
Since: 2.7.0
public get_discount_type ( string $context = 'view' ) : string
$context string
return string

get_email_restrictions() public method

Get emails to check customer usage restrictions.
Since: 2.7.0
public get_email_restrictions ( string $context = 'view' ) : array
$context string
return array

get_error_message() public method

Returns the error_message string.
public get_error_message ( ) : string
return string

get_exclude_sale_items() public method

If this coupon should exclude items on sale.
Since: 2.7.0
public get_exclude_sale_items ( string $context = 'view' ) : boolean
$context string
return boolean

get_excluded_product_categories() public method

Get product categories this coupon cannot not apply to.
Since: 2.7.0
public get_excluded_product_categories ( string $context = 'view' ) : array
$context string
return array

get_excluded_product_ids() public method

Get product IDs that this coupon should not apply to.
Since: 2.7.0
public get_excluded_product_ids ( string $context = 'view' ) : array
$context string
return array

get_free_shipping() public method

If this coupon grants free shipping or not.
Since: 2.7.0
public get_free_shipping ( string $context = 'view' ) : boolean
$context string
return boolean

get_generic_coupon_error() public static method

No coupon instance will be available where a coupon does not exist, so this static method exists.
public static get_generic_coupon_error ( integer $err_code ) : string | Error
$err_code integer Error code
return string | Error

get_hook_prefix() protected method

Prefix for action and filter hooks on data.
Since: 2.7.0
protected get_hook_prefix ( ) : string
return string

get_individual_use() public method

Get the "indvidual use" checkbox status.
Since: 2.7.0
public get_individual_use ( string $context = 'view' ) : boolean
$context string
return boolean

get_limit_usage_to_x_items() public method

Usage limited to certain amount of items
Since: 2.7.0
public get_limit_usage_to_x_items ( string $context = 'view' ) : integer
$context string
return integer

get_maximum_amount() public method

Get maximum spend amount.
Since: 2.7.0
public get_maximum_amount ( string $context = 'view' ) : float
$context string
return float

get_minimum_amount() public method

Get minium spend amount.
Since: 2.7.0
public get_minimum_amount ( string $context = 'view' ) : float
$context string
return float

get_product_categories() public method

Get product categories this coupon can apply to.
Since: 2.7.0
public get_product_categories ( string $context = 'view' ) : array
$context string
return array

get_product_ids() public method

Get product IDs this coupon can apply to.
Since: 2.7.0
public get_product_ids ( string $context = 'view' ) : array
$context string
return array

get_usage_count() public method

Get coupon usage count.
Since: 2.7.0
public get_usage_count ( string $context = 'view' ) : integer
$context string
return integer

get_usage_limit() public method

Get coupon usage limit.
Since: 2.7.0
public get_usage_limit ( string $context = 'view' ) : integer
$context string
return integer

get_usage_limit_per_user() public method

Get coupon usage limit per customer (for a single customer)
Since: 2.7.0
public get_usage_limit_per_user ( string $context = 'view' ) : integer
$context string
return integer

get_used_by() public method

Get records of all users who have used the current coupon.
Since: 2.7.0
public get_used_by ( string $context = 'view' ) : array
$context string
return array

increase_usage_count() public method

Increase usage count for current coupon.
public increase_usage_count ( string $used_by = '' )
$used_by string Either user ID or billing email

is_type() public method

Checks the coupon type.
public is_type ( string $type ) : boolean
$type string Array or string of types
return boolean

is_valid() public method

Check if a coupon is valid.
public is_valid ( ) : boolean
return boolean validity

is_valid_for_cart() public method

Check if a coupon is valid.
public is_valid_for_cart ( ) : boolean
return boolean

is_valid_for_product() public method

Check if a coupon is valid for a product.
public is_valid_for_product ( WC_Product $product, $values = [] ) : boolean
$product WC_Product
return boolean

read_manual_coupon() public method

Developers can programically return coupons. This function will read those values into our WC_Coupon class.
Since: 2.7.0
public read_manual_coupon ( string $code, array $coupon )
$code string Coupon code
$coupon array Array of coupon properties

set_amount() public method

Set amount.
Since: 2.7.0
public set_amount ( float $amount )
$amount float

set_code() public method

Set coupon code.
Since: 2.7.0
public set_code ( string $code )
$code string

set_date_created() public method

Set date_created
Since: 2.7.0
public set_date_created ( string $timestamp )
$timestamp string Timestamp

set_date_expires() public method

Set expiration date.
Since: 2.7.0
public set_date_expires ( string $timestamp )
$timestamp string Timestamp

set_date_modified() public method

Set date_modified
Since: 2.7.0
public set_date_modified ( string $timestamp )
$timestamp string

set_description() public method

Set coupon description.
Since: 2.7.0
public set_description ( string $description )
$description string

set_discount_type() public method

Set discount type.
Since: 2.7.0
public set_discount_type ( string $discount_type )
$discount_type string

set_email_restrictions() public method

Set email restrictions.
Since: 2.7.0
public set_email_restrictions ( array $emails = [] )
$emails array

set_exclude_sale_items() public method

Set if this coupon should excluded sale items or not.
Since: 2.7.0
public set_exclude_sale_items ( boolean $exclude_sale_items )
$exclude_sale_items boolean

set_excluded_product_categories() public method

Set the product category IDs this coupon cannot be used with.
Since: 2.7.0
public set_excluded_product_categories ( array $excluded_product_categories )
$excluded_product_categories array

set_excluded_product_ids() public method

Set the product IDs this coupon cannot be used with.
Since: 2.7.0
public set_excluded_product_ids ( array $excluded_product_ids )
$excluded_product_ids array

set_free_shipping() public method

Set if this coupon enables free shipping or not.
Since: 2.7.0
public set_free_shipping ( boolean $free_shipping )
$free_shipping boolean

set_individual_use() public method

Set if this coupon can only be used once.
Since: 2.7.0
public set_individual_use ( boolean $is_individual_use )
$is_individual_use boolean

set_limit_usage_to_x_items() public method

Set usage limit to x number of items.
Since: 2.7.0
public set_limit_usage_to_x_items ( integer $limit_usage_to_x_items )
$limit_usage_to_x_items integer

set_maximum_amount() public method

Set the maximum spend amount.
Since: 2.7.0
public set_maximum_amount ( float $amount )
$amount float

set_minimum_amount() public method

Set the minimum spend amount.
Since: 2.7.0
public set_minimum_amount ( float $amount )
$amount float

set_product_categories() public method

Set the product category IDs this coupon can be used with.
Since: 2.7.0
public set_product_categories ( array $product_categories )
$product_categories array

set_product_ids() public method

Set the product IDs this coupon can be used with.
Since: 2.7.0
public set_product_ids ( array $product_ids )
$product_ids array

set_usage_count() public method

Set how many times this coupon has been used.
Since: 2.7.0
public set_usage_count ( integer $usage_count )
$usage_count integer

set_usage_limit() public method

Set the amount of times this coupon can be used.
Since: 2.7.0
public set_usage_limit ( integer $usage_limit )
$usage_limit integer

set_usage_limit_per_user() public method

Set the amount of times this coupon can be used per user.
Since: 2.7.0
public set_usage_limit_per_user ( integer $usage_limit )
$usage_limit integer

set_used_by() public method

Set which users have used this coupon.
Since: 2.7.0
public set_used_by ( array $used_by )
$used_by array

Property Details

$cache_group protected property

Cache group.
protected string $cache_group
return string

$data protected property

Data array, with defaults.
Since: 2.7.0
protected array $data
return array