2

I have a shortcode (which works fine) that applies coupons and it does so using custom messages. Everything works fine except for one thing: if a coupon is applied and the user makes the "mistake" of trying to apply it again - it gives the error that the coupon does not exist.

I'm trying to modify this behavior, but with my latest update the output (the coupon input and submit button) is not even rendered.

I have the error and success messages, and they both work fine. What I need is a third message for when the user / customer tries to apply an already applied coupon.

Examples:

Customer / User applies "winter" and get a discount of 10%. The success messages is shown.

Customer / User applies "summer" and gets and error message because that coupon does not exist.

Customer / User applies "winter" again and get the "already applied" message. Why? Because that coupon has already been applied.

Hopefully this makes sense to you all. Here's the code I'm using:

add_shortcode( 'coupon', 'redeem_coupon_form' );
function redeem_coupon_form() {

    if ( is_wc_endpoint_url( 'order-received' ) ) return;
    
    if ( isset( $_GET['coupon'] ) && isset( $_GET['redeem-coupon'] ) ) {

        if ( $coupon = esc_attr( $_GET['coupon'] ) ) {
        
            $applied = WC()->cart->apply_coupon($coupon);
            
            $applied_coupon = WC()->cart->get_applied_coupons();

            } else {

                $coupon = false;
            }

    $success = sprintf( __('<br><p class="coupon-code-does-exist">Thanks for applying the "%s" coupon<br />Please make sure everything checks out before paying.</p>'), $coupon);

    $error = sprintf( __('<br><p class="coupon-code-does-not-exist">Ops! What happened? The "%s" coupon does not exist.<br />Please check the coupon name, alternative - your spelling.</p>'), $coupon);

    $already_applied = sprintf( __('<br><p class="coupon-code-already-applied">You have already applied "%s" and can therefore not apply it again.</p>'), $coupon);

        if ( $applied == $applied_coupon ) {

            $message = $already_applied;

        } else {

            $message = isset( $applied ) && $applied ? $success : $error;

        wc_clear_notices();
    }

    $output  = '<div class="redeem-coupon"><form id="coupon-redeem">
                <input style="display:block; width: 65%; margin-right: 1%; height: 50px; border:1px solid #333; padding: 0px; float: left;" type="text" name="coupon" id="coupon" placeholder="&#32;&#32;Coupon Code Here" />
                <input style="display:block; margin: 0px;  width: 34%; height: 51px; padding: 0px;" type="submit" class="redeem-coupon-button" name="redeem-coupon" value="'.__('Redeem Coupon').'" />';

    $output .= isset( $coupon ) ? '<p class="result"> ' . $message . ' </p>' : '';
    
        return $output . '</form></div>';
    }
}
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399

1 Answers1

1

There are some mistakes in your code, Try the following instead:

add_shortcode( 'coupon', 'redeem_coupon_form' );
function redeem_coupon_form() {
    if ( is_wc_endpoint_url( 'order-received' ) ||  is_wc_endpoint_url( 'order-pay' ) )
        return;

    if ( isset( $_GET['coupon'] ) && isset( $_GET['redeem-coupon'] ) ) {
        $coupon          = esc_attr( $_GET['coupon'] );
        $applied_coupons = WC()->cart->get_applied_coupons();

        if ( ! in_array( $coupon, $applied_coupons ) ) {
            $applied = WC()->cart->apply_coupon($coupon);

            if ( $applied ) {
                $message = sprintf( __('<br><p class="coupon-code-does-exist">Thanks for applying the "%s" coupon<br />Please make sure everything checks out before paying.</p>'), $coupon);
            } else {
                $message = sprintf( __('<br><p class="coupon-code-does-not-exist">Ops! What happened? The "%s" coupon does not exist.<br />Please check the coupon name, alternative - your spelling.</p>'), $coupon);
            }
        } else {
             $message = sprintf( __('<br><p class="coupon-code-already-applied">You have already applied "%s" and can therefore not apply it again.</p>'), $coupon);
        }
        wc_clear_notices();
    }

    $output  = '<div class="redeem-coupon">
        <form id="coupon-redeem">
            <input style="display:block; width: 65%; margin-right: 1%; height: 50px; border:1px solid #333; padding: 0px; float: left;" type="text" name="coupon" id="coupon" placeholder="&#32;&#32;Coupon Code Here" />
            <input style="display:block; margin: 0px;  width: 34%; height: 51px; padding: 0px;" type="submit" class="redeem-coupon-button" name="redeem-coupon" value="'.__('Redeem Coupon').'" />';

    $output .= isset($coupon) && isset($message) ? '<p class="result"> ' . $message . ' </p>' : '';

    return $output . '</form></div>';
}

Code goes in functions.php file of the active child theme (or active theme). Tested and works.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Hey again and thanks for you help. Testing your code, I get a notice message saying: `Notice: Undefined variable: message` –  Jan 03 '21 at 08:17
  • Yes, the notice is gone. Problem is; it does not apply coupons that exist. When attempting to apply an existing coupon (that has not been applied before), I get the error message. –  Jan 03 '21 at 08:38
  • Seems to be working okay:ish. I did a fresh test (no cookies etc). Problem now, when trying to apply a coupon that has already been applied, is this: `Notice: Undefined variable: message` which refers to this: `$message .= sprintf( __('

    You have already applied "%s" and can therefore not apply it again.

    '), $coupon);`
    –  Jan 03 '21 at 08:46
  • So I removed the . before the = sign and the notice is gone. Just want to make sure that is the correct solution here. –  Jan 03 '21 at 08:47