Encodera

Thanks for reading!

Donate to the site — we work hard for you.

Contents

Price formatting and taxes

A price in the database is just a number: 9.9. The customer sees it with a currency symbol, the right separators and, if taxes are on, with or without tax. There are ready-made functions for all of that — there is no need to build a price string by hand.

Formatting

// wp-content/plugins/acme-shop/includes/storefront.php
add_action('woocommerce_single_product_summary', function () {
    global $product;
 
    $perMonth = (float) $product->get_price() / 4;
 
    echo '<p>Or four payments of ' . wc_price($perMonth) . '</p>';
}, 11);

wc_price() turns a number into HTML with the currency: for a mug at €9.90 the line Or four payments of €2.48 appears under the price. The symbol, its position, the separators and the number of decimals come from WooCommerce → Settings → General, the Currency options block.

Price functions

FunctionWhat it returns
wc_price(9.9)Price HTML in the shop currency
wc_price(9.9, ['currency' => 'USD'])The same in another currency
get_woocommerce_currency()The currency code: EUR
get_woocommerce_currency_symbol()The symbol: €
wc_get_price_decimals()The number of decimals
wc_format_decimal(9.9, 2)A number with two decimals for storage: 9.90

wc_price() returns HTML with tags — for a plain-text email or an export file wrap it in wp_strip_all_tags().

Enabling taxes

Taxes are off until Enable tax rates and calculations is ticked in WooCommerce → Settings → General. After that a Tax tab appears with three main options:

OptionWhat it sets
Prices entered with or without taxHow to read the price in the product editor
Display prices in the shopWith or without tax in the storefront
Standard ratesA table: country, rate in percent, name

Add a row with the country DE and the rate 19 to Standard rates — and products get a tax.

Price with and without tax

// wp-content/plugins/acme-shop/includes/storefront.php
add_action('woocommerce_single_product_summary', function () {
    global $product;
 
    if (!wc_tax_enabled()) {
        return;
    }
 
    printf(
        '<p>Excluding tax: %s, including tax: %s</p>',
        wc_price(wc_get_price_excluding_tax($product)),
        wc_price(wc_get_price_including_tax($product))
    );
}, 12);

Both functions take a product and compute the price by the tax settings. If prices are entered without tax and the rate is 19%, the mug gives Excluding tax: €9.90, including tax: €11.78.

A quantity and your own price are passed in the second argument: wc_get_price_including_tax($product, ['qty' => 3, 'price' => 8.5]).

The display price

$price = wc_get_price_to_display($product);

The function itself picks the variant with or without tax — the one set in the Display prices in the shop option. Use it wherever a price is printed next to the standard product price.

A product's tax

Product methodWhat it returns
get_tax_status()taxable, shipping or none
get_tax_class()The tax class, an empty string for the standard one

A tax class is a separate rate table, for example a reduced one for books. Classes are added on the Tax tab and assigned to a product in the editor, the Tax class field.

Common mistake

A price is assembled by hand: $product->get_price() . ' €'. When the currency or the format changes in the settings, such strings stay old, and with taxes they show a different amount than the cart. Get the number with wc_get_price_to_display() and the string with wc_price().

Thanks for reading!

Donate to the site — we work hard for you.