jenny-r
8/27/2013 - 1:26 AM

This is a fully functioning WordPress plugin for Easy Digital Downloads that we use to allow users to choose between year or lifetime licens

This is a fully functioning WordPress plugin for Easy Digital Downloads that we use to allow users to choose between year or lifetime licenses. This assumes you are selling a singular product.

<?php
/*
 * Plugin Name: EDD License Filter
 * Description: Allows us to change license lengths based on pricing options.
 * Author: Eric Binnion
 * Version: 0.1
 */

// This snippet adapted from https://easydigitaldownloads.com/docs/changing-license-expiration-length/

function pw_edd_license_length( $length, $payment_id, $download_id, $license_id ) {
  // Gets information for payment
	$payment = edd_get_payment_meta_cart_details($payment_id);

	// Returns string representation of array when license created and emails it to admin.
	// Uncomment to see what data structure looks like.
	// wp_mail( get_option('admin_email'), 'Payment Dump', print_r($payment, true) );

	// Data structure for my test looks like this
	// Array
	// (
	//     [0] => Array
	//         (
	//             [name] => Engage
	//             [id] => 39
	//             [item_number] => Array
	//                 (
	//                     [id] => 39
	//                     [options] => Array
	//                         (
	//                             [price_id] => 1
	//                         )
	//                 )
	//             [price] => 37.00
	//             [quantity] => 1
	//         )
	// )

	if ( $payment[0]['name'] == 'Engage' && $payment[0]['price'] == '37.00'){
		// For lifetime license, let's add 20 years.
		// If product is still in sale in 20 years, let's pat ourselves on the back!
		return '+20 year';
	}

	// This is the standard
	return '+1 year';
}
add_filter( 'edd_sl_license_exp_length', 'pw_edd_license_length', 10, 4 );

?>