I’m currently working on a WooCommerce webshop.

On this shop, I want the customer to have the ability to switch between seeing all the prices with and without tax (VAT), when clicking a button. This I call business and private customer-mode.

I have over the weekend spend many hours, trying to figure out how to add this extra “factor” when the tax is calculated. I’ve studied a lot of the WooCommerce core files and functions, but found it very hard, since there are sooo many functions for calculating and showing the price.

Then yesterday, I found a few functions that I thought was worth taking a deeper look at: raw_woocommerce_price(), wc_prices(), wc_get_price_to_display(), wc_get_price_including_tax() and wc_get_price_excluding_tax()

I then tried to add a filter to raw_woocommerce_price(), containing my own code, and I finally found a working solution, but I don’t think it’s good to use on the longer term.

Here’s my filter:

function filter_raw_woocommerce_price( $price ) {

    $business_mode = $_SESSION['business_mode']; // true or false
    $tax = 25; // Tax-rate in percentage. Get from DB somehow

    $price = $price;
    // If prices are shown incl tax and if business-mode is on - Subtract tax from price
    if( 'incl' === get_option( 'woocommerce_tax_display_shop' ) && $business_mode === true ) {
        $price = $price / (1+$tax/100);
    }
    // If prices are shown excl tax and if business-mode is off - Add tax to price
    else if( 'excl' === get_option( 'woocommerce_tax_display_shop' ) && $business_mode === false ) {
        $price = $price * (1+$tax/100);
    }

    return $price; 
}
add_filter( 'raw_woocommerce_price', 'filter_raw_woocommerce_price', 10, 1 );

The reason why I don’t think its good on the longer term is because:

  1. I manually have to enter the taxrate
  2. I manually have to enter the calculation for the new price

I also have the feeling that raw_woocommerce_price() is not the right function to use in this situation.

Logically I think that wc_get_price_including_tax() and wc_get_price_excluding_tax() would be better to use, so I don’t have to find the taxrate and do the tax calculations my self, but I don’t know how to implement my own code to these functions. It may also be that I’m totally wrong with it and that it may be a completely different function I need to

To sum up
I need to find the best/a good way to add a second factor when the tax is calculated on the products, so that it is the customer who decides to see the prices and buy the products incl/excl tax.


I know about the plugin “Tax Toggle” but this only changes the prices in the shop and not in the cart which I need. I also want to learn to do it myself, instead of just buying a plugin.

Someone who can please help/guide me how to do this?

Thanks in advance :)

Read more here: WooCommerce – Customer switch between incl and excl tax (VAT)


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: