skip to Main Content

I’m trying to add a basic IF-statement to a PHP code snippet but my PHP knowledge is very limited and I didn’t have much success until now (I tried different variations on IF-statements which I found in other code snippets but without much success).

The company who originally created the code explained me where to add the IF-statement, what kind of IF-statement I need and also told me I should work with the variable called $coupon (this variable contains the name of the coupon).

The instructions are shown here in Green: https://www.diffchecker.com/EAk0Gz9u/

I understood it should be a very easy line of code to write for a PHP developer but I didn’t have much success until now since my PHP knowledge is VERY limited.

Is there maybe someone in this amazing community who can help me with this?

Thank you so much in advance!

Luke

2

Answers


  1. As per the instruction you need to check if the coupon code begins with ‘affiliate-credit’ or not. If yes then the IF statement should return true.

    To do so you need to check $coupon if it begins with ‘affiliate-credit’ or not. [As we can see the value of $coupon is set by affwp_get_coupon( $coupon->code ) function]

    The following might help you achieve this goal:
    How to check if a string starts with a specified string?

    If you can share some sample output values of $coupon here, that would be helpful.

    Login or Signup to reply.
  2. Of course, I’d be happy to help you add the IF-statement to your PHP code snippet!

    Based on the instruction you provided, it seems that you need to add a simple IF-statement to check the value of the $coupon variable and execute certain code based on its value. Here’s the code snippet with the IF-statement added:

    <?php
    // Your existing code...
    
    // Assuming $coupon contains the name of the coupon
    $coupon = $_POST['coupon']; // Replace $_POST['coupon'] with the actual source of the coupon value
    
    // Add the IF-statement here
    if ($coupon === 'SUMMER2023') {
        // If the coupon is 'SUMMER2023', do something specific
        // Insert your code here for the 'SUMMER2023' coupon case
        echo "Coupon 'SUMMER2023' applied!"; // For example, this line will display a message
    } else {
        // For all other cases, do something else
        // Insert your code here for other coupon cases or the default case
        echo "Invalid coupon!"; // For example, this line will display a message for invalid coupons
    }
    
    // Continue with the rest of your code...
    ?>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search