WooCommerce Code Reference

WC_Cart_Totals
in package
Uses WC_Item_Totals

WC_Cart_Totals class.

Tags
since
3.2.0

Table of Contents

$calculate_tax  : bool
Should taxes be calculated?
$cart  : WC_Cart
Reference to cart object.
$coupon_discount_tax_totals  : array<string|int, mixed>
Item/coupon discount tax totals.
$coupon_discount_totals  : array<string|int, mixed>
Item/coupon discount totals.
$coupons  : array<string|int, mixed>
Applied coupon objects.
$customer  : array<string|int, mixed>
Reference to customer object.
$fees  : array<string|int, mixed>
Fees to calculate.
$item_tax_rates  : array<string|int, mixed>
Cache of tax rates for a given tax class.
$items  : array<string|int, mixed>
Line items to calculate.
$shipping  : array<string|int, mixed>
Shipping costs.
$totals  : array<string|int, mixed>
Stores totals.
__construct()  : mixed
Sets up the items provided, and calculate totals.
get_rounded_items_total()  : float|int
Return rounded total based on settings. Will be used by Cart and Orders.
get_total()  : int|float
Get a single total with or without precision (in cents).
get_totals()  : mixed
Get all totals with or without precision (in cents).
round_item_subtotal()  : float
Apply rounding to item subtotal before summing.
adjust_non_base_location_price()  : object
Only ran if woocommerce_adjust_non_base_location_prices is true.
calculate()  : mixed
Run all calculation methods on the given items in sequence.
calculate_discounts()  : mixed
Calculate COUPON based discounts which change item prices.
calculate_fee_totals()  : mixed
Triggers the cart fees API, grabs the list of fees, and calculates taxes.
calculate_item_subtotals()  : mixed
Subtotals are costs before discounts.
calculate_item_totals()  : mixed
Calculate item totals.
calculate_shipping_totals()  : mixed
Calculate any shipping taxes.
calculate_totals()  : mixed
Main cart totals.
combine_item_taxes()  : array<string|int, mixed>
Combine item taxes into a single array, preserving keys.
get_coupons_from_cart()  : mixed
Return array of coupon objects from the cart. Normalises data into the same format for use by this class.
get_default_fee_props()  : array<string|int, mixed>
Get default blank set of props used per fee.
get_default_item_props()  : array<string|int, mixed>
Get default blank set of props used per item.
get_default_shipping_props()  : array<string|int, mixed>
Get default blank set of props used per shipping row.
get_discounted_price_in_cents()  : int
Get discounted price of an item with precision (in cents).
get_fees_from_cart()  : mixed
Get fee objects from the cart. Normalises data into the same format for use by this class.
get_item_costs_by_tax_class()  : array<string|int, mixed>
Get item costs grouped by tax class.
get_item_tax_rates()  : array<string|int, mixed>
Get tax rates for an item. Caches rates in class to avoid multiple look ups.
get_items_from_cart()  : mixed
Handles a cart or order object passed in for calculation. Normalises data into the same format for use by this class.
get_merged_taxes()  : array<string|int, mixed>
Get taxes merged by type.
get_shipping_from_cart()  : mixed
Get shipping methods from the cart and normalise.
get_tax_class_costs()  : array<string|int, mixed>
Get item costs grouped by tax class.
get_values_for_total()  : array<string|int, mixed>
Returns array of values for totals calculation.
remove_item_base_taxes()  : object
Ran to remove all base taxes from an item. Used when prices include tax, and the customer is tax exempt.
round_at_subtotal()  : bool
Should always round at subtotal?
round_line_tax()  : float
Apply rounding to an array of taxes before summing. Rounds to store DP setting, ignoring precision.
round_merged_taxes()  : array<string|int, mixed>
Round merged taxes.
set_total()  : mixed
Set a single total.
sort_coupons_callback()  : int
Sort coupons so discounts apply consistently across installs.

Properties

$coupon_discount_tax_totals

Item/coupon discount tax totals.

protected array<string|int, mixed> $coupon_discount_tax_totals = array()
Tags
since
3.2.0

$coupon_discount_totals

Item/coupon discount totals.

protected array<string|int, mixed> $coupon_discount_totals = array()
Tags
since
3.2.0

$totals

Stores totals.

