Skip to content

Instantly share code, notes, and snippets.

@bappi-d-great
Last active November 23, 2015 06:28
Show Gist options
  • Save bappi-d-great/dcc54b72afaf9e6c44f7 to your computer and use it in GitHub Desktop.
Save bappi-d-great/dcc54b72afaf9e6c44f7 to your computer and use it in GitHub Desktop.
MEMBERSHIP 2 PRO - HOW TO APPLY COUPON CODE AUTOMATICALLY
<?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
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment