Membership 2 Pro - How to apply coupon code automatically

Is there a way to pass a coupon value in the URL when a user registers for a paid membership so they don't need to enter the coupon code? Or is there a hidden input I can use in the button form that would add a coupon value?

  • packjazz
    • The Incredible Code Injector

    I have created a landing page to promote a Black Friday sale for one my membership levels. I want there to be a button on that page that will allow them to register and pay and have the coupon code already applied so they don't need to fill in the coupon.

    • packjazz
      • The Incredible Code Injector

      @Michael Bourne - Right, and I only want the people who hit the landing page to have access to the discount, so changing the price of the membership level as a whole isn't going to work.

      If this isn't possible can we make this a feature request?

  • Luís
    • Support

    Hi @packjazz and @Michael Bourne,

    I hope had been good today!

    Firstly, sorry for the late reply and all the problems it may have caused.

    After checking, I think that we can customize the code to achieve that.

    I flagged the SLS team (our code experts :slight_smile: ) to investigate further and get their valuable feedback.

    Since this team deals with more advanced threads it may take them longer to reply.

    Cheers, Luís

  • Ash
    • WordPress Hacker

    Hello guys,

    Would you please try the following code:

    <?php
    /**
     * Configuration: Just set the query variable name in the first line - DEFINE
     * In the example, I have used coupon.
     * So, in my case the URL would be: http://domain.com/?coupon=AAAA
     * Where AAAA is the M2 coupon
     */
    
    if( ! defined( 'M2_COUPON' ) ) define( 'M2_COUPON', 'coupon' );
    
    add_action( 'init', 'm2_sess_init' );
    function m2_sess_init() {
        if( isset( $_SESSION ) ) {
            session_start();
        }
    }
    
    add_action( 'template_redirect', 'm2_check_coupon' );
    function m2_check_coupon() {
        if( isset( $_REQUEST[M2_COUPON] ) && $_REQUEST[M2_COUPON] != '' ) {
            $_SESSION['M2_COUPON'] = $_REQUEST[M2_COUPON];
        }
    }
    
    add_action( 'wp_footer', 'm2_apply_coupon' );
    function m2_apply_coupon() {
        if( isset( $_SESSION['M2_COUPON'] ) && $_SESSION['M2_COUPON'] != '' ) {
        ?>
        <style>
            .membership-coupon{display: none;}
        </style>
        <script type="text/javascript">
        jQuery(function($) {
            if( ! $('.membership-coupon .ms-alert-box').length ){
                $('#coupon_code').val('<?php echo $_SESSION['M2_COUPON'] ?>');
                $('#apply_coupon_code').click();
            }
        });
        </script>
        <?php
        }
    }

    You just need to config a line to set your own query variable:

    if( ! defined( 'M2_COUPON' ) ) define( 'M2_COUPON', 'coupon' );

    I have used "coupon" as default, so the URL needs to http://domain.com/?coupon=CODE

    It's just ?coupon=CODE needs to be added at the end of the URL.

    You can add those codes in your functions.php in the theme, if you think your theme won’t be changed. Otherwise mu-plugins is the best solution. To use mu-plugins, go to /wp-content/ and find the folder with name 'mu-plugins'. If there is no folder in that name, then create a folder, name it 'mu-plugins', create a file inside that, give any name you like and paste the code in there. You don't need to activate that plugin. Mu-plugins means must use plugins, so it will be activated automatically always. If you use mu-plugins then add a php start tag at the beginning of the code.

    Hope it helps :slight_smile: Please feel free to ask more question if you have any.

    Cheers
    Ash

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.