Search code examples
phpwordpresswoocommerceproduct

Add text after price unless the price is zero, then Call for price in WooCommerce


  1. 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.

  2. 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';
}

Solution

  • 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.