WooCommerce Code Reference

WC_Tax
in package

Performs tax calculations and loads tax rates

Tags
class

WC_Tax

Table of Contents

$precision  : int
Precision.
$round_at_subtotal  : bool
Round at subtotal.
_delete_tax_rate()  : mixed
Delete a tax rate from the database.
_get_tax_rate()  : array<string|int, mixed>|object
Get tax rate.
_insert_tax_rate()  : int
Insert a new tax rate.
_update_tax_rate()  : mixed
Update a tax rate.
_update_tax_rate_cities()  : mixed
Update cities for a tax rate in the DB.
_update_tax_rate_postcodes()  : mixed
Update postcodes for a tax rate in the DB.
calc_exclusive_tax()  : array<string|int, mixed>
Calc tax from exclusive price.
calc_inclusive_tax()  : array<string|int, mixed>
Calc tax from inclusive price.
calc_shipping_tax()  : array<string|int, mixed>
Calculate the shipping tax using a passed array of rates.
calc_tax()  : array<string|int, mixed>
Calculate tax for a line.
create_tax_class()  : WP_Error|array<string|int, mixed>
Create a new tax class.
delete_tax_class_by()  : WP_Error|bool
Delete an existing tax class.
find_rates()  : array<string|int, mixed>
Searches for all matching country/state/postcode tax rates.
find_shipping_rates()  : array<string|int, mixed>
Searches for all matching country/state/postcode tax rates.
format_tax_rate_class()  : string
Format the class.
get_base_tax_rates()  : array<string|int, mixed>
Get's an array of matching rates for the shop's base country.
get_rate_code()  : string
Get a rates code. Code is made up of COUNTRY-STATE-NAME-Priority. E.g GB-VAT-1, US-AL-TAX-1.
get_rate_label()  : string
Return a given rates label.
get_rate_percent()  : string
Return a given rates percent.
get_rate_percent_value()  : float
Return a given rates percent.
get_rates()  : array<string|int, mixed>
Get's an array of matching rates for a tax class.
get_rates_for_tax_class()  : array<string|int, mixed>|null|object
Used by admin settings page.
get_rates_from_location()  : mixed|void
Get's an array of matching rates from location and tax class. $customer parameter is used to preserve backward compatibility for filter.
get_shipping_tax_rates()  : mixed
Gets an array of matching shipping tax rates for a given class.
get_shop_base_rate()  : array<string|int, mixed>
Alias for get_base_tax_rates().
get_tax_class_by()  : array<string|int, mixed>|bool
Get an existing tax class.
get_tax_class_slugs()  : array<string|int, mixed>
Get store tax classes as slugs.
get_tax_classes()  : array<string|int, mixed>
Get store tax class names.
get_tax_location()  : array<string|int, mixed>
Get the customer tax location based on their status and the current page.
get_tax_rate_classes()  : array<string|int, mixed>
Gets all tax rate classes from the database.
get_tax_total()  : float
Sums a set of taxes to form a single total. Values are pre-rounded to precision from 3.6.0.
init()  : mixed
Load options.
is_compound()  : bool
Return true/false depending on if a rate is a compound rate.
maybe_remove_tax_class_rates()  : mixed
When the woocommerce_tax_classes option is changed, remove any orphan rates.
round()  : float
Round to precision.
format_tax_rate()  : string
Format the rate.
format_tax_rate_city()  : string
Format the city.
format_tax_rate_country()  : string
Format the country.
format_tax_rate_name()  : string
Format the tax rate name.
format_tax_rate_priority()  : int
Format the priority.
format_tax_rate_state()  : string
Format the state.
get_matched_tax_rates()  : array<string|int, mixed>
Loop through a set of tax rates and get the matching rates (1 per priority).
prepare_tax_rate()  : array<string|int, mixed>
Prepare and format tax rate for DB insertion.
sort_rates()  : array<string|int, mixed>
Logical sort order for tax rates based on the following in order of priority.
sort_rates_callback()  : int
Does the sort comparison. Compares (in this order): - Priority - Country - State - Number of postcodes - Number of cities - ID
update_tax_rate_locations()  : mixed
Updates locations (postcode and city).

Properties

Methods

_delete_tax_rate()

Delete a tax rate from the database.

public static _delete_tax_rate(int $tax_rate_id) : mixed

Internal use only.

