WP SITES

3085 Coded Tutorials & 288 Plugins

Display Price Including Tax in WooCommerce Products

You can use code snippets to display products prices including tax based on tax status. The tax status setting is included in the single product data general tab. To display your product prices including tax, take the following steps :

  • Enable taxes : Go to WooCommerce > Settings > General and enables taxes
  • Tax Options : Go to WooCommerce > Settings > Tax and add additional tax classes
  • Tax Rates : Click the link for each tax class and configure the rates
  • Custom Functions : Add PHP code to your child themes functions file or code snippets plugin.

Single Product Page Prices

Use this code to include tax on your single product pages

add_filter('woocommerce_get_price_html', function ($price_html, $product) {
    if ($product instanceof WC_Product) {
        if ($product->get_tax_status() === 'none') {
            return $price_html; // Return default price if non-taxable
        }

        $tax_class = $product->get_tax_class();

        // Define tax classes that should show price INCLUDING tax
        $tax_classes_incl = ['custom-tax-class'];

        if (in_array($tax_class, $tax_classes_incl)) {
            // Get price including tax
            $price_incl_tax = wc_get_price_including_tax($product, ['qty' => 1]);

            // Get tax amount
            $tax_amount = $price_incl_tax - wc_get_price_excluding_tax($product, ['qty' => 1]);

            // Return price with tax included
            return wc_price($price_incl_tax) . ' <small>(Incl. Tax: ' . wc_price($tax_amount) . ')</small>';
        } else {
            // Get price excluding tax
            $price_excl_tax = wc_get_price_excluding_tax($product, ['qty' => 1]);

            // Get tax amount
            $tax_amount = wc_get_price_including_tax($product, ['qty' => 1]) - $price_excl_tax;

            // Return price excluding tax
            return wc_price($price_excl_tax) . ' <small>(Tax: ' . wc_price($tax_amount) . ')</small>';
        }
    }

    return $price_html;
}, 10, 2);

Shop Page Prices

Use this code to include tax on your shop page prices

add_filter('woocommerce_get_price_html', function ($price_html, $product) {
    if ($product instanceof WC_Product) {
        if ($product->get_tax_status() === 'none') {
            return $price_html; // Return default price if non-taxable
        }

        $tax_class = $product->get_tax_class();

        // Define tax classes that should show price INCLUDING tax
        $tax_classes_incl = ['custom-tax-class'];

        if (in_array($tax_class, $tax_classes_incl)) {
            // Get price including tax
            $price_incl_tax = wc_get_price_including_tax($product, ['qty' => 1]);

            // Get tax amount
            $tax_amount = $price_incl_tax - wc_get_price_excluding_tax($product, ['qty' => 1]);

            // Return price with tax included
            return wc_price($price_incl_tax) . ' <small>(Incl. Tax: ' . wc_price($tax_amount) . ')</small>';
        } else {
            // Get price excluding tax
            $price_excl_tax = wc_get_price_excluding_tax($product, ['qty' => 1]);

            // Get tax amount
            $tax_amount = wc_get_price_including_tax($product, ['qty' => 1]) - $price_excl_tax;

            // Return price excluding tax
            return wc_price($price_excl_tax) . ' <small>(Tax: ' . wc_price($tax_amount) . ')</small>';
        }
    }

    return $price_html;
}, 10, 2);

Cart Page Prices

Use this code to change prices on your cart and checkout pages.

add_filter('woocommerce_cart_item_price', function ($price_html, $cart_item, $cart_item_key) {
    $product = $cart_item['data'];

    if ($product instanceof WC_Product) {
        if ($product->get_tax_status() === 'none') {
            return $price_html; // Return default price if non-taxable
        }

        $tax_class = $product->get_tax_class();

        // Define tax classes that should show price INCLUDING tax
        $tax_classes_incl = ['custom-tax-class'];

        if (in_array($tax_class, $tax_classes_incl)) {
            // Get price including tax
            $price_incl_tax = wc_get_price_including_tax($product, ['qty' => 1]);

            // Get tax amount
            $tax_amount = $price_incl_tax - wc_get_price_excluding_tax($product, ['qty' => 1]);

            // Return price with tax included
            return wc_price($price_incl_tax) . ' <small>(Incl. Tax: ' . wc_price($tax_amount) . ')</small>';
        } else {
            // Get price excluding tax
            $price_excl_tax = wc_get_price_excluding_tax($product, ['qty' => 1]);

            // Get tax amount
            $tax_amount = wc_get_price_including_tax($product, ['qty' => 1]) - $price_excl_tax;

            // Return price excluding tax
            return wc_price($price_excl_tax) . ' <small>(Incl Tax: ' . wc_price($tax_amount) . ')</small>';
        }
    }

    return $price_html;
}, 10, 3);

Note : You must swap out the tax class slug in the code snippets above. Example : custom-tax-class

Screen Shots

Shows the product price adjusted to include tax when a tax class is selected on the single product page, otherwise, tax is not included in the actual price but will be added to the totals on the cart and checkout pages.

Leave a Reply

New Plugins