WooCommerce Code Reference

Search results

WC_Discounts

Discounts class.

Table of Contents

$object Reference to cart or order object. WC_Cart|WC_Order
$items An array of items to discount. array
$discounts An array of discounts which have been applied to items. array[]
__construct() WC_Discounts Constructor. mixed
set_items() Set items directly. Used by WC_Cart_Totals. mixed
set_items_from_cart() Normalise cart items which will be discounted. mixed
set_items_from_order() Normalise order items which will be discounted. mixed
get_object() Get the object concerned. object
get_items() Get items. object[]
get_items_to_validate() Get items to validate. object[]
get_discount() Get discount by key with or without precision. float
get_discounts() Get all discount totals. array
get_discounts_by_item() Get all discount totals per item. array
get_discounts_by_coupon() Get all discount totals per coupon. array
get_discounted_price() Get discounted price of an item without precision. float
get_discounted_price_in_cents() Get discounted price of an item to precision (in cents). int
apply_coupon() Apply a discount to all items using a coupon. bool|WP_Error
sort_by_price() Sort by price. int
filter_products_with_price() Filter out all products which have been fully discounted to 0. bool
get_items_to_apply_coupon() Get items which the coupon should be applied to. array
apply_coupon_percent() Apply percent discount to items and return an array of discounts granted. int
apply_coupon_fixed_product() Apply fixed product discount to items. int
apply_coupon_fixed_cart() Apply fixed cart discount to items. int
apply_coupon_custom() Apply custom coupon discount to items. int
apply_coupon_remainder() Deal with remaining fractional discounts by splitting it over items until the amount is expired, discounting 1 cent at a time. int
validate_coupon_exists() Ensure coupon exists or throw exception. bool
validate_coupon_usage_limit() Ensure coupon usage limit is valid or throw exception. bool
validate_coupon_user_usage_limit() Ensure coupon user usage limit is valid or throw exception. bool
validate_coupon_expiry_date() Ensure coupon date is valid or throw exception. bool
validate_coupon_minimum_amount() Ensure coupon amount is valid or throw exception. bool
validate_coupon_maximum_amount() Ensure coupon amount is valid or throw exception. bool
validate_coupon_product_ids() Ensure coupon is valid for products in the list is valid or throw exception. bool
validate_coupon_product_categories() Ensure coupon is valid for product categories in the list is valid or throw exception. bool
validate_coupon_sale_items() Ensure coupon is valid for sale items in the list is valid or throw exception. bool
validate_coupon_excluded_items() All exclusion rules must pass at the same time for a product coupon to be valid. bool
validate_coupon_eligible_items() Cart discounts cannot be added if non-eligible product is found. bool
validate_coupon_excluded_product_ids() Exclude products. bool
validate_coupon_excluded_product_categories() Exclude categories from product list. bool
get_object_subtotal() Get the object subtotal int
is_coupon_valid() Check if a coupon is valid. bool|WP_Error

Properties

$discounts

An array of discounts which have been applied to items.

protected array[] $discounts = array()

Methods

set_items()

Set items directly. Used by WC_Cart_Totals.

public set_items( $items : array ) : mixed
Parameters
$items : array

Items to set.

Tags
since
Return values
mixed

set_items_from_cart()

Normalise cart items which will be discounted.

public set_items_from_cart( $cart : WC_Cart ) : mixed
Parameters
$cart : WC_Cart

Cart object.

Tags
since
Return values
mixed

set_items_from_order()

Normalise order items which will be discounted.

public set_items_from_order( $order : WC_Order ) : mixed
Parameters
$order : WC_Order

Order object.

Tags
since
Return values
mixed

get_object()

Get the object concerned.

public get_object( ) : object
Tags
since
Return values
object

get_items()

Get items.

public get_items( ) : object[]
Tags
since
Return values
object[]

get_items_to_validate()

Get items to validate.

public get_items_to_validate( ) : object[]
Tags
since
Return values
object[]

get_discount()

Get discount by key with or without precision.

public get_discount( $key : string [, $in_cents : bool = false ] ) : float
Parameters
$key : string

name of discount row to return.

$in_cents : bool = false

Should the totals be returned in cents, or without precision.

Tags
since
Return values
float

get_discounts()

Get all discount totals.

public get_discounts( [ $in_cents : bool = false ] ) : array
Parameters
$in_cents : bool = false

Should the totals be returned in cents, or without precision.

Tags
since
Return values
array

get_discounts_by_item()

