Bulk extends Type implements Actionable, Applicable
Bulk discount type.
This is a percentage or price discount that applies to a range of quantities. Example: Set up quantity-based discounts, e.g. 1-5: $5.00, 6-10: $4.50, and 11-20: $4.00.
Tags
Interfaces, Classes and Traits
- Actionable
- Interface for discount types that have cart actions.
- Applicable
- Indicates that a discount type needs to be verified as applicable to the cart as a whole.
Table of Contents
- ORDER = 6
- $discount : Discount
- The discount object.
- $processing_order : bool
- Whether the discount type is processing a WooCommerce order.
- $total_discounted_products : int
- The total number of discounted products in the cart|order.
- $total_order_discount : float
- The total discount that needs to be applied to the order.
- apply_discount_to_cart() : void
- Apply quantity-based discounts to the cart.
- apply_discount_to_line_item() : void
- The discount here is applied individually the line item given.
- decrease_total_discounted_products() : self
- Decrease the total number of discounted products in the cart|order.
- decrease_total_order_discount() : self
- Decrease the total discount that needs to be applied to the order.
- generate_virtual_coupon_for_order() : void
- Generate a virtual coupon for the order.
- get_bulk_table_display_location() : string
- Get the bulk table display location.
- get_cart_quantity_of_relevant_products() : int
- Return the total quantity of relevant products in the cart.
- get_default_settings_values() : array<string|int, mixed>
- Get the default settings values for this discount type.
- get_discount() : Discount
- Get the discount object.
- get_name() : string
- Get the name for this discount type.
- get_order_quantity_of_relevant_products() : int
- Return the total quantity of relevant products in the order.
- get_settings() : array<string|int, mixed>
- Get the settings for this discount type.
- get_slug() : string
- Get the slug for this discount type.
- get_tiers_for_product() : array<string|int, mixed>
- Get the tiers for a product.
- get_tooltip() : string
- Get the tooltip for this discount type.
- get_total_discounted_products() : int
- Return the total number of discounted products in the cart|order.
- get_total_order_discount() : float
- Return the total discount that needs to be applied to the order.
- increase_total_discounted_products() : self
- Increase the total number of discounted products in the cart|order.
- increase_total_order_discount() : self
- Increase the total discount that needs to be applied to the order.
- is_applicable_to_cart() : bool
- Determine if the discount is applicable to the cart as a whole.
- is_applicable_to_order() : bool
- Determine if the discount is applicable to the order as a whole.
- is_processing_order() : bool
- Return whether the discount type is being applied to a WooCommerce order.
- make() : static
- Make a new discount type.
- reset_total_discounted_products() : self
- Reset the total number of discounted products in the cart|order.
- reset_total_order_discount() : self
- Reset the total discount that needs to be applied to the order.
- run_cart_actions() : void
- Apply quantity-based discounts to the cart.
- run_order_actions() : void
- Apply quantity-based discounts to the order.
- set_discount() : self
- Set the discount object.
- set_proccesing_order() : self
- Flag whether the discount type is being applied to a WooCommerce order.
- set_tier_max_quantity_when_empty() : array<string|int, mixed>
- Set the max quantity to PHP_INT_MAX when empty.
- set_total_discounted_products() : self
- Set the total number of discounted products in the cart|order.
- set_total_order_discount() : self
- Set the total discount that needs to be applied to the order.
- should_display_bulk_table() : bool
- Determine whether to display the bulk table.
- find_tier() : array<string|int, mixed>|null
- Find the tier that matches the quantity.
- generate_discount_for_entire_order() : void
- Generate a virtual coupon for the discount and apply it to the order.
- generate_discount_for_products_in_order() : void
- Generate a virtual coupon for the discount and apply it to the order.
Constants
ORDER
public
mixed
ORDER
= 6
Properties
$discount
The discount object.
protected
Discount
$discount
$processing_order
Whether the discount type is processing a WooCommerce order.
protected
bool
$processing_order
= false
This is used so that we prevent applying discounts at a order line level due to the multitude of issues in how taxes are calculated in WooCommerce.
$total_discounted_products
The total number of discounted products in the cart|order.
protected
int
$total_discounted_products
= 0
This is used to track the number of discounted products in the cart|order but not all types will use this.
Not all discount types need to track the number of discounted products.
$total_order_discount
The total discount that needs to be applied to the order.
protected
float
$total_order_discount
= 0
Methods
apply_discount_to_cart()
Apply quantity-based discounts to the cart.
public
apply_discount_to_cart(WC_Cart|WC_Order &$cart_or_order, array<string|int, mixed> $products_to_discount, array<string|int, mixed> $tier, string $tier_range_type[, bool $is_order = false ]) : void
The discount here is spread across all products in the cart so that the final cart subtotal matches the discount amount.
Parameters
- $cart_or_order : WC_Cart|WC_Order
-
The cart or order instance.
- $products_to_discount : array<string|int, mixed>
-
The products to discount.
- $tier : array<string|int, mixed>
-
The tier.
- $tier_range_type : string
-
The tier range type.
- $is_order : bool = false
-
Whether the discount is being applied to an order.
Return values
void —apply_discount_to_line_item()
The discount here is applied individually the line item given.
public
apply_discount_to_line_item(array<string|int, mixed>|WC_Order_Item_Product $item, array<string|int, mixed> $tier, string $tier_range_type[, bool $is_order = false ]) : void
Parameters
- $item : array<string|int, mixed>|WC_Order_Item_Product
-
The cart item.
- $tier : array<string|int, mixed>
-
The tier.
- $tier_range_type : string
-
The tier range type.
- $is_order : bool = false
-
Whether the discount is being applied to an order.
Return values
void —decrease_total_discounted_products()
Decrease the total number of discounted products in the cart|order.
public
decrease_total_discounted_products(int $amount) : self
Parameters
- $amount : int
-
The amount to decrease the total number of discounted products by.
Return values
self —The discount type.
decrease_total_order_discount()
Decrease the total discount that needs to be applied to the order.
public
decrease_total_order_discount(float $amount) : self
Parameters
- $amount : float
-
The amount to decrease the total discount by.
Return values
self —The discount type.
generate_virtual_coupon_for_order()
Generate a virtual coupon for the order.
public
generate_virtual_coupon_for_order(WC_Order $order) : void
This is used to apply the discount to the order and is not a real coupon.
This is mainly used during the creating of manual orders.
Parameters
- $order : WC_Order
-
The order object.
Return values
void —get_bulk_table_display_location()
Get the bulk table display location.
public
get_bulk_table_display_location() : string
Return values
string —get_cart_quantity_of_relevant_products()
Return the total quantity of relevant products in the cart.
public
get_cart_quantity_of_relevant_products(WC_Cart $cart) : int
Parameters
- $cart : WC_Cart
-
The cart object.
Return values
int —get_default_settings_values()
Get the default settings values for this discount type.
public
static get_default_settings_values() : array<string|int, mixed>
Tags
Return values
array<string|int, mixed> —get_discount()
Get the discount object.
public
get_discount() : Discount
Return values
Discount —get_name()
Get the name for this discount type.
public
static get_name() : string
Tags
Return values
string —get_order_quantity_of_relevant_products()
Return the total quantity of relevant products in the order.
public
get_order_quantity_of_relevant_products(WC_Order $order) : int
Parameters
- $order : WC_Order
-
The order object.
Return values
int —get_settings()
Get the settings for this discount type.
public
static get_settings() : array<string|int, mixed>
Tags
Return values
array<string|int, mixed> —get_slug()
Get the slug for this discount type.
public
static get_slug() : string
Tags
Return values
string —get_tiers_for_product()
Get the tiers for a product.
public
get_tiers_for_product(int $product_id) : array<string|int, mixed>
Parameters
- $product_id : int
-
The product ID.
Return values
array<string|int, mixed> —get_tooltip()
Get the tooltip for this discount type.
public
static get_tooltip() : string
Tags
Return values
string —get_total_discounted_products()
Return the total number of discounted products in the cart|order.
public
get_total_discounted_products() : int
Return values
int —get_total_order_discount()
Return the total discount that needs to be applied to the order.
public
get_total_order_discount() : float
Return values
float —increase_total_discounted_products()
Increase the total number of discounted products in the cart|order.
public
increase_total_discounted_products(int $amount) : self
Parameters
- $amount : int
-
The amount to increase the total number of discounted products by.
Return values
self —The discount type.
increase_total_order_discount()
Increase the total discount that needs to be applied to the order.
public
increase_total_order_discount(float $amount) : self
Parameters
- $amount : float
-
The amount to increase the total discount by.
Return values
self —The discount type.
is_applicable_to_cart()
Determine if the discount is applicable to the cart as a whole.
public
is_applicable_to_cart(WC_Cart $cart) : bool
Parameters
- $cart : WC_Cart
-
The cart object.
Return values
bool —True if the discount is applicable to the cart as a whole.
is_applicable_to_order()
Determine if the discount is applicable to the order as a whole.
public
is_applicable_to_order(WC_Order $order) : bool
Parameters
- $order : WC_Order
-
The order object.
Return values
bool —True if the discount is applicable to the order as a whole.
is_processing_order()
Return whether the discount type is being applied to a WooCommerce order.
public
is_processing_order() : bool
Return values
bool —make()
Make a new discount type.
public
static make(Discount $discount) : static
Parameters
- $discount : Discount
-
The discount object.
Return values
static —The new discount type.
reset_total_discounted_products()
Reset the total number of discounted products in the cart|order.
public
reset_total_discounted_products() : self
Return values
self —The discount type.
reset_total_order_discount()
Reset the total discount that needs to be applied to the order.
public
reset_total_order_discount() : self
Return values
self —The discount type.
run_cart_actions()
Apply quantity-based discounts to the cart.
public
run_cart_actions(WC_Cart &$cart) : void
Parameters
- $cart : WC_Cart
Return values
void —run_order_actions()
Apply quantity-based discounts to the order.
public
run_order_actions(WC_Order &$order) : void
The discount here is processed differently to the cart discount. We do not discount the products directly but instead we generate a virtual coupon for the discount and apply it to the order.
In order to apply the virtual coupon to the order, we need to calculate the total discount amount for the order and then apply the discount to the order using the virtual coupon.
Parameters
- $order : WC_Order
Return values
void —set_discount()
Set the discount object.
public
set_discount(Discount $discount) : self
Parameters
- $discount : Discount
-
The discount object.
Return values
self —set_proccesing_order()
Flag whether the discount type is being applied to a WooCommerce order.
public
set_proccesing_order(bool $processing_order) : self
Parameters
- $processing_order : bool
-
Whether the discount type is processing a WooCommerce order.
Return values
self —The discount type.
set_tier_max_quantity_when_empty()
Set the max quantity to PHP_INT_MAX when empty.
public
set_tier_max_quantity_when_empty(array<string|int, mixed> $tiers) : array<string|int, mixed>
Parameters
- $tiers : array<string|int, mixed>
-
The tiers.
Return values
array<string|int, mixed> —set_total_discounted_products()
Set the total number of discounted products in the cart|order.
public
set_total_discounted_products(int $total_discounted_products) : self
Parameters
- $total_discounted_products : int
-
The total number of discounted products in the cart|order.
Return values
self —The discount type.
set_total_order_discount()
Set the total discount that needs to be applied to the order.
public
set_total_order_discount(float $total_order_discount) : self
Parameters
- $total_order_discount : float
-
The total discount that needs to be applied to the order.
Return values
self —The discount type.
should_display_bulk_table()
Determine whether to display the bulk table.
public
should_display_bulk_table() : bool
Return values
bool —find_tier()
Find the tier that matches the quantity.
private
find_tier(array<string|int, mixed> $tiers, int $quantity) : array<string|int, mixed>|null
Parameters
- $tiers : array<string|int, mixed>
- $quantity : int
Return values
array<string|int, mixed>|null —generate_discount_for_entire_order()
Generate a virtual coupon for the discount and apply it to the order.
private
generate_discount_for_entire_order(WC_Order $order, array<string|int, mixed> $products, array<string|int, mixed> $tier, string $tier_range_type) : void
The calculation here is the same as the cart discount.
Which means the discount is based off the total order quantity instead of the individual product quantity.
Parameters
- $order : WC_Order
-
The order.
- $products : array<string|int, mixed>
-
The products.
- $tier : array<string|int, mixed>
-
The tier.
- $tier_range_type : string
-
The tier range type.
Return values
void —generate_discount_for_products_in_order()
Generate a virtual coupon for the discount and apply it to the order.
private
generate_discount_for_products_in_order(WC_Order $order, array<string|int, mixed> $products, array<string|int, mixed> $tiers) : void
The calculation here is the same as the cart line item discount.
Which means the discount is based off the individual product quantity instead of the total order quantity.
Parameters
- $order : WC_Order
-
The order.
- $products : array<string|int, mixed>
-
The products.
- $tiers : array<string|int, mixed>
-
The tiers.