要仅显示您的“机器”产品类别的不含税价格,您应该使用挂钩在 wc_price 过滤器挂钩中的自定义函数,这样: p>
add_filter( 'wc_price', 'product_category_price_excl_tax', 10, 3 );
function product_category_price_excl_tax( $price_html, $price, $args ){
global $post;
// Price excluding taxes for "Machines" product category with corresponding label
if( has_term( 'Machines', 'product_cat', $post->ID ) ){
$decimal_separator = wc_get_price_decimal_separator();
$thousand_separator = wc_get_price_thousand_separator();
$decimals = wc_get_price_decimals();
$price_format = get_woocommerce_price_format();
$number_format = number_format( $price, $decimals, $decimal_separator, $thousand_separator );
$negative = $price < 0;
$currency = get_woocommerce_currency();
// Get an instance of the WC_Product object
$product = wc_get_product($post->ID);
// Get the product price excluding taxes
$price = wc_get_price_excluding_tax( $product, $price );
$price = apply_filters( 'raw_woocommerce_price', floatval( $negative ? $price * -1 : $price ) );
$price = apply_filters( 'formatted_woocommerce_price', $number_format, $price, $decimals, $decimal_separator, $thousand_separator );
if ( apply_filters( 'woocommerce_price_trim_zeros', false ) && $decimals > 0 )
$price = wc_trim_zeros( $price );
$formatted_price = ( $negative ? '-' : '' ) . sprintf( $price_format, '<span class="woocommerce-Price-currencySymbol">' . get_woocommerce_currency_symbol( $currency ) . '</span>', $price );
$price_html = '<span class="woocommerce-Price-amount amount">' . $formatted_price . '</span>';
if ( wc_tax_enabled() )
$price_html .= ' <small class="woocommerce-Price-taxLabel tax_label">' . WC()->countries->ex_tax_or_vat() . '</small>';
}
return $price_html;
}
代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件中。
此代码经过测试,适用于 WooCommerce 版本 3+
现在,要在“机器”商店类别档案页面中仅显示该价格,您需要替换:
if( has_term( 'Machines', 'product_cat', $post->ID ) ){
作者:
if( has_term( 'Machines', 'product_cat', $post->ID ) && is_product_category ('machines') ){