我设置我的Woocommerce网站,以便输入不含税的价格 .
但是,我想在购物车和产品页面上显示价格包含税,但不包括在结帐页面上 .
问题是,Woocommerce默认允许您选择包含/不包括购物车和结账税 . 但我需要它们单独显示 .
有任何想法吗?
在单个产品页面上使用过滤器 woocommerce_get_price_including_tax . 我将代码复制出函数 get_price_including_tax . 我没有测试过这段代码,但这是你需要做的基本想法 .
woocommerce_get_price_including_tax
get_price_including_tax
function modify_woocommerce_get_price_including_tax( $price, $qty, $product ) { if( ! is_checkout() ) { $tax_rates = WC_Tax::get_rates( $product->get_tax_class() ); $base_tax_rates = WC_Tax::get_base_tax_rates( $product->tax_class ); if ( ! empty( WC()->customer ) && WC()->customer->is_vat_exempt() ) { $base_taxes = WC_Tax::calc_tax( $price * $qty, $base_tax_rates, true ); $base_tax_amount = array_sum( $base_taxes ); $price = round( $price * $qty - $base_tax_amount, wc_get_price_decimals() ); /** * The woocommerce_adjust_non_base_location_prices filter can stop base taxes being taken off when dealing with out of base locations. * e.g. If a product costs 10 including tax, all users will pay 10 regardless of location and taxes. * product feature is experimental @since 2.4.7 and may change in the future. Use at your risk. */ } elseif ( $tax_rates !== $base_tax_rates && apply_filters( 'woocommerce_adjust_non_base_location_prices', true ) ) { $base_taxes = WC_Tax::calc_tax( $price * $qty, $base_tax_rates, true ); $modded_taxes = WC_Tax::calc_tax( ( $price * $qty ) - array_sum( $base_taxes ), $tax_rates, false ); $price = round( ( $price * $qty ) - array_sum( $base_taxes ) + array_sum( $modded_taxes ), wc_get_price_decimals() ); } else { $price = $price * $qty; } } return $price; } add_filter( 'woocommerce_get_price_including_tax', 'modify_woocommerce_get_price_including_tax', 10, 3 );
如果要修改购物车页面上的价格,则需要覆盖子主题中的模板 cart-totals.php .
cart-totals.php
1 回答
在单个产品页面上使用过滤器
woocommerce_get_price_including_tax
. 我将代码复制出函数get_price_including_tax
. 我没有测试过这段代码,但这是你需要做的基本想法 .如果要修改购物车页面上的价格,则需要覆盖子主题中的模板
cart-totals.php
.