当产品价格字段为空时,我使用了这个钩子,使用了"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') ;
}但我需要的价格不能是空的,我想通过寻找一个我称之为“价格呼唤”的类别来实现这一目标。
我不是一个程序员,但我想应该是这样的,使用'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') ;
}
}但这不管用!
有了这个代码,这个类别中的产品就像预期的那样,但是这个类别之外的产品不再显示价格了!
有没有人有办法添加“如果产品显示按价格分类”和“价格存在”的条件?
提前谢谢你的帮助。
发布于 2022-06-18 06:40:08
你错过了条件之外的价格。在filters钩子中,必须从筛选器返回一个值。
应该是那样的。
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;
}https://stackoverflow.com/questions/72663338
复制相似问题