0

I am trying to write a function such that if the price is not zero it should check that the variable price_label_before is zero, then if should return a certain format or else return something else.

E.g 1

price_label_before =USD Currency= KES Price=2000

Return USD 2000

E.g 2 price_label_before= null Currency= KES Price=2000

Return KES 2000

Below is the code:

$currency       =   esc_html( get_option('wp_estate_currency_symbol', '') );

$where_currency =   esc_html( get_option('wp_estate_where_currency_symbol', '') );

$price_label_before    =   floatval ( get_post_meta($post_id, 'property_label_before', true) );  

$price  = floatval   ( get_post_meta($post_id, 'property_price', true) );

if ($price != 0 ) {

    if ($price_label_before = 0) {


    $price =wpestate_show_price($post_id,$currency,$where_currency,1);   
     }
     else {
           $myprice = floatval   ( get_post_meta($post_id, 'property_price', true) );

           $price='<span class="price_label price_label_before1">'.$price_label_before. '&nbsp;' .number_format($myprice).'</span><span class="price_label ">'.$price_label.'</span>';  
     }             

}else{

    $price='';

} 
2
  • And what is the code doing or not doing? Commented Mar 5, 2018 at 19:22
  • @JayBlanchard the code is returning 0 2000 Commented Mar 5, 2018 at 19:24

1 Answer 1

1

Your nested else condition will never be met because you're using the assignment operator in your if statement.

if ($price_label_before = 0) {

Should instead be: if ($price_label_before == 0) {

Edit: Update to reflect empty condition instead of 0 condition

if ($price_label_before == '' ) {

Or:

if ( empty( $price_label_before ) ) {

Right now, it's setting $price_label_before to 0, and then checking if it's 0 (which is always the case)

This is a difference between an Assignment Operator and Comparison Operator (Specifically the equality operator)

As a (partly irrelevant to the question) sidenote, I'd try and work on your code consistency with spacing and whitespace to make your code more legible. It seems like you've got lots of random spaces and breaks in your code that will make it hard to read and maintain as your codebase grows larger.

$currency       =   esc_html( get_option( 'wp_estate_currency_symbol', '' ) );
$where_currency =   esc_html( get_option( 'wp_estate_where_currency_symbol', '' ) );

$price_label_before = floatval( get_post_meta( $post_id, 'property_label_before', true ) );  
$price              = floatval( get_post_meta( $post_id, 'property_price', true ) );

if( $price != 0 ){
    if( $price_label_before == '' ){
        $price = wpestate_show_price( $post_id, $currency, $where_currency, 1 );   
    } else {
        $myprice = floatval( get_post_meta( $post_id, 'property_price', true ) );
        $price   = '<span class="price_label price_label_before1">'. $price_label_before .'&nbsp;'. number_format( $myprice ) .'</span><span class="price_label ">'. $price_label .'</span>';  
    }
} else {
    $price = '';
} 
Sign up to request clarification or add additional context in comments.

3 Comments

this still doesn't seem to work the result that is outputted is still returning USD KES 2000, when it is only meant to be USD 2000 or KES 2000 depending on whether $price_label_before is empty or not
Currently you're comparing it to the value 0 which isn't empty. I've updated the large code block to compare it to an empty value instead
To compare to an empty value, you can use $price_label_before == '' or empty( $price_label_before );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.