Total_Spend extends Type implements Actionable, Applicable Uses Discount_With_Reductions

Total spend discount type.

Give a discount based on the amount spent, e.g. 10% off if you spend over $100, or $5 off t-shirts when you spend $100 in that category.

Tags
author

Barn2 Plugins support@barn2.com

license

GPL-3.0

copyright

Barn2 Media Ltd

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  = 2
$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.
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_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_discount_amount_type()  : string
Get the type of discount amount.
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_reduction()  : Reduction
Get the reduction to apply to products.
get_settings()  : array<string|int, mixed>
Get the settings for this discount type.
get_slug()  : string
Get the slug for this discount type.
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
If the discount is applicable, apply it to the cart so that the total of the cart is reduced.
run_order_actions()  : void
If the discount is applicable, apply it to the order so that the total of the order is reduced.
set_discount()  : self
Set the discount object.
set_proccesing_order()  : self
Flag whether the discount type is being applied to a WooCommerce order.
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.
get_all_categories_of_all_products()  : array<string|int, mixed>
Get all categories of all products in the cart/order.
get_cart_total_for_relevant_products()  : float
Retrieve the total cart value for the relevant products.
get_order_total_for_relevant_products()  : float
Retrieve the total order value for the relevant products.
products_selection_is_applicable()  : bool
Determine if the discount is applicable to the cart or order based on the products it applies to.

Constants

Properties

$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

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_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
inheritdoc
Return values
array<string|int, mixed>

get_discount_amount_type()

Get the type of discount amount.

public get_discount_amount_type() : string

Possible values: 'percentage', 'fixed'.

Return values
string

The type of discount amount.

get_name()

Get the name for this discount type.

public static get_name() : string
Tags
inheritdoc
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
inheritdoc
Return values
array<string|int, mixed>

get_slug()

Get the slug for this discount type.

public static get_slug() : string
Tags
inheritdoc
Return values
string

get_tooltip()

Get the tooltip for this discount type.

public static get_tooltip() : string
Tags
inheritdoc
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.

Tags
inheritdoc
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.

Tags
inheritdoc
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()

If the discount is applicable, apply it to the cart so that the total of the cart is reduced.

public run_cart_actions(WC_Cart &$cart) : void

To achieve this, we need to:

  • Retrieve the total cart value.
  • Check if the discount type is a percentage or a fixed amount.
  • Calculate the new total cart value by reducing the total cart value by the discount amount.
  • Use the new total cart value to dynamically apply a reduction to each cart item so that the total cart value now matches the new total cart value.
Parameters
$cart : WC_Cart
Return values
void

run_order_actions()

If the discount is applicable, apply it to the order so that the total of the order is reduced.

public run_order_actions(WC_Order &$order) : void

To achieve this, we need to:

  • Retrieve the total order value.
  • Check if the discount type is a percentage or a fixed amount.
  • Calculate the new total order value by reducing the total order value by the discount amount.
  • Use the new total order value to dynamically apply a reduction to each order item so that the total order value now matches the new total order value.
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_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.

get_all_categories_of_all_products()

Get all categories of all products in the cart/order.

private get_all_categories_of_all_products(bool|WC_Cart $cart[, bool|WC_Order $order = false ]) : array<string|int, mixed>
Parameters
$cart : bool|WC_Cart
$order : bool|WC_Order = false
Return values
array<string|int, mixed>

get_cart_total_for_relevant_products()

Retrieve the total cart value for the relevant products.

private get_cart_total_for_relevant_products(WC_Cart $cart) : float
Parameters
$cart : WC_Cart
Return values
float

get_order_total_for_relevant_products()

Retrieve the total order value for the relevant products.

private get_order_total_for_relevant_products(WC_Order $order) : float
Parameters
$order : WC_Order
Return values
float

products_selection_is_applicable()

Determine if the discount is applicable to the cart or order based on the products it applies to.

private products_selection_is_applicable(bool|WC_Cart $cart[, bool|WC_Order $order = false ]) : bool

When the discount applies to all products, it is always applicable. When the discount applies to specific products, it is only applicable if the cart/order contains ONLY those specific products. When the discount applies to specific categories, it is only applicable if the cart/order contains ONLY products in those categories.

Parameters
$cart : bool|WC_Cart

The cart. False when checking an order.

$order : bool|WC_Order = false

The order. False when checking a cart.

Return values
bool

Search results