Add text after price unless the price is zero, then Call for price in WooCommerce
-
I have products with prices that need to all end with "per sq. ft." I want this to only apply to products of a particular category and only when the price is greater than 0.
-
When the product has no price listed I need the price to say "Call for Price" without the square footage text after it.
So here is a start I found online but they dont have any conditions associated with them so they apply to all product all the time.
/* Display "Call for Price" instead of empty price */
add_filter( 'woocommerce_get_price_html', 'custom_price_message' );
function custom_price_message( $price ) {
$vat = ' per sq. ft.';
return $price . $vat;
}
/* Display "Call for Price" instead of empty price */
add_filter('woocommerce_empty_price_html', 'custom_call_for_price');
add_filter('woocommerce_variable_empty_price_html', 'custom_call_for_price');
add_filter('woocommerce_variation_empty_price_html', 'custom_call_for_price');
function custom_call_for_price() {
return 'Call for Price';
}
Solutions
Thank you both for the help. I used both of your code strings and here is what i came up with:
add_filter( 'woocommerce_get_price_html', 'custom_price_message' );
function custom_price_message($price) {
if ( !has_term( 'stone', 'product_cat' ) ) {
return $price;
} elseif (!empty($price)){
$vat = ' per ft<sup>2</sup>';
return $price . $vat;
}else{
return 'Call for Price';
}
}
add_filter( 'woocommerce_get_price_html', 'custom_price_message' );
function custom_price_message($price) {
if(!empty($price)){
$vat = 'Your Text Here';
return $price . $vat;
}else{
return 'CALL FOR PRICE';
}
}
I hope this works.
Similar questions
Hide "Add to cart" button when the product price is zero
I have an event based WordPress website on that I sell tickets using WooCommerce. Is there any way to hide the "add to cart" button for the product having cost zero? Thanks.
How to search and replace matched text with PHP unless found within an HTML tag?
How can I search and replace all occurrences of a word or phrase of a string, unless it's been located within an HTML tag? Currently have this: But that will only work if the word or phrase is found at the start of an HTML attribute.