Get all discount totals per item.

public get_discounts_by_item( [ $in_cents : bool = false ] ) : array
Parameters
$in_cents : bool = false

Should the totals be returned in cents, or without precision.

Tags
since
Return values
array

get_discounts_by_coupon()

Get all discount totals per coupon.

public get_discounts_by_coupon( [ $in_cents : bool = false ] ) : array
Parameters
$in_cents : bool = false

Should the totals be returned in cents, or without precision.

Tags
since
Return values
array

get_discounted_price()

Get discounted price of an item without precision.

public get_discounted_price( $item : object ) : float
Parameters
$item : object

Get data for this item.

Tags
since
Return values
float

get_discounted_price_in_cents()

Get discounted price of an item to precision (in cents).

public get_discounted_price_in_cents( $item : object ) : int
Parameters
$item : object

Get data for this item.

Tags
since
Return values
int

apply_coupon()

Apply a discount to all items using a coupon.

public apply_coupon( $coupon : WC_Coupon [, $validate : bool = true ] ) : bool|WP_Error
Parameters
$coupon : WC_Coupon

Coupon object being applied to the items.

$validate : bool = true

Set to false to skip coupon validation.

Tags
since
throws

Error message when coupon isn't valid.

Return values
bool|WP_Error

True if applied or WP_Error instance in failure.

sort_by_price()

Sort by price.

protected sort_by_price( $a : array , $b : array ) : int
Parameters
$a : array

First element.

$b : array

Second element.

Tags
since
Return values
int

filter_products_with_price()

Filter out all products which have been fully discounted to 0.

protected filter_products_with_price( $item : object ) : bool

Used as array_filter callback.

Parameters
$item : object

Get data for this item.

Tags
since
Return values
bool

get_items_to_apply_coupon()

Get items which the coupon should be applied to.

protected get_items_to_apply_coupon( $coupon : object ) : array
Parameters
$coupon : object

Coupon object.

Tags
since
Return values
array

apply_coupon_percent()

Apply percent discount to items and return an array of discounts granted.

protected apply_coupon_percent( $coupon : WC_Coupon , $items_to_apply : array ) : int
Parameters
$coupon : WC_Coupon

Coupon object. Passed through filters.

$items_to_apply : array

Array of items to apply the coupon to.

Tags
since
Return values
int

Total discounted.

apply_coupon_fixed_product()

Apply fixed product discount to items.

protected apply_coupon_fixed_product( $coupon : WC_Coupon , $items_to_apply : array [, $amount : int = null ] ) : int
Parameters
$coupon : WC_Coupon

Coupon object. Passed through filters.

$items_to_apply : array

Array of items to apply the coupon to.

$amount : int = null

Fixed discount amount to apply in cents. Leave blank to pull from coupon.

Tags
since
Return values
int

Total discounted.

apply_coupon_fixed_cart()

Apply fixed cart discount to items.

protected apply_coupon_fixed_cart( $coupon : WC_Coupon , $items_to_apply : array [, $amount : int = null ] ) : int
Parameters
$coupon : WC_Coupon

Coupon object. Passed through filters.

$items_to_apply : array

Array of items to apply the coupon to.

$amount : int = null

Fixed discount amount to apply in cents. Leave blank to pull from coupon.

Tags
since
Return values
int

Total discounted.

apply_coupon_custom()

Apply custom coupon discount to items.

protected apply_coupon_custom( $coupon : WC_Coupon , $items_to_apply : array ) : int
Parameters
$coupon : WC_Coupon

Coupon object. Passed through filters.

$items_to_apply : array

Array of items to apply the coupon to.

Tags
since
Return values
int

Total discounted.

apply_coupon_remainder()

Deal with remaining fractional discounts by splitting it over items until the amount is expired, discounting 1 cent at a time.

protected apply_coupon_remainder( $coupon : WC_Coupon , $items_to_apply : array , $amount : int ) : int
Parameters
$coupon : WC_Coupon

Coupon object if appliable. Passed through filters.

$items_to_apply : array

Array of items to apply the coupon to.

$amount : int

Fixed discount amount to apply.

Tags
since
Return values
int

Total discounted.

validate_coupon_exists()

Ensure coupon exists or throw exception.

protected validate_coupon_exists( $coupon : WC_Coupon ) : bool
Parameters
$coupon : WC_Coupon

Coupon data.

Tags
since
throws

Error message.

Return values
bool

validate_coupon_usage_limit()

Ensure coupon usage limit is valid or throw exception.

