Skip to content

Instantly share code, notes, and snippets.

@bappi-d-great
Last active Nov 23, 2015
Embed
What would you like to do?
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