WC_Discounts
in package
Discounts class.
Table of Contents
- $discounts : array<string|int, array<string|int, mixed>>
- An array of discounts which have been applied to items.
- $items : array<string|int, mixed>
- An array of items to discount.
- $object : WC_Cart|WC_Order
- Reference to cart or order object.
- __construct() : mixed
- WC_Discounts Constructor.
- apply_coupon() : bool|WP_Error
- Apply a discount to all items using a coupon.
- get_discount() : float
- Get discount by key with or without precision.
- get_discounted_price() : float
- Get discounted price of an item without precision.
- get_discounted_price_in_cents() : int
- Get discounted price of an item to precision (in cents).
- get_discounts() : array<string|int, mixed>
- Get all discount totals.
- get_discounts_by_coupon() : array<string|int, mixed>
- Get all discount totals per coupon.
- get_discounts_by_item() : array<string|int, mixed>
- Get all discount totals per item.
- get_items() : array<string|int, object>
- Get items.
- get_items_to_validate() : array<string|int, object>
- Get items to validate.
- get_object() : object
- Get the object concerned.
- is_coupon_valid() : bool|WP_Error
- Check if a coupon is valid.
- set_items() : mixed
- Set items directly. Used by WC_Cart_Totals.
- set_items_from_cart() : mixed
- Normalise cart items which will be discounted.
- set_items_from_order() : mixed
- Normalise order items which will be discounted.
- apply_coupon_custom() : int
- Apply custom coupon discount to items.
- apply_coupon_fixed_cart() : int
- Apply fixed cart discount to items.
- apply_coupon_fixed_product() : int
- Apply fixed product discount to items.
- apply_coupon_percent() : int
- Apply percent discount to items and return an array of discounts granted.
- apply_coupon_remainder() : int
- Deal with remaining fractional discounts by splitting it over items until the amount is expired, discounting 1 cent at a time.
- filter_products_with_price() : bool
- Filter out all products which have been fully discounted to 0.
- get_items_to_apply_coupon() : array<string|int, mixed>
- Get items which the coupon should be applied to.
- get_object_subtotal() : int
- Get the object subtotal
- sort_by_price() : int
- Sort by price.
- validate_coupon_allowed_emails() : bool
- Ensure coupon is valid for allowed emails or throw exception.
- validate_coupon_eligible_items() : bool
- Cart discounts cannot be added if non-eligible product is found.
- validate_coupon_excluded_items() : bool
- All exclusion rules must pass at the same time for a product coupon to be valid.
- validate_coupon_excluded_product_categories() : bool
- Exclude categories from product list.
- validate_coupon_excluded_product_ids() : bool
- Exclude products.
- validate_coupon_exists() : bool
- Ensure coupon exists or throw exception.
- validate_coupon_expiry_date() : bool
- Ensure coupon date is valid or throw exception.
- validate_coupon_maximum_amount() : bool
- Ensure coupon amount is valid or throw exception.
- validate_coupon_minimum_amount() : bool
- Ensure coupon amount is valid or throw exception.
- validate_coupon_product_categories() : bool
- Ensure coupon is valid for product categories in the list is valid or throw exception.
- validate_coupon_product_ids() : bool
- Ensure coupon is valid for products in the list is valid or throw exception.
- validate_coupon_sale_items() : bool
- Ensure coupon is valid for sale items in the list is valid or throw exception.
- validate_coupon_usage_limit() : bool
- Ensure coupon usage limit is valid or throw exception.
- validate_coupon_user_usage_limit() : bool
- Ensure coupon user usage limit is valid or throw exception.
Properties
$discounts
An array of discounts which have been applied to items.
protected
array<string|int, array<string|int, mixed>>
$discounts
= array()
$items
An array of items to discount.
protected
array<string|int, mixed>
$items
= array()
$object
Reference to cart or order object.
protected
WC_Cart|WC_Order
$object
Tags
Methods
__construct()
WC_Discounts Constructor.
public
__construct([WC_Cart|WC_Order $object = null ]) : mixed
Parameters
Return values
mixed —apply_coupon()
Apply a discount to all items using a coupon.
public
apply_coupon(WC_Coupon $coupon[, bool $validate = 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
Return values
bool|WP_Error — True if applied or WP_Error instance in failure.get_discount()
Get discount by key with or without precision.
public
get_discount(string $key[, bool $in_cents = 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
Return values
float —get_discounted_price()
Get discounted price of an item without precision.
public
get_discounted_price(object $item) : float
Parameters
- $item : object
-
Get data for this item.
Tags
Return values
float —get_discounted_price_in_cents()
Get discounted price of an item to precision (in cents).
public
get_discounted_price_in_cents(object $item) : int
Parameters
- $item : object
-
Get data for this item.
Tags
Return values
int —get_discounts()
Get all discount totals.
public
get_discounts([bool $in_cents = false ]) : array<string|int, mixed>
Parameters
- $in_cents : bool = false
-
Should the totals be returned in cents, or without precision.
Tags
Return values
array<string|int, mixed> —get_discounts_by_coupon()
Get all discount totals per coupon.
public
get_discounts_by_coupon([bool $in_cents = false ]) : array<string|int, mixed>
Parameters
- $in_cents : bool = false
-
Should the totals be returned in cents, or without precision.
Tags
Return values
array<string|int, mixed> —get_discounts_by_item()
Get all discount totals per item.
public
get_discounts_by_item([bool $in_cents = false ]) : array<string|int, mixed>
Parameters
- $in_cents : bool = false
-
Should the totals be returned in cents, or without precision.
Tags
Return values
array<string|int, mixed> —get_items()
Get items.
public
get_items() : array<string|int, object>
Tags
Return values
array<string|int, object> —get_items_to_validate()
Get items to validate.
public
get_items_to_validate() : array<string|int, object>
Tags
Return values
array<string|int, object> —get_object()
Get the object concerned.
public
get_object() : object
Tags
Return values
object —is_coupon_valid()
Check if a coupon is valid.
public
is_coupon_valid(WC_Coupon $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
Return values
bool|WP_Error —set_items()
Set items directly. Used by WC_Cart_Totals.
public
set_items(array<string|int, mixed> $items) : mixed
Parameters
- $items : array<string|int, mixed>
-
Items to set.
Tags
Return values
mixed —set_items_from_cart()
Normalise cart items which will be discounted.
public
set_items_from_cart(WC_Cart $cart) : mixed
Parameters
- $cart : WC_Cart
-
Cart object.
Tags
Return values
mixed —set_items_from_order()
Normalise order items which will be discounted.
public
set_items_from_order(WC_Order $order) : mixed
Parameters
- $order : WC_Order
-
Order object.
Tags
Return values
mixed —apply_coupon_custom()
Apply custom coupon discount to items.
protected
apply_coupon_custom(WC_Coupon $coupon, array<string|int, mixed> $items_to_apply) : int
Parameters
- $coupon : WC_Coupon
-
Coupon object. Passed through filters.
- $items_to_apply : array<string|int, mixed>
-
Array of items to apply the coupon to.
Tags
Return values
int — Total discounted.apply_coupon_fixed_cart()
Apply fixed cart discount to items.
protected
apply_coupon_fixed_cart(WC_Coupon $coupon, array<string|int, mixed> $items_to_apply[, int $amount = null ]) : int
Parameters
- $coupon : WC_Coupon
-
Coupon object. Passed through filters.
- $items_to_apply : array<string|int, mixed>
-
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
Return values
int — Total discounted.apply_coupon_fixed_product()
Apply fixed product discount to items.
protected
apply_coupon_fixed_product(WC_Coupon $coupon, array<string|int, mixed> $items_to_apply[, int $amount = null ]) : int
Parameters
- $coupon : WC_Coupon
-
Coupon object. Passed through filters.
- $items_to_apply : array<string|int, mixed>
-
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
Return values
int — Total discounted.apply_coupon_percent()
Apply percent discount to items and return an array of discounts granted.
protected
apply_coupon_percent(WC_Coupon $coupon, array<string|int, mixed> $items_to_apply) : int
Parameters
- $coupon : WC_Coupon
-
Coupon object. Passed through filters.
- $items_to_apply : array<string|int, mixed>
-
Array of items to apply the coupon to.
Tags
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(WC_Coupon $coupon, array<string|int, mixed> $items_to_apply, int $amount) : int
Parameters
- $coupon : WC_Coupon
-
Coupon object if applicable. Passed through filters.
- $items_to_apply : array<string|int, mixed>
-
Array of items to apply the coupon to.
- $amount : int
-
Fixed discount amount to apply.
Tags
Return values
int — Total discounted.filter_products_with_price()
Filter out all products which have been fully discounted to 0.
protected
filter_products_with_price(object $item) : bool
Used as array_filter callback.
Parameters
- $item : object
-
Get data for this item.
Tags
Return values
bool —get_items_to_apply_coupon()
Get items which the coupon should be applied to.
protected
get_items_to_apply_coupon(object $coupon) : array<string|int, mixed>
Parameters
- $coupon : object
-
Coupon object.
Tags
Return values
array<string|int, mixed> —get_object_subtotal()
Get the object subtotal
protected
get_object_subtotal() : int
Return values
int —sort_by_price()
Sort by price.
protected
sort_by_price(array<string|int, mixed> $a, array<string|int, mixed> $b) : int
Parameters
- $a : array<string|int, mixed>
-
First element.
- $b : array<string|int, mixed>
-
Second element.
Tags
Return values
int —validate_coupon_allowed_emails()
Ensure coupon is valid for allowed emails or throw exception.
protected
validate_coupon_allowed_emails(WC_Coupon $coupon) : bool
Parameters
- $coupon : WC_Coupon
-
Coupon data.
Tags
Return values
bool —validate_coupon_eligible_items()
Cart discounts cannot be added if non-eligible product is found.
protected
validate_coupon_eligible_items(WC_Coupon $coupon) : bool
Parameters
- $coupon : WC_Coupon
-
Coupon data.
Tags
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(WC_Coupon $coupon) : bool
Parameters
- $coupon : WC_Coupon
-
Coupon data.
Tags
Return values
bool —validate_coupon_excluded_product_categories()
Exclude categories from product list.
protected
validate_coupon_excluded_product_categories(WC_Coupon $coupon) : bool
Parameters
- $coupon : WC_Coupon
-
Coupon data.
Tags
Return values
bool —validate_coupon_excluded_product_ids()
Exclude products.
protected
validate_coupon_excluded_product_ids(WC_Coupon $coupon) : bool
Parameters
- $coupon : WC_Coupon
-
Coupon data.
Tags
Return values
bool —validate_coupon_exists()
Ensure coupon exists or throw exception.
protected
validate_coupon_exists(WC_Coupon $coupon) : bool
A coupon is also considered to no longer exist if it has been placed in the trash, even if the trash has not yet been emptied.
Parameters
- $coupon : WC_Coupon
-
Coupon data.
Tags
Return values
bool —validate_coupon_expiry_date()
Ensure coupon date is valid or throw exception.
protected
validate_coupon_expiry_date(WC_Coupon $coupon) : bool
Parameters
- $coupon : WC_Coupon
-
Coupon data.
Tags
Return values
bool —validate_coupon_maximum_amount()
Ensure coupon amount is valid or throw exception.
protected
validate_coupon_maximum_amount(WC_Coupon $coupon) : bool
Parameters
- $coupon : WC_Coupon
-
Coupon data.
Tags
Return values
bool —validate_coupon_minimum_amount()
Ensure coupon amount is valid or throw exception.
protected
validate_coupon_minimum_amount(WC_Coupon $coupon) : bool
Parameters
- $coupon : WC_Coupon
-
Coupon data.
Tags
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(WC_Coupon $coupon) : bool
Parameters
- $coupon : WC_Coupon
-
Coupon data.
Tags
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(WC_Coupon $coupon) : bool
Parameters
- $coupon : WC_Coupon
-
Coupon data.
Tags
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(WC_Coupon $coupon) : bool
Parameters
- $coupon : WC_Coupon
-
Coupon data.
Tags
Return values
bool —validate_coupon_usage_limit()
Ensure coupon usage limit is valid or throw exception.
protected
validate_coupon_usage_limit(WC_Coupon $coupon) : bool
Parameters
- $coupon : WC_Coupon
-
Coupon data.
Tags
Return values
bool —validate_coupon_user_usage_limit()
Ensure coupon user usage limit is valid or throw exception.
protected
validate_coupon_user_usage_limit(WC_Coupon $coupon, int $user_id) : 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.