WooCommerce output product tax rate (WC 3.0+)

时间秒杀一切 提交于 2019-12-13 20:10:20

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!