"Call for price" Woocommerce的钩子



我使用这个钩子,当产品价格字段为空时,使用"woocommerce_empty_price_html":

add_filter('woocommerce_empty_price_html', 'snaj_replace_text_with_call_for_price');
function snaj_replace_text_with_call_for_price() {
return _e('Call for price','admin_texts_xts-woodmart-options') ;
}

但是我需要价格不是空的,我想通过寻找一个我称之为"call-for-price"的类别来实现这一点。

我不是一个程序员,但我想它应该看起来像这样,使用'woocommerce_get_price_html':

add_filter('woocommerce_get_price_html', 'snaj_replace_text_with_call_for_price');
function snaj_replace_text_with_call_for_price() {
$categories = array( 'call-for-price');
if ( has_term( $categories, 'product_cat', get_the_id() ) ) {
return _e('Call for price','admin_texts_xts-woodmart-options') ;
}
}

但是它不工作!

使用该代码,此类别中的产品按预期运行,但此类别之外的产品不再显示价格!

如果产品显示"品类呼价",是否有办法添加条件"?而且"价格存在"?

提前感谢您的帮助。

您错过了返回超出条件的价格。在过滤器钩子中,你必须从过滤器返回一个值。

应该是这样的。

add_filter('woocommerce_get_price_html', 'snaj_replace_text_with_call_for_price', 100, 2 );
function snaj_replace_text_with_call_for_price( $price, $product ) {
$categories = array( 'call-for-price');
if ( has_term( $categories, 'product_cat', get_the_id() ) ) {
return __('Call for price','admin_texts_xts-woodmart-options') ;
}
return $price;
}

最新更新