WooCommerce中所选产品类别的自定义产品价格后缀



我想在WooCommerce的价格标签后面显示一段文字。我有一个适用于我的函数的代码.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;
}

代码进入函数.php活动子主题(或活动主题(的文件。经过测试并工作。


要排除定义的产品类别,您将替换:

if( has_term( $product_categories, 'product_cat', $product->get_id() ) )

只需:

if( ! has_term( $product_categories, 'product_cat', $product->get_id() ) )

最新更新