• Resolved dansperfect

    (@dansperfect)


    ok so the code word press is ignoring.

    if ( $totalVal <= 99.99 ){
                            echo '1My Text' . $totalVal;}

    In normal php it works fine but word press ignores the comparing the values and prints the echo regardless. The $totalVal is given by a woocommerce order total cost.

    Is there another way to compare to numerical values in wordpress?

    • This topic was modified 5 years, 10 months ago by dansperfect.
Viewing 13 replies - 1 through 13 (of 13 total)
  • Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    please add “print_r( $totalVal);” to the output. You may have an issue with strings vs. real numbers. “99.99” is a real number, ‘99.99’ is a string.

    Or, try casting $totalVal as a number: if ( (float)$totalVal…

    https://php.net/manual/en/language.types.type-juggling.php

    Thread Starter dansperfect

    (@dansperfect)

    ok will do thank you

    Thread Starter dansperfect

    (@dansperfect)

    I tried both of these and tried:

    <div><?php
            $strpTotal = $total['value'];
          $totalVal = preg_replace("/$/", '', $strpTotal );
            $floatVal = floatval($totalVal);
           if ( $floatVal <= 99.99 ){
                            echo '1My Text' . $totalVal;}
                         ?></div>

    None of it is working for me. This is the full portion of my inserted code. The $total[‘value’] is provided by woocoomerce. It’s an array already in the file. The array only has a label with value of Total and a value with the total sum of all products shipping and tax etc…

    Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    So I asked about doing a print_r. You didn’t do that.

    Thread Starter dansperfect

    (@dansperfect)

    No I said I tried both suggestions. the print_r didn’t change anything it still printed when the value of $totalVal was above 99.99. But while it printed my echo it also printed the print_r before the echo

    Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    It wouldn’t change anything, It would tell you what the variable was.

    Thread Starter dansperfect

    (@dansperfect)

    Well it didn’t change anything except that it reprinted the price in front of the echo and it did tell me what the variable was by printing it before the echo

    Dion

    (@diondesigns)

    It’s possible your totals have been formatted with additional characters, some of which may be non-ASCII. Please try the following and report back the results:

    <div><?php
    $totalVal = preg_replace('/[^0-9.]/u', '', $total['value']);
    if (floatval($totalVal) <= 99.99) {
    	echo '1My Text' . $totalVal;
    }
    else {
    	var_dump($total['value'], $totalVal);
    }
    ?></div>

    The “else” section is for debugging purposes and should be removed after the problem is resolved.

    FYI, since this deals with output from WooCommerce, you should probably be asking your questions in the WooCommerce support area.

    Thread Starter dansperfect

    (@dansperfect)

    This is what is given back by your code:

    string(117) “$50.47” string(7) “3650.47”

    the 36 added to the front is added because of the html for the $ that is why i used preg_replace the way I did. I don’t understand the patterns yet my brain is just not wrapping around it.

    I didn’t ask over at woocommerce because I just didn’t even think about it. My mind went I’m working with wordpress let me go there. I should have but maybe if you can explain to me what [^0.9.] is actually doing for the preg_replace and how I should add the html code for the $ sign to that I believe (I’m hoping it will work)

    • This reply was modified 5 years, 10 months ago by dansperfect.
    Thread Starter dansperfect

    (@dansperfect)

    Ok so I figured out how to add the $ html to the pattern as well so there is no more 36 infront of the total. I would still like to know what the [^0-9.] does. So with that done please see below for both outputs

    Total was less than 99.99
    1My Text50.47

    Total more than 99.99
    1My Text147.22

    Thread Starter dansperfect

    (@dansperfect)

    Ok so I took your debugging statement and added it to my echo so I can always see it and the reason why it always shows is that when I floatVal($totalVal) and I also tried abs($totalVal) it turns the string into a number but only a 0 for both functions. Like below.

    1My Text190.22string(118) “$190.22” float(0)

    Dion

    (@diondesigns)

    Your text from WooCommerce appears to be using HTML entities. You can try this line in the code I provided and see if it works.

    $totalVal = preg_replace('/[^0-9.]/u', '', html_entity_decode($total['value']));
    

    If not, I strongly suggest you ask for help in the WooCommerce support area.

    The regex I used will match all characters other than numbers and the “.” (period) character.

    Thread Starter dansperfect

    (@dansperfect)

    Sweet that worked thank you so much @diondesigns You have helped me understand so much more with just your examples I appreciated it. It’s hard trying to learn this with no guidance or help and most of the books don’t explain much, just tell you type this and type that.

    So once again thank you for taking the time to help me it’s not something that happens everyday at least for me.

Viewing 13 replies - 1 through 13 (of 13 total)
  • The topic ‘Cannot Get word press to obey comparison operators’ is closed to new replies.