根据WooCommerce变体中的特定税种显示税额 [英] Display tax amount based on specific tax class in WooCommerce variations

查看:77
本文介绍了根据WooCommerce变体中的特定税种显示税额的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用自定义功能来定位特定产品,并在产品页面上更改含税和不含税的价格输出.

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.

目前,该功能可用于单个产品ID,但是尝试将其用于特定的tax_class,而无济于事

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;
}
} 

我想将if参数更改为

$taxclass = $product->get_tax_class();

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

但是目前无法正常运行,并两次显示常规价格数据

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

推荐答案

自WooCommerce 3以来,您的代码已过时且存在一些错误.

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

我猜您正在使用 woocommerce_available_variation 操作挂钩.

I guess that you are using woocommerce_available_variation action hook.

请尝试以下操作:

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;
}

代码进入您的活动子主题(或活动主题)的functions.php文件中.它应该更好地工作.

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

这篇关于根据WooCommerce变体中的特定税种显示税额的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