下面是对我之前的一个问题的Alter Woocommerce product name when price is on sale个回答代码,即根据销售价格附加产品名称.
现在,我正在try 为具有特定产品标签的产品添加相同的功能:
add_filter( 'the_title', 'change_product_title', 10, 2 );
function change_product_title( $post_title, $post_id ) {
if( get_post_type($post_id) === 'product' ) {
$product = wc_get_product($post_id);
if( is_a($product, 'WC_Product') && $product->get_sale_price() > 0 ) {
$post_title .= ' NET';
}else if( is_a($product, 'WC_Product') && has_tag( $tag = 'Hard Goods', $post = null ) ){
$post_title .= 'NET';
}
}
return $post_title;
}
add_filter( 'woocommerce_product_variation_get_name', 'change_product_name', 10, 2 );
add_filter( 'woocommerce_product_get_name', 'change_product_name', 10, 2 );
function change_product_name( $name, $product ) {
if ( $product->get_sale_price() > 0 ){
$name .= ' NET';
}else if( has_tag( $tag = 'Hard Goods', $post = null )){
$name .= ' NET';
}
return $name;
}
此代码没有产生预期的效果.我从未使用过has_tag函数,我不确定它是否适合这个场景.我try 使用了标签名称的几个版本(Hard-Goods、Hard_Goods等),但都无济于事.