简体   繁体   中英

Check if coupon is applied in WooCommerce using coupon shortcode form

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>';
    }
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM