繁体   English   中英

根据 WooCommerce 变体中的特定税种显示税额

[英]Display tax amount based on specific tax class in WooCommerce variations

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

这目前适用于单个产品 ID,但是尝试为特定的 tax_class 进行这项工作,但无济于事

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' ) {

但目前这不能正常工作并显示两次正常价格数据

从 WooCommerce 3 开始,您的代码已经过时并且存在一些错误。

我猜您正在使用woocommerce_available_variation动作挂钩。

请尝试以下操作:

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 文件中。 它应该更好地工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM