phpwordpressadvanced-custom-fieldscode-snippets

Acf numeric value filed if empty show message


I have a problem with advanced custom fields.

I have a numeric value field "price" when I don't put a price it displays $0.00 instead of

"please contact us"

despite my css

.price:empty {
  content: 'please contact us';
}

I use this code to format the value

add_filter('acf/format_value/name=property_price', 'fix_number', 20, 3);
function fix_number($value, $post_id, $field) {
$value = number_format((floatval($value)), 2, ',', ',');
return $value;
}

How to solve this problem ?

does php understand empty value as zero?

or do I need to include something extra in my code to tell him if empty = displays a message?

enter image description here enter image description here


Solution

  • add_filter('acf/format_value/name=property_price', 'fix_number', 20, 3);
    function fix_number($value, $post_id, $field) {
      if(empty($value)){
        return 'please contact us';
      }
      $value = '$' . number_format((floatval($value)), 2, ',', ',');
      return $value;
    }