protected array<string|int, mixed> $totals = array('fees_total' => 0, 'fees_total_tax' => 0, 'items_subtotal' => 0, 'items_subtotal_tax' => 0, 'items_total' => 0, 'items_total_tax' => 0, 'total' => 0, 'shipping_total' => 0, 'shipping_tax_total' => 0, 'discounts_total' => 0)
Tags
since
3.2.0

Methods

__construct()

Sets up the items provided, and calculate totals.

public __construct([WC_Cart &$cart = null ]) : mixed
Parameters
$cart : WC_Cart = null

Cart object to calculate totals for.

Tags
since
3.2.0
throws
Exception

If missing WC_Cart object.

Return values
mixed

get_rounded_items_total()

Return rounded total based on settings. Will be used by Cart and Orders.

public static get_rounded_items_total(array<string|int, mixed> $values) : float|int
Parameters
$values : array<string|int, mixed>

Values to round. Should be with precision.

Tags
since
3.9.0
Return values
float|intAppropriately rounded value.

get_total()

Get a single total with or without precision (in cents).

public get_total([string $key = 'total' ][, bool $in_cents = false ]) : int|float
Parameters
$key : string = 'total'

Total to get.

$in_cents : bool = false

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

Tags
since
3.2.0
Return values
int|float

get_totals()

Get all totals with or without precision (in cents).

public get_totals([bool $in_cents = false ]) : mixed
Parameters
$in_cents : bool = false

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

Tags
since
3.2.0
Return values
mixed

round_item_subtotal()

Apply rounding to item subtotal before summing.

public static round_item_subtotal(float $value) : float
Parameters
$value : float

Item subtotal value.

Tags
since
3.9.0
Return values
float

adjust_non_base_location_price()

Only ran if woocommerce_adjust_non_base_location_prices is true.

protected adjust_non_base_location_price(object $item) : object

If the customer is outside of the base location, this removes the base taxes. This is off by default unless the filter is used.

Uses edit context so unfiltered tax class is returned.

Parameters
$item : object

Item to adjust the prices of.

Tags
since
3.2.0
Return values
object

calculate()

Run all calculation methods on the given items in sequence.

protected calculate() : mixed
Tags
since
3.2.0
Return values
mixed

calculate_discounts()

Calculate COUPON based discounts which change item prices.

protected calculate_discounts() : mixed
Tags
since
3.2.0
uses
WC_Discounts

class.

Return values
mixed

calculate_fee_totals()

Triggers the cart fees API, grabs the list of fees, and calculates taxes.

protected calculate_fee_totals() : mixed

Note: This class sets the totals for the 'object' as they are calculated. This is so that APIs like the fees API can see these totals if needed.

Tags
since
3.2.0
Return values
mixed

calculate_item_subtotals()

Subtotals are costs before discounts.

protected calculate_item_subtotals() : mixed

To prevent rounding issues we need to work with the inclusive price where possible otherwise we'll see errors such as when working with a 9.99 inc price, 20% VAT which would be 8.325 leading to totals being 1p off.

Pre tax coupons come off the price the customer thinks they are paying - tax is calculated afterwards.

e.g. $100 bike with $10 coupon = customer pays $90 and tax worked backwards from that.

Tags
since
3.2.0
Return values
mixed

calculate_item_totals()

Calculate item totals.

protected calculate_item_totals() : mixed
Tags
since
3.2.0
Return values
mixed

calculate_shipping_totals()

Calculate any shipping taxes.

protected calculate_shipping_totals() : mixed
Tags
since
3.2.0
Return values
mixed

combine_item_taxes()

Combine item taxes into a single array, preserving keys.

protected combine_item_taxes(array<string|int, mixed> $item_taxes) : array<string|int, mixed>
Parameters
$item_taxes : array<string|int, mixed>

Taxes to combine.

Tags
since
3.2.0
Return values
array<string|int, mixed>

get_coupons_from_cart()

Return array of coupon objects from the cart. Normalises data into the same format for use by this class.

protected get_coupons_from_cart() : mixed
Tags
since
3.2.0
Return values
mixed

get_default_fee_props()

Get default blank set of props used per fee.

protected get_default_fee_props() : array<string|int, mixed>
Tags
since
3.2.0
Return values
array<string|int, mixed>

get_default_item_props()

Get default blank set of props used per item.

