John Davidson

php - Show single product prices with and without VAT in WooCommerce


I'm trying to show price with and without VAT on every Single Product page in a WooCommerce store.

I already done it for variable products with the script below, thanks to the WooCommerce_available_variation hook:

add_filter( 'woocommerce_available_variation', 'my_variation', 10, 3);
function my_variation( $data, $product, $variation ) {
$data['price_html'] = "<bdi><span class='inc-vat-price'></span>" . woocommerce_price($variation->get_price_including_tax()) . "</bdi><br>";
$data['price_html'] .= "<bdi><span class='ex-vat-price'>Tax Free - </span>". woocommerce_price($variation->get_price_excluding_tax()) . "</bdi>" ;
return $data;

Instead, for a Single Product, I haven't found any solution.

By modifying the Price.php file of the Single Product in WooCommerce, the price without Vat is repeated also in the variable products. In this case I have used the code:


What is a possible solution to this problem?

My Answer:
'Price: ' . wc_price( $variation->get_price_excluding_tax() ) . ' (Excluding VAT) - ' . wc_price( $variation->get_price_including_tax() ) . ' (Including VAT)';_ return $data;_}_However, for simple products, I need to modify the price display on the Single Product page. I can achieve this by adding the following code to my theme's functions.php file:_add_filter( 'woocommerce_get_price_html', 'my_price_html', 10, 2 );_function my_price_html( $price, $product ) {_ $price_excluding_tax = wc_get_price_excluding_tax( $product );_ $price_including_tax = wc_get_price_including_tax( $product );_ $price = 'Price: ' . wc_price( $price_excluding_tax ) . ' (Excluding VAT) - ' . wc_price( $price_including_tax ) . ' (Including VAT)';_ return $price;_}This code will display the price of simple products with and without VAT on the Single Product page in your WooCommerce store.

Rate this post

3 of 5 based on 9020 votes


© 2024 - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us