Get email from signup form to generate a unique coupon to be displayed on thank you page

Question

We use Constant Contact for our direct marketing and I set up a Contact Forms 7 signup form that only takes a user’s email address. The goal is to capture the email address for a user that has signed up, redirect them to a "Thank You" page and generate a custom, single use coupon code for them that is displayed on that page.

Here is my snippet from functions.php:

add_action( 'capture_email', 'create_email_coupon' );
    function create_email_coupon() {
        $user_email = sanitize_email( get_query_var( 'your-email' ) );

        /**
        * Create a coupon programatically
        */
        $coupon_code = $user_email + 'TheSandwich'; // Code
        $amount = '10'; // Amount
        $discount_type = 'percent'; // Type: fixed_cart, percent, fixed_product, percent_product

        $coupon = array(
        'post_title' => $coupon_code,
        'post_content' => '10% Off Your Next Order!',
        'post_status' => 'publish',
        'post_author' => 1,
        'post_type' => 'shop_coupon');
        
        $new_coupon_id = wp_insert_post( $coupon );
        
        // Add meta
        update_post_meta( $new_coupon_id, 'discount_type', $discount_type );
        update_post_meta( $new_coupon_id, 'coupon_amount', $amount );
        update_post_meta( $new_coupon_id, 'individual_use', 'yes' );
        update_post_meta( $new_coupon_id, 'product_ids', '' );
        update_post_meta( $new_coupon_id, 'exclude_product_ids', '' );
        update_post_meta( $new_coupon_id, 'usage_limit', '1' );
        update_post_meta( $new_coupon_id, 'expiry_date', '' );
        update_post_meta( $new_coupon_id, 'apply_before_tax', 'yes' );
        update_post_meta( $new_coupon_id, 'free_shipping', 'no' );

        return $coupon_code;
}

And here is the interstitial:

jQuery(document).ready(function($){
    var session_cookie = Cookies.get('rj_visit1');
    var signupSheet = "{staging-site}/sandwich-signup/"; // I realize this is not functional, but the site is password protected and I am nervous about posting it on the www!
    
    if(!session_cookie){
        Cookies.set('rj_visit1', '1');
        jQuery('#page').before(signupModal).hide().fadeIn('slow');
        jQuery("#signup-modal").load(signupSheet);
        }
    else{
    jQuery('#signupModal').hide();
    }

    });
    
    var signupModal = `
<div id="signup-overlay" style="z-index:10001; height:100%; width:100%; position:fixed; padding-top:5em; background-color:rgba(255,255,255,0.7);">
    <div id="signupModal" class="modal fade" tabindex="-1" role="dialog" style="opacity:1;">
      <div class="modal-dialog" role="document">
        <div class="modal-content" id="signup-modal">
        
        </div>
      </div>
    </div>
</div>
`;

And here is "thank-you":

<h2> Thank you for signing up for our newsletter <em> The Sandwich </em></h2>
<?php do_action('capture_email', ); ?>
[get_param param="your-email"] // By my understanding, according to the plugin, this should be preserving the email to be picked up on the next page by get_query_var() ! 
<p> We would like to offer you this coupon code:</p>
    <div id="email-coupon-code"></div>
<p> It's good for 10% off you next order through our online store.</p>
<p><strong> Keep an eye on your inbox for the latest news from Ruby Jewel!</strong></p>

Finally, the "sandwich-signup" page just has the shortcode for the form:

[contact-form-7 404 "Not Found"]

This is what "sandwich-signup" looks like:
SignUpImage

Here is a shot of "thank-you":
ThankYouImage

Unfortunately, I can’t run it through my VSCode Debugger on localhost to figure out exactly what is not happening as I expect. I am a new dev with >1 yr WP experience. Without the community here at StackOverflow and StackExchange I would not know as much as I do, so thank you to all of you public servants answering these questions. Does anyone have a suggestion? Any thoughts would be greatly appreciated!

0
RJAdmin 2 years 2020-12-20T18:10:37-05:00 0 Answers 9 views 0

Leave an answer

Browse
Browse