• abreex

    (@abreex)


    add_action( ‘init’, ‘auto_redeem_coupon’ );

    function auto_redeem_coupon() {
    if ( isset( $_GET[‘redeem_coupon’] ) ) {
    $coupon_code = sanitize_text_field( $_GET[‘redeem_coupon’] );

        // Get the WooCommerce coupon
        $coupon = new WC_Coupon( $coupon_code );
    
        if ( $coupon->get_id() ) {
            // Check if coupon exists
            if ( function_exists( 'mycred_add' ) ) {
                $user_id = get_current_user_id();
    
                if ($user_id > 0) { // Ensure the user is logged in
                    // Get coupon discount amount as points
                    $points = $coupon->get_amount();
    
                    // Check if user has already redeemed the coupon
                    $redeemed = get_user_meta($user_id, '_redeemed_' . $coupon_code, true);
                    if (!$redeemed) {
                        mycred_add(
                            'reward_points', // Point type as defined in MyCred
                            $user_id,
                            $points,
                            'Points earned on coupon redemption',
                            ''
                        );
    
                        // Mark the coupon as redeemed for this user
                        update_user_meta($user_id, '_redeemed_' . $coupon_code, true);
    
                        // Display a success message
                        add_action( 'wp_footer', function() use ($points) {
                            echo '<div style="background: #d4edda; color: #155724; padding: 10px; margin: 10px 0; border: 1px solid #c3e6cb; border-radius: 5px;">Your coupon has been successfully redeemed, and you have earned ' . $points . ' points!</div>';
                        });
                    } else {
                        // Display an error message
                        add_action( 'wp_footer', 'display_error_message' );
                    }
                } else {
                    // Redirect user to login page
                    wp_redirect( wp_login_url( home_url( '/redeem-coupon/?redeem_coupon=' . $coupon_code ) ) ); // Redirect to login page
                    exit;
                }
            }
        }
    }

    }

    function display_error_message() {
    echo ‘

    This coupon has already been redeemed.’;
    }

    This snippet automatically redeems a WooCommerce coupon code via a direct URL. I generate a QR code from this URL, and I have set up a QR code scanner on my website. When the QR code is scanned, it automatically redirects to the URL, and the coupon code is redeemed, awarding points to the user. I use the MyCred plugin for points.

    However, I want to modify this setup so that instead of including the entire URL in the QR code, I generate QR codes that only contain the coupon code. Then, the QR code scanner should read only the coupon code and redeem it automatically without needing to include a URL.

    rihgt now i’m using this type url for coupon redeeming function
    https://domain.com/redeem-coupon/?redeem_coupon=J26WC5QR

  • You must be logged in to reply to this topic.