Parameters
$tax_rate_id : int

Tax rate to delete.

Tags
since
2.3.0
Return values
mixed

_get_tax_rate()

Get tax rate.

public static _get_tax_rate(int $tax_rate_id[, string $output_type = ARRAY_A ]) : array<string|int, mixed>|object

Internal use only.

Parameters
$tax_rate_id : int

Tax rate ID.

$output_type : string = ARRAY_A

Type of output.

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

_insert_tax_rate()

Insert a new tax rate.

public static _insert_tax_rate(array<string|int, mixed> $tax_rate) : int

Internal use only.

Parameters
$tax_rate : array<string|int, mixed>

Tax rate to insert.

Tags
since
2.3.0
Return values
inttax rate id

_update_tax_rate()

Update a tax rate.

public static _update_tax_rate(int $tax_rate_id, array<string|int, mixed> $tax_rate) : mixed

Internal use only.

Parameters
$tax_rate_id : int

Tax rate to update.

$tax_rate : array<string|int, mixed>

Tax rate values.

Tags
since
2.3.0
Return values
mixed

_update_tax_rate_cities()

Update cities for a tax rate in the DB.

public static _update_tax_rate_cities(int $tax_rate_id, string $cities) : mixed

Internal use only.

Parameters
$tax_rate_id : int

Tax rate to update.

$cities : string

Cities to set.

Tags
since
2.3.0
Return values
mixed

_update_tax_rate_postcodes()

Update postcodes for a tax rate in the DB.

public static _update_tax_rate_postcodes(int $tax_rate_id, string $postcodes) : mixed

Internal use only.

Parameters
$tax_rate_id : int

Tax rate to update.

$postcodes : string

String of postcodes separated by ; characters.

Tags
since
2.3.0
Return values
mixed

calc_exclusive_tax()

Calc tax from exclusive price.

public static calc_exclusive_tax(float $price, array<string|int, mixed> $rates) : array<string|int, mixed>
Parameters
$price : float

Price to calculate tax for.

$rates : array<string|int, mixed>

Array of tax rates.

Return values
array<string|int, mixed>

calc_inclusive_tax()

Calc tax from inclusive price.

public static calc_inclusive_tax(float $price, array<string|int, mixed> $rates) : array<string|int, mixed>
Parameters
$price : float

Price to calculate tax for.

$rates : array<string|int, mixed>

Array of tax rates.

Return values
array<string|int, mixed>

calc_shipping_tax()

Calculate the shipping tax using a passed array of rates.

public static calc_shipping_tax(float $price, array<string|int, mixed> $rates) : array<string|int, mixed>
Parameters
$price : float

Shipping cost.

$rates : array<string|int, mixed>

Taxation Rate.

Return values
array<string|int, mixed>

calc_tax()

Calculate tax for a line.

public static calc_tax(float $price, array<string|int, mixed> $rates[, bool $price_includes_tax = false ][, bool $deprecated = false ]) : array<string|int, mixed>
Parameters
$price : float

Price to calc tax on.

$rates : array<string|int, mixed>

Rates to apply.

$price_includes_tax : bool = false

Whether the passed price has taxes included.

$deprecated : bool = false

Whether to suppress any rounding from taking place. No longer used here.

Return values
array<string|int, mixed>Array of rates + prices after tax.

create_tax_class()

Create a new tax class.

public static create_tax_class(string $name[, string $slug = '' ]) : WP_Error|array<string|int, mixed>
Parameters
$name : string

Name of the tax class to add.

$slug : string = ''

(optional) Slug of the tax class to add. Defaults to sanitized name.

Tags
since
3.7.0
Return values
WP_Error|array<string|int, mixed>Returns name and slug (array) if the tax class is created, or WP_Error if something went wrong.

delete_tax_class_by()

Delete an existing tax class.

public static delete_tax_class_by(string $field, string|int $item) : WP_Error|bool
Parameters
$field : string

Field to delete by. Valid values are id, name, or slug.

$item : string|int

Item to delete.

Tags
since
3.7.0
Return values
WP_Error|boolReturns true if deleted successfully, false if nothing was deleted, or WP_Error if there is an invalid request.

find_rates()

Searches for all matching country/state/postcode tax rates.

public static find_rates([array<string|int, mixed> $args = array() ]) : array<string|int, mixed>
Parameters
$args : array<string|int, mixed> = array()

