我想在 WooCommerce 中的价格标签后面显示一段文本。我有一个适用于我的functions.php 的代码,但它显示在所有产品类别上。
我想知道是否有人知道如何使此自定义文本仅显示选定的类别;或者甚至更好的是,未选择的产品类别,因为显示文本的产品类别比不显示文本的产品类别多得多。
我的实际代码:
add_filter( 'woocommerce_get_price_html', 'custom_price_message' );
function custom_price_message( $price ) {
$mt = ' per M/T';
return $price . $mt;
}
尝试使用以下代码,对定义的产品类别使用
has_term()
条件函数:
add_filter( 'woocommerce_get_price_html', 'conditional_price_suffix', 20, 2 );
function conditional_price_suffix( $price, $product ) {
// HERE define your product categories (can be IDs, slugs or names)
$product_categories = array('t-shirts','hoodies');
if( has_term( $product_categories, 'product_cat', $product->get_id() ) )
$price .= ' ' . __('per M/T');
return $price;
}
代码位于活动子主题(或活动主题)的 function.php 文件中。已测试并有效。
要排除已定义的产品类别,您将替换:
if( has_term( $product_categories, 'product_cat', $product->get_id() ) )
只需:
if( ! has_term( $product_categories, 'product_cat', $product->get_id() ) )