I'm using this hook which works great when the product price field is empty, using "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') ;
}
But I need the price not to be empty, and I'd like to make this work by looking for a category that I'd call "call-for-price".
I'm not a coder, but I guess it should look like something like that, using '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') ;
}
}
But it doesn't work !
With that code, the product in this category is acting as intended, but products out of this category are not showing prices anymore !
Does anyone have the solution to add the condition "if the product displays the category call-for-price" AND that "the price exists"?
Thanks in advance for your help.
CodePudding user response:
you missed returning the price outside the condition. in filters hook, you must return a value from the filter.
It should be like that.
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;
}