protected validate_coupon_usage_limit( $coupon : WC_Coupon ) : bool
Parameters
$coupon : WC_Coupon

Coupon data.

Tags
since
throws

Error message.

Return values
bool

validate_coupon_user_usage_limit()

Ensure coupon user usage limit is valid or throw exception.

protected validate_coupon_user_usage_limit( $coupon : WC_Coupon , $user_id : int ) : bool

Per user usage limit - check here if user is logged in (against user IDs). Checked again for emails later on in WC_Cart::check_customer_coupons().

Parameters
$coupon : WC_Coupon

Coupon data.

$user_id : int

User ID.

Tags
since
throws

Error message.

Return values
bool

validate_coupon_expiry_date()

Ensure coupon date is valid or throw exception.

protected validate_coupon_expiry_date( $coupon : WC_Coupon ) : bool
Parameters
$coupon : WC_Coupon

Coupon data.

Tags
since
throws

Error message.

Return values
bool

validate_coupon_minimum_amount()

Ensure coupon amount is valid or throw exception.

protected validate_coupon_minimum_amount( $coupon : WC_Coupon ) : bool
Parameters
$coupon : WC_Coupon

Coupon data.

Tags
since
throws

Error message.

Return values
bool

validate_coupon_maximum_amount()

Ensure coupon amount is valid or throw exception.

protected validate_coupon_maximum_amount( $coupon : WC_Coupon ) : bool
Parameters
$coupon : WC_Coupon

Coupon data.

Tags
since
throws

Error message.

Return values
bool

validate_coupon_product_ids()

Ensure coupon is valid for products in the list is valid or throw exception.

protected validate_coupon_product_ids( $coupon : WC_Coupon ) : bool
Parameters
$coupon : WC_Coupon

Coupon data.

Tags
since
throws

Error message.

Return values
bool

validate_coupon_product_categories()

Ensure coupon is valid for product categories in the list is valid or throw exception.

protected validate_coupon_product_categories( $coupon : WC_Coupon ) : bool
Parameters
$coupon : WC_Coupon

Coupon data.

Tags
since
throws

Error message.

Return values
bool

validate_coupon_sale_items()

Ensure coupon is valid for sale items in the list is valid or throw exception.

protected validate_coupon_sale_items( $coupon : WC_Coupon ) : bool
Parameters
$coupon : WC_Coupon

Coupon data.

Tags
since
throws

Error message.

Return values
bool

validate_coupon_excluded_items()

All exclusion rules must pass at the same time for a product coupon to be valid.

protected validate_coupon_excluded_items( $coupon : WC_Coupon ) : bool
Parameters
$coupon : WC_Coupon

Coupon data.

Tags
since
throws

Error message.

Return values
bool

validate_coupon_eligible_items()

Cart discounts cannot be added if non-eligible product is found.

protected validate_coupon_eligible_items( $coupon : WC_Coupon ) : bool
Parameters
$coupon : WC_Coupon

Coupon data.

Tags
since
throws

Error message.

Return values
bool

validate_coupon_excluded_product_ids()

Exclude products.

protected validate_coupon_excluded_product_ids( $coupon : WC_Coupon ) : bool
Parameters
$coupon : WC_Coupon

Coupon data.

Tags
since
throws

Error message.

Return values
bool

validate_coupon_excluded_product_categories()

Exclude categories from product list.

protected validate_coupon_excluded_product_categories( $coupon : WC_Coupon ) : bool
Parameters
$coupon : WC_Coupon

Coupon data.

Tags
since
throws

Error message.

Return values
bool

get_object_subtotal()

Get the object subtotal

protected get_object_subtotal( ) : int
Return values
int

is_coupon_valid()

Check if a coupon is valid.

public is_coupon_valid( $coupon : WC_Coupon ) : bool|WP_Error

Error Codes:

  • 100: Invalid filtered.
  • 101: Invalid removed.
  • 102: Not yours removed.
  • 103: Already applied.
  • 104: Individual use only.
  • 105: Not exists.
  • 106: Usage limit reached.
  • 107: Expired.
  • 108: Minimum spend limit not met.
  • 109: Not applicable.
  • 110: Not valid for sale items.
  • 111: Missing coupon code.
  • 112: Maximum spend limit met.
  • 113: Excluded products.
  • 114: Excluded categories.
Parameters
$coupon : WC_Coupon

Coupon data.

Tags
since
throws

Error message.

Return values
bool|WP_Error
Back to the top