-2

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).

The company that originally created the code explained me in the code snippet where to add the IF-statement, what kind of IF-statement I need (if the coupon code begins with 'affiliate-credit' return true) and also told me I should work with the variable called $coupon (this variable contains the name of the coupon).

Here is the code snippet:

<?php
/**
 * Plugin Name: AffiliateWP - Block WooCommerce Assigned Affiliate Coupon If Affiliate Purchasing
 * Plugin URI: http://affiliatewp.com
 * Description: Prevents assigned affiliate coupon codes from being used on the WooCommerce order if the affiliate is the purchaser
 * Author: AffiliateWP
 * Author URI: https://affiliatewp.com
 * Version: 1.0
 */
/**
 * Make assigned coupon invalid if customer is an affiliate.
 */
function affwp_custom_block_affiliate_coupon_if_customer_affiliate( $condition, $coupon, $vars ) {
    
    if ( ! function_exists( 'affiliate_wp' ) ) {
        return $condition; // Make sure AffiliateWP is installed, otherwise just return.
    }
    $current_user_id = get_current_user_id();
    
    if ( empty( $current_user_id ) ) {
        return $condition; // Probably not logged in, just return.
    }
    if ( ! affwp_is_affiliate( $current_user_id ) ) {
        return $condition; // Not an affiliate, just return.    
    }
    
    if ( empty( $coupon->id ) ) {
        $coupon = affwp_get_coupon( $coupon->code );
// Add IF statement here that if the coupon code begins with 'affiliate-credit' return true to allow store credit coupons.
        if ( false !== $coupon && affwp_get_affiliate_user_id( $coupon->affiliate_id ) === $current_user_id ) {
            return false; // Dynamic coupon assigned to this affiliate, disallow the coupon.
        }
    }
    $is_tracked_coupon = get_post_meta( $coupon->id, 'affwp_discount_affiliate', true );
    if ( affwp_get_affiliate_user_id( $is_tracked_coupon ) === $current_user_id ) {
        return false; // Coupon assigned to this affiliate, disallow the coupon.
    }
    return $condition;
}
add_filter( 'woocommerce_coupon_is_valid', 'affwp_custom_block_affiliate_coupon_if_customer_affiliate', 10, 3 );

To give you some more info about what this code snippet does:

  • This code snippet blocks affiliates to use discount coupon codes for new clients themselves (because they already got a discount when registering)

What I'm trying to accomplish by adding that IF-statement:

  • Add an exception to the code snippet so that coupons for which the coupon code starts with the text 'affiliate-credit' are not blocked (so an affiliate is allowed to use that coupon).

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.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
LukeBo
  • 1
  • 1
  • 1
    Please put a [mre] in the question itself, not a link to an external site. – Barmar Aug 01 '23 at 00:13
  • @Barmar Can you please check the question again? I did an edit like you requested almost a week ago. Thank you. – LukeBo Aug 07 '23 at 12:45
  • What did you try and where are you stuck, exactly? Show your attempt and explain what difficulty you encountered. Whether the solution is simple or not, this site isn't a replacement for learning the basics and making an attempt. If you need someone to write all your code for you, hire a programmer for money. Stackoverflow is for fellow programmers to collaborate and assist each other with programming problems. The questions should be just as useful as the answers, not merely requests for free labour. See also the [tour] and [ask]. Thanks. – ADyson Aug 17 '23 at 14:00

2 Answers2

0

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.

Kaysar
  • 43
  • 6
  • Hi @Kaysar, thank you for your reply! An example output value of `$coupon` beginning with 'affiliate-credit' is for example 'affiliate-credit-202308151438_44'. It can also have other output values like 'coupon123' or 'DADJGDLFSL". It would be great if you could help me further with the code. Thank you in advance! – LukeBo Aug 15 '23 at 14:03
  • Hi @LukeBo, it's my pleasure. However, as per your example to check if it is starting with 'affiliate-credit' or not I have already shared the link in the answer where it is explained how to check it. Hope you have managed to solve it. – Kaysar Aug 15 '23 at 15:58
  • Thank you for your reply @Kaysar Unfortunately my PHP knowledge is very limited. It's still too complicated for me. Like I understood from the company that originally created the code, the extra code I need to get the result we need for this is only 1 or 2 lines max. According to them a very easy job for a PHP developer. Could you maybe provide that piece of code? I would be very grateful for this! – LukeBo Aug 15 '23 at 20:46
-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...
?>
SunilDahal
  • 10
  • 3
  • 4
    Hi, SunilDahal! All three of your answers, and most of your comments, appear likely to be entirely or partially written by AI (e.g., ChatGPT). Please be aware that [posting AI-generated content is not allowed here](//meta.stackoverflow.com/q/421831). If you used an AI tool to assist with any answer, I would encourage you to delete it. We do hope you'll stick around and continue to be a valuable part of our community by posting *your own* quality content. Thanks! – NotTheDr01ds Aug 05 '23 at 02:19
  • 2
    **Readers should review this answer carefully and critically, as AI-generated information often contains fundamental errors and misinformation.** If you observe quality issues and/or have reason to believe that this answer was generated by AI, please leave feedback accordingly. – NotTheDr01ds Aug 05 '23 at 02:19