【发布时间】:2019-12-20 01:02:03
【问题描述】:
我目前正在使用自定义函数来定位特定产品,并在产品页面上更改含税和不含税价格的输出。
这目前适用于单个产品 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_available_variation)...
标签: php wordpress woocommerce hook-woocommerce tax