Display tax amount based on specific tax class in WooCommerce variations

こ雲淡風輕ζ 提交于 2021-01-29 04:01:46

问题


I'm currently using a custom function to target a specific product and change the output of the price with and without tax on the product page.

This currently works as intended for an individual product id, however trying to get this work for a specific tax_class instead with no avail

add_filter( 'woocommerce_available_variation', 'tax_variation', 10, 3);
function tax_variation( $data, $product, $variation ) {
$product = wc_get_product();
$id = $product->get_id();
$price_excl_tax = wc_get_price_excluding_tax( $variation ); // price without VAT
$price_incl_tax = wc_get_price_including_tax( $variation );  // price with VAT
$tax_amount     = $price_incl_tax - $price_excl_tax; // VAT amount


if ( $id == 113576 ) {

    $data['price_html'] = "<span class='ex-vat-price'>Price: <b>" . woocommerce_price($variation->get_price_excluding_tax()) . "</b></span><br>";
    $data['price_html'] .= "<span class='tax_amount'>Sales Tax 13%: <b>" . woocommerce_price($tax_amount) . "</b></span><br>"; 
    $data['price_html'] .= "<span class='inc-vat-price'>Total: <b>" . woocommerce_price($variation->get_price_including_tax()) . "</b></span>";
    return $data; 
    } else {
    $data['price_html'] .= "<span class='regular-price'> " . woocommerce_price($variation->get_price()) . "</span>";
    return $data;
}
} 

i want to change the if parameter to

$taxclass = $product->get_tax_class();

if ( $taxclass == 'costa-rate' ) {

but currently this does not function correctly and displays the regular price data twice


回答1:


Since WooCommerce 3, your code is outdated and with some errors.

I guess that you are using woocommerce_available_variation action hook.

Try the following instead:

add_filter( 'woocommerce_available_variation', 'custom_variation_price', 10, 3 );
function custom_variation_price( $data, $product, $variation ) {
    $price_excl_tax = (float) wc_get_price_excluding_tax( $variation ); // price without VAT
    $price_incl_tax = (float) wc_get_price_including_tax( $variation );  // price with VAT
    $tax_amount     = $price_incl_tax - $price_excl_tax;

    if( $variation->get_tax_class() === 'costa-rate' ) {
        $data['price_html'] = '<span class="ex-vat-price">' . __("Price:") . ' <strong>' . wc_price($price_excl_tax) . '</strong></span><br>
        <span class="tax_amount">' . __("Sales Tax 13%:") . ' <strong>' . wc_price($tax_amount) . '</strong></span><br>
        <span class="inc-vat-price">' . __("Total:") . ' <strong>' . wc_price($price_incl_tax) . '</strong></span>';
    } else {
        $data['price_html'] .= '<span class="regular-price"> ' . wc_price( wc_get_price_to_display( $variation ) ) . '</span>';
    }
    return $data;
}

Code goes in functions.php file of your active child theme (or active theme). It should better works.



来源:https://stackoverflow.com/questions/57478297/display-tax-amount-based-on-specific-tax-class-in-woocommerce-variations

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