businessdad icon

WooCommerce - Calculate and display tax rate for a product

businessdad | PRO | 07/14/17 09:57:36 PM UTC | 0 ⭐ | 925 👁️ | Never ⏰ | []
PHP |

800 B

|

None

|

0 👍

/

0 👎

/**
 * Display the tax rate applicable to a product, on the single product page.
 * Need help customising the code for your need? Hire us on Codeable: https://aelia.co/hire_us
 */
function exbtw_melding(){
  global $product;
  
  $price_incl_tax = wc_get_price_including_tax($product);
  $price_excl_tax = wc_get_price_excluding_tax($product);
  
  if(($price_incl_tax != 0) && ($price_excl_tax != 0)) {
    // Determine the tax rate, with a maximum of two decimals
    $tax_rate = round((($price_incl_tax / $price_excl_tax) - 1) * 100, 2);
  }
  
  // Display a message with the tax rates
  echo 'Alle vermelde prijzen zijn excl. <b>'. $tax_rate . ' BTW</b> / Tous les prix indiqués sont hors <b> '. $tax_rate . ' TVA</b>';
}
 
add_action( 'woocommerce_before_add_to_cart_form', 'exbtw_melding', 20 );

Comments