问题
I need to output the product tax rate (in % value) from a product on the single product page. I have tried several code snippets but so far I only get either the tax class name or the text value "Array" back.
Does anybody know how to get this value? My shop sells food (6% TAX VAT) and non-food (21% TAX VAT) products. All prices are shown excl. VAT but the tax class and rates have been set correctly and applied to all products.
When a customer check the product page, I have to show them (by law) which TAX VAT applies to the product.
I have the below snippet so far. This is showing "Array" as output.
// For WooCommerce Version 3.0+ (only)
add_action( 'woocommerce_before_add_to_cart_form', 'exbtw_melding', 20 );
function exbtw_melding(){
global $product;
$tax_rates = WC_Tax::get_rates( $product->get_tax_class() );
echo 'Alle vermelde prijzen zijn excl. <b>'. $tax_rates . ' BTW</b> /
Tous les prix indiqués sont hors <b> '. $tax_rates . ' TVA</b>';
}
Any help please?
Thanks!
Fabio
回答1:
This is what I have used:
function woocommerce_template_display_tax() {
global $product;
$tax_rates = WC_Tax::get_rates( $product->get_tax_class() );
if (!empty($tax_rates)) {
$tax_rate = reset($tax_rates);
echo sprintf(_x('Inclusive %.2f %% tax', 'Text for tax rate. %.2f = tax rate', 'wptheme.foundation'), $tax_rate['rate']);
}
}
来源:https://stackoverflow.com/questions/44788306/woocommerce-output-product-tax-rate-wc-3-0