首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >“呼唤价格”钩子为Woocommerce

“呼唤价格”钩子为Woocommerce
EN

Stack Overflow用户
提问于 2022-06-17 18:15:41
回答 1查看 144关注 0票数 0

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

代码语言:javascript
复制
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':

代码语言:javascript
复制
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') ;
}
}

但这不管用!

有了这个代码,这个类别中的产品就像预期的那样,但是这个类别之外的产品不再显示价格了!

有没有人有办法添加“如果产品显示按价格分类”和“价格存在”的条件?

提前谢谢你的帮助。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-06-18 06:40:08

你错过了条件之外的价格。在filters钩子中,必须从筛选器返回一个值。

应该是那样的。

代码语言:javascript
复制
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;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72663338

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档