0

I am using a ternary function just to check if a value is empty or not.But its returning a fatal error

Fatal error: Can't use function return value in write context in /home/sommelie/food.toogoodtogo.hu/wp-content/themes/...../woocommerce/config.woocommerce.php on line 551

 $shipping = !empty(get_post_meta( $restaurant_id, 'delivery_charge', true )) ? get_post_meta( $restaurant_id, 'delivery_charge', true ) : '0.00';

What could be a possible error?is there any error, i don't think so because it works on my server but not this current one.

I am not understanding why it's happening.This is a part of a wordpress theme.

GrumpyCrouton
  • 8,486
  • 7
  • 32
  • 71
Hudai
  • 55
  • 1
  • 8
  • What is the PHP version on the server where it doesn't work? – Difster Aug 09 '17 at 16:33
  • "But its returning a fatal error" which issssssssss? It would help us determine the problem if you actually let us know what the error is. – GrumpyCrouton Aug 09 '17 at 16:34
  • This code snippet is incomplete. I tried it and had the error, `Uncaught Error: Call to undefined function get_post_meta()`. Please post a complete code sample and the exact error you're getting. – JBH Aug 09 '17 at 16:34
  • 3
    Possible duplicate of [Can't use method return value in write context](https://stackoverflow.com/questions/1075534/cant-use-method-return-value-in-write-context) – chiliNUT Aug 09 '17 at 16:36
  • @JBH It would be incredibly hard to provide that information more than likely as it would require database structures and probably multiple functions. – GrumpyCrouton Aug 09 '17 at 16:36
  • 1
    you can't call `empty` on a function call before php 5.5 – chiliNUT Aug 09 '17 at 16:36

1 Answers1

0

I had some trouble when trying to use empty with a function call

empty(someFunction());

You should declare a variable to store the post medatata. Or your code will call 2 times the get_post_meta() if the first call return something. It isn't needed :)

 $postMetadata = get_post_meta( $restaurant_id, 'delivery_charge', true );
 $shipping = !empty($postMetadata) ? $postMetadata : '0.00';
JBH
  • 1,823
  • 1
  • 19
  • 30
Mcsky
  • 1,426
  • 1
  • 10
  • 20