WooCommerce Code Reference

Search results

WC_Tax

Performs tax calculations and loads tax rates

Table of Contents

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

Properties

$round_at_subtotal

Round at subtotal.

public static bool $round_at_subtotal = \false

Methods

init()

Load options.

public static init( ) : mixed
Return values
mixed

maybe_remove_tax_class_rates()

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

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

Old rates value.

$value : string

New rates value.

Tags
deprecated
Return values
mixed

calc_tax()

Calculate tax for a line.

public static calc_tax( $price : float , $rates : array [, $price_includes_tax : bool = false ] [, $deprecated : bool = false ] ) : array
Parameters
$price : float

Price to calc tax on.

$rates : array

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

Array of rates + prices after tax.

calc_shipping_tax()

Calculate the shipping tax using a passed array of rates.

public static calc_shipping_tax( $price : float , $rates : array ) : array
Parameters
$price : float

Shipping cost.

$rates : array

Taxation Rate.

Return values
array

round()

Round to precision.

public static round( $in : float|int ) : 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

calc_inclusive_tax()

Calc tax from inclusive price.

public static calc_inclusive_tax( $price : float , $rates : array ) : array
Parameters
$price : float

Price to calculate tax for.

$rates : array

Array of tax rates.

Return values
array

calc_exclusive_tax()

Calc tax from exclusive price.

public static calc_exclusive_tax( $price : float , $rates : array ) : array
Parameters
$price : float

Price to calculate tax for.

$rates : array

Array of tax rates.

Return values
array

find_rates()

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

public static find_rates( [ $args : array = array() ] ) : array
Parameters
$args : array = array()

Args that determine the rate to find.

Return values
array

find_shipping_rates()

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

public static find_shipping_rates( [ $args : array = array() ] ) : array
Parameters
$args : array = array()

Args that determine the rate to find.

Return values
array

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( $rate1 : object , $rate2 : object ) : int
Parameters
$rate1 : object

First rate to compare.

$rate2 : object

Second rate to compare.

Return values
int

sort_rates()

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

private static sort_rates( $rates : array ) : array
Parameters
$rates : array

Rates to be sorted.

Return values
array

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( $country : string , $state : string , $postcode : string , $city : string , $tax_class : string ) : array
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

get_tax_location()

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

public static get_tax_location( [ $tax_class : string = '' ] [, $customer : object = null ] ) : array

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

get_rates()

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

public static get_rates( [ $tax_class : string = '' ] [, $customer : object = null ] ) : array
Parameters
$tax_class : string = ''

Tax class to get rates for.

$customer : object = null

Override the customer object to get their location.

Return values
array

get_base_tax_rates()

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

public static get_base_tax_rates( [ $tax_class : string = '' ] ) : array
Parameters
$tax_class : string = ''

Tax Class.

Return values
array

get_shop_base_rate()

Alias for get_base_tax_rates().

public static get_shop_base_rate( [ $tax_class : string = '' ] ) : array
Parameters
$tax_class : string = ''

Tax Class.

Tags
deprecated
Return values
array

get_shipping_tax_rates()

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

public static get_shipping_tax_rates( [ $tax_class : string = null ] [, $customer : object = 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

is_compound()

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

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

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

Return values
bool

get_rate_label()

Return a given rates label.

public static get_rate_label( $key_or_rate : mixed ) : 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( $key_or_rate : mixed ) : 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( $key_or_rate : mixed ) : float
Parameters
$key_or_rate : mixed

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

Return values
float

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( $key_or_rate : mixed ) : string
Parameters
$key_or_rate : mixed

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

Return values
string

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( $taxes : array ) : float
Parameters
$taxes : array

Array of taxes.

Return values
float

get_tax_rate_classes()

Gets all tax rate classes from the database.

protected static get_tax_rate_classes( ) : array
Tags
since
Return values
array

Array of tax class objects consisting of tax_rate_class_id, name, and slug.

get_tax_classes()

Get store tax class names.

public static get_tax_classes( ) : array
Return values
array

Array of class names ("Reduced rate", "Zero rate", etc).

get_tax_class_slugs()

Get store tax classes as slugs.

public static get_tax_class_slugs( ) : array
Tags
since
Return values
array

Array of class slugs ("reduced-rate", "zero-rate", etc).

create_tax_class()

Create a new tax class.

public static create_tax_class( $name : string [, $slug : string = '' ] ) : WP_Error|array
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
Return values
WP_Error|array

Returns name and slug (array) if the tax class is created, or WP_Error if something went wrong.

get_tax_class_by()

Get an existing tax class.

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

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

$item : string|int

Item to get.

Tags
since
Return values
array|bool

Returns the tax class as an array. False if not found.

delete_tax_class_by()

Delete an existing tax class.

public static delete_tax_class_by( $field : string , $item : string|int ) : 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
Return values
WP_Error|bool

Returns true if deleted successfully, false if nothing was deleted, or WP_Error if there is an invalid request.

format_tax_rate_city()

Format the city.

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

Value to format.

Return values
string

format_tax_rate_state()

Format the state.

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

Value to format.

Return values
string

format_tax_rate_country()

Format the country.

private static format_tax_rate_country( $country : string ) : 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( $name : string ) : string
Parameters
$name : string

Value to format.

Return values
string

format_tax_rate()

Format the rate.

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

Value to format.

Return values
string

format_tax_rate_priority()

Format the priority.

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

Value to format.

Return values
int

format_tax_rate_class()

Format the class.

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

Value to format.

Return values
string

prepare_tax_rate()

Prepare and format tax rate for DB insertion.

private static prepare_tax_rate( $tax_rate : array ) : array
Parameters
$tax_rate : array

Tax rate to format.

Return values
array

_insert_tax_rate()

Insert a new tax rate.

public static _insert_tax_rate( $tax_rate : array ) : int

Internal use only.

Parameters
$tax_rate : array

Tax rate to insert.

Tags
since
Return values
int

tax rate id

_get_tax_rate()

Get tax rate.

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

Internal use only.

Parameters
$tax_rate_id : int

Tax rate ID.

$output_type : string = ARRAY_A

Type of output.

Tags
since
Return values
array|object

_update_tax_rate()

Update a tax rate.

public static _update_tax_rate( $tax_rate_id : int , $tax_rate : array ) : mixed

Internal use only.

Parameters
$tax_rate_id : int

Tax rate to update.

$tax_rate : array

Tax rate values.

Tags
since
Return values
mixed

_delete_tax_rate()

Delete a tax rate from the database.

public static _delete_tax_rate( $tax_rate_id : int ) : mixed

Internal use only.

Parameters
$tax_rate_id : int

Tax rate to delete.

Tags
since
Return values
mixed

_update_tax_rate_postcodes()

Update postcodes for a tax rate in the DB.

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

Internal use only.

Parameters
$tax_rate_id : int

Tax rate to update.

$postcodes : string

String of postcodes separated by ; characters.

Tags
since
Return values
mixed

_update_tax_rate_cities()

Update cities for a tax rate in the DB.

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

Internal use only.

Parameters
$tax_rate_id : int

Tax rate to update.

$cities : string

Cities to set.

Tags
since
Return values
mixed

update_tax_rate_locations()

Updates locations (postcode and city).

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

Internal use only.

Parameters
$tax_rate_id : int

Tax rate ID to update.

$values : array

Values to set.

$type : string

Location type.

Tags
since
Return values
mixed

get_rates_for_tax_class()

Used by admin settings page.

public static get_rates_for_tax_class( $tax_class : string ) : array|null|object
Parameters
$tax_class : string

Tax class slug.

Return values
array|null|object
Back to the top