Args that determine the rate to find.

Return values
array<string|int, mixed>

find_shipping_rates()

Searches for all matching country/state/postcode tax rates.

public static find_shipping_rates([array<string|int, mixed> $args = array() ]) : array<string|int, mixed>
Parameters
$args : array<string|int, mixed> = array()

Args that determine the rate to find.

Return values
array<string|int, mixed>

format_tax_rate_class()

Format the class.

public static format_tax_rate_class(string $class) : string
Parameters
$class : string

Value to format.

Return values
string

get_base_tax_rates()

Get's an array of matching rates for the shop's base country.

public static get_base_tax_rates([string $tax_class = '' ]) : array<string|int, mixed>
Parameters
$tax_class : string = ''

Tax Class.

Return values
array<string|int, mixed>

get_rate_code()

Get a rates code. Code is made up of COUNTRY-STATE-NAME-Priority. E.g GB-VAT-1, US-AL-TAX-1.

public static get_rate_code(mixed $key_or_rate) : string
Parameters
$key_or_rate : mixed

Tax rate ID, or the db row itself in object format.

Return values
string

get_rate_label()

Return a given rates label.

public static get_rate_label(mixed $key_or_rate) : string
Parameters
$key_or_rate : mixed

Tax rate ID, or the db row itself in object format.

Return values
string

get_rate_percent()

Return a given rates percent.

public static get_rate_percent(mixed $key_or_rate) : string
Parameters
$key_or_rate : mixed

Tax rate ID, or the db row itself in object format.

Return values
string

get_rate_percent_value()

Return a given rates percent.

public static get_rate_percent_value(mixed $key_or_rate) : float
Parameters
$key_or_rate : mixed

Tax rate ID, or the db row itself in object format.

Return values
float

get_rates()

Get's an array of matching rates for a tax class.

public static get_rates([string $tax_class = '' ][, object $customer = null ]) : array<string|int, mixed>
Parameters
$tax_class : string = ''

Tax class to get rates for.

$customer : object = null

Override the customer object to get their location.

Return values
array<string|int, mixed>

get_rates_for_tax_class()

Used by admin settings page.

public static get_rates_for_tax_class(string $tax_class) : array<string|int, mixed>|null|object
Parameters
$tax_class : string

Tax class slug.

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

get_rates_from_location()

Get's an array of matching rates from location and tax class. $customer parameter is used to preserve backward compatibility for filter.

public static get_rates_from_location(string $tax_class, array<string|int, mixed> $location[, object $customer = null ]) : mixed|void
Parameters
$tax_class : string

Tax class to get rates for.

$location : array<string|int, mixed>

Location to compute rates for. Should be in form: array( country, state, postcode, city).

$customer : object = null

Only used to maintain backward compatibility for filter woocommerce-matched_rates.

Return values
mixed|voidTax rates.

get_shipping_tax_rates()

Gets an array of matching shipping tax rates for a given class.

public static get_shipping_tax_rates([string $tax_class = null ][, object $customer = null ]) : mixed
Parameters
$tax_class : string = null

Tax class to get rates for.

$customer : object = null

Override the customer object to get their location.

Return values
mixed

get_shop_base_rate()

Alias for get_base_tax_rates().

public static get_shop_base_rate([string $tax_class = '' ]) : array<string|int, mixed>
Parameters
$tax_class : string = ''

Tax Class.

Tags
deprecated
2.3
Return values
array<string|int, mixed>

get_tax_class_by()

Get an existing tax class.

public static get_tax_class_by(string $field, string|int $item) : array<string|int, mixed>|bool
Parameters
$field : string

Field to get by. Valid values are id, name, or slug.

$item : string|int

Item to get.

Tags
since
3.7.0
Return values
array<string|int, mixed>|boolReturns the tax class as an array. False if not found.

get_tax_class_slugs()

Get store tax classes as slugs.

public static get_tax_class_slugs() : array<string|int, mixed>
Tags
since
3.0.0
Return values
array<string|int, mixed>Array of class slugs ("reduced-rate", "zero-rate", etc).

get_tax_classes()

Get store tax class names.

public static get_tax_classes() : array<string|int, mixed>
Return values
array<string|int, mixed>Array of class names ("Reduced rate", "Zero rate", etc).

get_tax_location()