protected get_default_item_props() : array<string|int, mixed>
Tags
since
3.2.0
Return values
array<string|int, mixed>

get_default_shipping_props()

Get default blank set of props used per shipping row.

protected get_default_shipping_props() : array<string|int, mixed>
Tags
since
3.2.0
Return values
array<string|int, mixed>

get_discounted_price_in_cents()

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

protected get_discounted_price_in_cents(object $item_key) : int
Parameters
$item_key : object

Item to get the price of.

Tags
since
3.2.0
Return values
int

get_fees_from_cart()

Get fee objects from the cart. Normalises data into the same format for use by this class.

protected get_fees_from_cart() : mixed
Tags
since
3.2.0
Return values
mixed

get_item_costs_by_tax_class()

Get item costs grouped by tax class.

protected get_item_costs_by_tax_class() : array<string|int, mixed>
Tags
since
3.2.0
Return values
array<string|int, mixed>

get_item_tax_rates()

Get tax rates for an item. Caches rates in class to avoid multiple look ups.

protected get_item_tax_rates(object $item) : array<string|int, mixed>
Parameters
$item : object

Item to get tax rates for.

Return values
array<string|int, mixed>of taxes

get_items_from_cart()

Handles a cart or order object passed in for calculation. Normalises data into the same format for use by this class.

protected get_items_from_cart() : mixed

Each item is made up of the following props, in addition to those returned by get_default_item_props() for totals.

  • key: An identifier for the item (cart item key or line item ID).
  • cart_item: For carts, the cart item from the cart which may include custom data.
  • quantity: The qty for this line.
  • price: The line price in cents.
  • product: The product object this cart item is for.
Tags
since
3.2.0
Return values
mixed

get_merged_taxes()

Get taxes merged by type.

protected get_merged_taxes([bool $in_cents = false ][, array<string|int, mixed>|string $types = array('items', 'fees', 'shipping') ]) : array<string|int, mixed>
Parameters
$in_cents : bool = false

If returned value should be in cents.

$types : array<string|int, mixed>|string = array('items', 'fees', 'shipping')

Types to merge and return. Defaults to all.

Tags
since
3.2.0
Return values
array<string|int, mixed>

get_shipping_from_cart()

Get shipping methods from the cart and normalise.

protected get_shipping_from_cart() : mixed
Tags
since
3.2.0
Return values
mixed

get_tax_class_costs()

Get item costs grouped by tax class.

protected get_tax_class_costs() : array<string|int, mixed>
Tags
since
3.2.0
Return values
array<string|int, mixed>

get_values_for_total()

Returns array of values for totals calculation.

protected get_values_for_total(string $field) : array<string|int, mixed>
Parameters
$field : string

Field name. Will probably be total or subtotal.

Return values
array<string|int, mixed>Items object

remove_item_base_taxes()

Ran to remove all base taxes from an item. Used when prices include tax, and the customer is tax exempt.

protected remove_item_base_taxes(object $item) : object
Parameters
$item : object

Item to adjust the prices of.

Tags
since
3.2.2
Return values
object

round_line_tax()

Apply rounding to an array of taxes before summing. Rounds to store DP setting, ignoring precision.

protected static round_line_tax(float $value[, bool $in_cents = true ]) : float
Parameters
$value : float

Tax value.

$in_cents : bool = true

Whether precision of value is in cents.

Tags
since
3.2.6
Return values
float

round_merged_taxes()

Round merged taxes.

protected round_merged_taxes(array<string|int, mixed> $taxes) : array<string|int, mixed>
Parameters
$taxes : array<string|int, mixed>

Taxes to round.

Tags
deprecated
3.9.0

calculate_item_subtotals should already appropriately round the tax values.

since
3.5.4
Return values
array<string|int, mixed>

set_total()

Set a single total.

protected set_total(string $key, int $total) : mixed
Parameters
$key : string

Total name you want to set.

$total : int

Total to set.

Tags
since
3.2.0
Return values
mixed

sort_coupons_callback()

Sort coupons so discounts apply consistently across installs.

protected sort_coupons_callback(WC_Coupon $a, WC_Coupon $b) : int

In order of priority;

  • sort param
  • usage restriction
  • coupon value
  • ID
Parameters
$a : WC_Coupon

Coupon object.

$b : WC_Coupon

Coupon object.

Return values
int