Dodanie do woocommerce brutto i netto

zapytanie-ze-schowka

 

https://tomjesch.com/display-woocommerce-products-with-and-without-tax/

 

Display WooCommerce products with and without tax

I’ve recently run into the problem that is not possible for a WooCommerce shop to display both VAT and EX. VAT prices at the same time. Though this was possible in earlier version (if I remember correctly).

Sure, there are many plugins that are able to change the price suffix, but finding the right plugin can become a lengthy process and I like to code these fixes myself, but more importantly: it keeps your WordPress install clean from unnecessary code.

WooCommerce provides a lot of actions and filters which allows developers to change or add elements to their WooCommerce shop. The hook we need is the ‘woocommerce_price_html’, which allows us to change the way a price is displayed in the shop overview and on the product page.

Using WooCommerce settings

As stated by Noel in the comments section, all of this is achievable in the WooCommerce setting.
Log in to your admin dashboard and navigate to (depending on your language):

WooCommerce > Settings > Tax

And scroll down to: ‘Price Display Suffix’ or in Dutch: Prijsweergave-achtervoegsel.

If you are displaying the prices including VAT, you’ll have to add the following to the field:

{price_excluding_tax}

If you are displaying the prices excluding VAT, you’ll need to add:

{price_including_tax}

By hovering over the question mark, you’ll receive some detailed explanation on it’s usage.

Applying tax/vat to all WooCommerce products

Whilst using the WooCommerce settings is the most easiest way, you can also achieve it by filtering the WooCommerce price by adding our own function. This allows us to add our own classes with which we can style our price with!

Copy the code below into your functions.php to display both product prices, with and without taxes.

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
function edit_price_display() {
  $product = new WC_Product( get_the_ID() );
  $price = $product->price;
  $price_incl_tax = $price + round($price * ( 21 / 100 ), 2);
  $price_incl_tax = number_format($price_incl_tax, 2, ",", ".");
  $price = number_format($price, 2, ",", ".");
  $display_price = '<span class="price">';
  $display_price .= '<span class="amount">€ ' . $price_incl_tax .'<small class="woocommerce-price-suffix"> incl BTW</small></span>';
  $display_price .= '<br>';
  $display_price .= '<span class="amount">€ ' . $price .'<small class="woocommerce-price-suffix"> excl BTW</small></span>';
  $display_price .= '</span>';
  echo $display_price;
}
add_filter('woocommerce_price_html', 'edit_price_display');

* Note that I live in the Netherlands, so we generally pay 21% taxes on our goods. To be sure, double check the tax percentage you pay in your country.

The ‘woocommerce_price_html’ hook is called everytime a product is displayed your screen.
– On line 2 and 3 we use the product ID to get it’s price.
– On the 4th line we calculate the amount of tax (21% of the normal price), round off the tax amount to 2 decimal behind the comma and add it to the original price.
– From line 6 to 12 we display the orginal price as well as the price including tax using the default WooCommerce price markup.
– Finally we echo the $display_price, in which we’ve save out HTML code.

Because we made use of the default WooCommerce classes, we let WooCommerce do all the hard work of styling the price output (we’re programmers, not designers :P).

Applying tax/vat to specific WooCommerce products

Below is an edit of the code above, allowing the code to function only for specific products by using their ID.
I’ve only added an if statement that compares the current product with the desired ID. I would only apply this for a maximum of 5 products, to keep everything neat and clean and. For even more products you should look at adding custom meta fields (like a checkbox) to a WooCommerce product in the WordPress dashboard, which is a more user friendly solution for those who aren’t experienced programmers.

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
function edit_price_display() {
  $product = new WC_Product( get_the_ID() );
  if($product->id == 'ID'){
    $price = $product->price;
    $price_incl_tax = $price + round($price * ( 21 / 100 ), 2);
    
    $price_incl_tax = number_format($price_incl_tax, 2, ",", ".");
    $price = number_format($price, 2, ",", ".");
    $display_price = '<span class="price">';
    $display_price .= '<span class="amount">€ ' . $price_incl_tax .'<small class="woocommerce-price-suffix"> incl BTW</small></span>';
    $display_price .= '<br>';
    $display_price .= '<span class="amount">€ ' . $price .'<small class="woocommerce-price-suffix"> excl BTW</small></span>';
    $display_price .= '</span>';
    echo $display_price;
  }
}
add_filter('woocommerce_price_html', 'edit_price_display');

Include and exclude VAT to a WooCommerce variation product price

The code above only works for the regular entered price in WooCommerce, but what if we have price variations? Simple, we just add one line below the already existing ‘add_filter’ .

1
add_filter('woocommerce_variable_price_html', 'edit_price_display');

With this additional ‘add_filter’ apply our function when WooCommerce calls the price for a product with several variations. This allows us to keep our code clean; creating functionality once and adding it to several WooCommerce filters!

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
function edit_price_display() {
  $product = new WC_Product( get_the_ID() );
  $price = $product->price;
  $price_incl_tax = $price + round($price * ( 21 / 100 ), 2);
  
  $price_incl_tax = number_format($price_incl_tax, 2, ",", ".");
  $price = number_format($price, 2, ",", ".");
  $display_price = '<span class="price">';
  $display_price .= '<span class="amount">€ ' . $price_incl_tax .'<small class="woocommerce-price-suffix"> incl BTW</small></span>';
  $display_price .= '<br>';
  $display_price .= '<span class="amount">€ ' . $price .'<small class="woocommerce-price-suffix"> excl BTW</small></span>';
  $display_price .= '</span>';
  echo $display_price;
}
add_filter('woocommerce_price_html', 'edit_price_display');
add_filter('woocommerce_variable_price_html', 'edit_price_display');

Comments

Reklamy

Skomentuj

Wprowadź swoje dane lub kliknij jedną z tych ikon, aby się zalogować:

Logo WordPress.com

Komentujesz korzystając z konta WordPress.com. Wyloguj / Zmień )

Zdjęcie z Twittera

Komentujesz korzystając z konta Twitter. Wyloguj / Zmień )

Zdjęcie na Facebooku

Komentujesz korzystając z konta Facebook. Wyloguj / Zmień )

Zdjęcie na Google+

Komentujesz korzystając z konta Google+. Wyloguj / Zmień )

Connecting to %s