Get the customer tax location based on their status and the current page.

public static get_tax_location([string $tax_class = '' ][, object $customer = null ]) : array<string|int, mixed>

Used by get_rates(), get_shipping_rates().

Parameters
$tax_class : string = ''

string Optional, passed to the filter for advanced tax setups.

$customer : object = null

Override the customer object to get their location.

Return values
array<string|int, mixed>

get_tax_rate_classes()

Gets all tax rate classes from the database.

public static get_tax_rate_classes() : array<string|int, mixed>
Tags
since
3.7.0
Return values
array<string|int, mixed>Array of tax class objects consisting of tax_rate_class_id, name, and slug.

get_tax_total()

Sums a set of taxes to form a single total. Values are pre-rounded to precision from 3.6.0.

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

Array of taxes.

Return values
float

is_compound()

Return true/false depending on if a rate is a compound rate.

public static is_compound(mixed $key_or_rate) : bool
Parameters
$key_or_rate : mixed

Tax rate ID, or the db row itself in object format.

Return values
bool

maybe_remove_tax_class_rates()

When the woocommerce_tax_classes option is changed, remove any orphan rates.

public static maybe_remove_tax_class_rates(string $old_value, string $value) : mixed
Parameters
$old_value : string

Old rates value.

$value : string

New rates value.

Tags
deprecated
3.7.0
Return values
mixed

round()

Round to precision.

public static round(float|int $in) : float

Filter example: to return rounding to .5 cents you'd use:

function euro_5cent_rounding( $in ) { return round( $in / 5, 2 ) * 5; } add_filter( 'woocommerce_tax_round', 'euro_5cent_rounding' );

Parameters
$in : float|int

Value to round.

Return values
float

format_tax_rate()

Format the rate.

private static format_tax_rate(float $rate) : string
Parameters
$rate : float

Value to format.

Return values
string

format_tax_rate_city()

Format the city.

private static format_tax_rate_city(string $city) : string
Parameters
$city : string

Value to format.

Return values
string

format_tax_rate_country()

Format the country.

private static format_tax_rate_country(string $country) : string
Parameters
$country : string

Value to format.

Return values
string

format_tax_rate_name()

Format the tax rate name.

private static format_tax_rate_name(string $name) : string
Parameters
$name : string

Value to format.

Return values
string

format_tax_rate_priority()

Format the priority.

private static format_tax_rate_priority(string $priority) : int
Parameters
$priority : string

Value to format.

Return values
int

format_tax_rate_state()

Format the state.

private static format_tax_rate_state(string $state) : string
Parameters
$state : string

Value to format.

Return values
string

get_matched_tax_rates()

Loop through a set of tax rates and get the matching rates (1 per priority).

private static get_matched_tax_rates(string $country, string $state, string $postcode, string $city, string $tax_class) : array<string|int, mixed>
Parameters
$country : string

Country code to match against.

$state : string

State code to match against.

$postcode : string

Postcode to match against.

$city : string

City to match against.

$tax_class : string

Tax class to match against.

Return values
array<string|int, mixed>

prepare_tax_rate()

Prepare and format tax rate for DB insertion.

private static prepare_tax_rate(array<string|int, mixed> $tax_rate) : array<string|int, mixed>
Parameters
$tax_rate : array<string|int, mixed>

Tax rate to format.

Return values
array<string|int, mixed>

sort_rates()

Logical sort order for tax rates based on the following in order of priority.

private static sort_rates(array<string|int, mixed> $rates) : array<string|int, mixed>
Parameters
$rates : array<string|int, mixed>

Rates to be sorted.

Return values
array<string|int, mixed>

sort_rates_callback()

Does the sort comparison. Compares (in this order): - Priority - Country - State - Number of postcodes - Number of cities - ID

private static sort_rates_callback(object $rate1, object $rate2) : int
Parameters
$rate1 : object

First rate to compare.

$rate2 : object

Second rate to compare.

Return values
int

update_tax_rate_locations()

Updates locations (postcode and city).

private static update_tax_rate_locations(int $tax_rate_id, array<string|int, mixed> $values, string $type) : mixed

Internal use only.

Parameters
$tax_rate_id : int

Tax rate ID to update.

$values : array<string|int, mixed>

Values to set.

$type : string

Location type.

Tags
since
2.3.0
Return values
mixed