0

So I have a function which counts the amount of customer orders.

I want to show a congratulations modal that appears when a customer hits a specific amount of orders.

All of my code is working fine, except when I add this part to the IF statement:

|| in_array($count, $the_digits) && isset( $_COOKIE['tier_advance'] ) && $_COOKIE['tier_advance'] !== $count )

The cookie is being saved properly, and when saved the number is saved as the current value of count. The idea is that the modal will only show if:

A) their current count is in $the_digits array & the cookie is not set. B) their current count is in $the_digits array & the cookie IS set, however the cookies value does not equal their current order $count amount.

This is to prevent the modal showing after the initial display, but still allow it to show again if they hit the next $count amount to move to the next tier.

Can't figure out what is wrong. Or perhaps there is a better way to do this. All help appreciated.

        $the_digits = ['3','5','15','30','50'];

        if ( in_array($count, $the_digits) && !isset( $_COOKIE[ 'tier_advance' ] ) || in_array($count, $the_digits) && isset( $_COOKIE['tier_advance'] ) && $_COOKIE['tier_advance'] !== $count )  {
        echo'
            
            <script>
                    jQuery(window).on("load",function(){
                        jQuery("#tier_advance").modal("show");
                    });
                    jQuery(document).on("click", "#tier_confirm", function(){            
                        // Set a cookie
                        Cookies.set("tier_advance", "' . $count . '", { expires: 356 });
                    });

            </script>
            
            <!-- BEGIN Share Modal -->
            <div class="modal fade" id="tier_advance" tabindex="-1" role="dialog">
              <div class="modal-dialog modal-dialog-centered" role="document">
                <div class="modal-content">
                  <div class="modal-header text-center">
                    <h5>CONGRATULATIONS!</h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                      <span aria-hidden="true">&times;</span>
                    </button>
                  </div>
                  <div class="modal-body text-center">
                  <p>You are now a <strong class="all-caps"> ' . $tier_name . ' </strong>tier customer!</p>
                  <p>Expect some dope rewards to start coming your way!</p>
                  </div>
                  <div class="modal-footer modal-footer-centered">
                    <button id="tier_confirm" type="button" class="btn btn-secondary" data-dismiss="modal">Awesome, thanks!</button>
                  </div>
                </div>
              </div>
            </div>
            <!-- END Share Modal -->';

     }
0

ANSWER

$_COOKIE["tier_advance"] !== "$count")

Note the quotations on the variable. Cookies are saved as strings, therefore without the quotations it was ringing true.

Thanks to @mozboz for cleaning it up and making this more apparent for me.

| improve this answer | |
0

If the problem is with that huge statement with lots of && and || then it's a good idea to simplify it. To refactor your code a bit to make it more understandable (and so you can figure it out you when you look at it in 2 years) you could do something like this:

Before:

    if ( in_array($count, $the_digits) && !isset( $_COOKIE[ 'tier_advance' ] ) || in_array($count, $the_digits) && isset( $_COOKIE['tier_advance'] ) && $_COOKIE['tier_advance'] !== $count )

After:

    $showModal = false;

    if (in_array($count, $the_digits)) {
        if (!isset( $_COOKIE[ 'tier_advance' ] )) {
            $showModal= true;
        } else {
            if ($_COOKIE['tier_advance'] !== $count) {
                $showModal = true;
            } else {
                // echo "false because tier_advance !== count
            }
        } 
    } else {
        // echo "false because not in the_digits";
    }

    if ($showModal) {
          // do the thing
    }

Although this is much longer, it's much easier to understand the logic and it allows you to add some debug statements to it so you can see at what point it set to true/false to debug. I put a couple of echo's in there as examples, but obviously if you can use logging functionality that's better.

EDIT: I just noticed that you're using !== which as you may know does a type check as well as an equality check. I think in this case the cookie might always be a string, so you should try this with just != instead.

| improve this answer | |
  • This is a much cleaner approach, thank you! I need to start writing cleaner code. However, it still seems to not recognize the value of the Cookie and act accordingly... no idea why – Colin G Jul 11 at 15:33
  • I just noticed that the equality check on the cookie might have something to do with it. Oh, and that you've also noticed this is another answer, heh. – mozboz Jul 11 at 17:04
  • 1
    I was not aware of that! So !== equates to is not or is equal to whereas != equates to is not equal to? That is a huge lesson I just learned. Thank you! – Colin G Jul 11 at 18:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.