Skip to content

Instantly share code, notes, and snippets.

@noameppel
Created June 3, 2014 20:02
Show Gist options
  • Save noameppel/476030ee49cea8da1c31 to your computer and use it in GitHub Desktop.
Save noameppel/476030ee49cea8da1c31 to your computer and use it in GitHub Desktop.
Allows Easy Digital Downloads discount codes to be passed via URL.
<?php
/**
Plugin Name: CFS EDD Custom Discount
Plugin URI: http://cleanforest.co
Description: Allows EDD discount codes to be passed via URL.
Author: Noam Eppel
Version: 0.99
Author URI: http://cleanforest.co
License: GNU General Public License (Version 2 - GPLv2)
*/
/**
* cfs_set_newuser_cookie will set a cookie using the value from ?special=value
*/
function cfs_set_newuser_cookie() {
if ( isset($_REQUEST['special']) ) {
setcookie('cfs_discount_cookie', $_REQUEST['special'], time()+60*60*24*3);
}
}
add_action( 'init', 'cfs_set_newuser_cookie');
/**
* Listens for a URL special discount and automatically applies it if present and valid.
* This function is based on edd_listen_for_cart_discount() which will be available in EDD version 2.0
* https://github.com/easydigitaldownloads/Easy-Digital-Downloads/commit/34ebe04f9c2412412a3d027abf2d8cac2a26ab66
*
* @return void
*/
function cfs_edd_listen_for_cart_discount() {
if( empty( $_COOKIE['cfs_discount_cookie'] ) ) {
return;
}
if (isset($_COOKIE['cfs_discount_cookie'])) {
$cookiediscount = sanitize_text_field( $_COOKIE['cfs_discount_cookie'] );
} else {
return;
}
if( edd_is_discount_valid( $cookiediscount ) ) {
edd_set_cart_discount( $cookiediscount );
} else {
return;
}
}
add_action( 'template_redirect', 'cfs_edd_listen_for_cart_discount', 500 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment