skip to Main Content

So I’m trying to follow this tutorial but what I want to do is a bit different.

I want to have a unique link per coupon generator and the coupon is per customer and can only be used once (e.g. example.com/coupon/7AD8679adO).

Now I want to have a form for this page and just have a input boxes for users like first_name, last_name, and email. And the email field is the identifier that the current url coupon will be registered to that email.

I also tried to research and I found out that there’s URL Coupons feature from Woocommerce (Not sure though if this is exactly what Im looking for), but suddenly, it is not free. So, any idea with this?

2

Answers


  1. I’m pretty sure you’ve seen this answer elsewhere, but I hope this is written clearly enough for you to understand it better.

    It seems as though you may need to implement the "add_rewrite_rule()" in order for WordPress to recognise the element (the unique "/coupon/URL") and pass on the resulting code to the right page.


    Additionally, it can sometimes be a bit more difficult to verify a coupon code without the user being logged into an account that can be relied on.
    Therefore, I would recommend trying to store the uniquely generated "/coupon/URL" in the user’s meta data, and then querying for the unique in the user’s meta data, and validating the request if the current user and the related user’s ID match. This is because the code doesn’t just get tied to the email address, but also to a registered user.

    Login or Signup to reply.
  2. I have implemented rewrite rule in "twentytwentyone" theme, but you may implement it elsewhere in wordpress.

    For more info, please refer to this.
    https://developer.wordpress.org/reference/functions/add_rewrite_rule/

    Implement in wordpress Theme

    1. Add the following code to the bottom of next file

    ~/wp-content/themes/twentytwentyone/functions.php

    // wp-content/themes/twentytwentyone/functions.php
    ...
    // rewrite-rule
    add_action( 'init',  function() {
        add_rewrite_rule( 'coupon/([a-zA-Z0-9]+)[/]?$', 'index.php?coupon=$matches[1]', 'top' );
    } );
    
    // whitelist "coupon" param
    add_filter( 'query_vars', function( $query_vars ) {
        $query_vars[] = 'coupon';
        return $query_vars;
    } );
    
    // set template php file
    add_action( 'template_include', function( $template ) {
        if ( get_query_var( 'coupon' ) == false || get_query_var( 'coupon' ) == '' ) {
            return $template;
        }
        
        //You can return wherever you want to go
        return get_template_directory() . '/template-coupon.php';
    } );
    
    1. Create ~/wp-content/themes/twentytwentyone/template-coupon.php
    // wp-content/themes/twentytwentyone/template-coupon.php
    <?php
    if ($_SERVER['REQUEST_METHOD'] === 'POST') {
        echo get_query_var( 'coupon' )." from POST".PHP_EOL;
        var_dump($_POST);
    
        // check coupon validation here
        // $query = $wpdb->prepare("SELECT 1"); // Your query
        // $result = $wpdb->get_var($query);
        // var_dump($result);
    
    
    } else  {
        echo get_query_var( 'coupon' )." from redirection GET".PHP_EOL;
        // do something
    }
    
    ?>
    
    
    <?php //get_template_part( 'header' ); ?>
    
    <form action="" method="POST">
        <label for="first_name">First Name</label><input type="text" name="first_name" id="first_name"><br/>
        <label for="last_name">Last Name</label><input type="text" name="last_name" id="last_name"><br/>
        <label for="email">Email</label><input type="text" name="email" id="email"><br/>
        <input type="submit">
    </form>
    
    <?php //get_template_part( 'footer' ); ?>
    

    Implement in wordpress Plugin (Hello Dolly)

    1. Add the above step 1 code to the bottom of next file

    ~/wp-content/plugins/hello.php

    change

    return get_template_directory() . '/template-coupon.php';
    

    to

    return __DIR__ . '/template-coupon.php';
    
    1. Add the above step 2 code to ~/wp-content/plugins/template-coupon.php

    Refresh permalinks cache

    When you add/modify URL part in step1, you need to refresh permalniks cache, otherwise wordress does not apply rewrite rule.

    Settings > Permalinks > Save Changes
    enter image description here

    Result

    check http://your-domain/coupon/7AD8679adO/
    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search