Skip to content

Instantly share code, notes, and snippets.

@vpratfr
Created February 12, 2014 09:25
Show Gist options
  • Save vpratfr/8952364 to your computer and use it in GitHub Desktop.
Save vpratfr/8952364 to your computer and use it in GitHub Desktop.
EDD - Custom acitivation limit and expiration dates for variable pricing and bundles
/**
* Function to consolidate purchase data because info is missing from edd_get_payment_meta_cart_details
*/
function mlabs_get_product_price_id_for_purchase( $download_id, $payment_id ) {
$purchase_details = edd_get_payment_meta_cart_details( $payment_id, true );
if ( !is_array( $purchase_details ) ) return false;
foreach( $purchase_details as $item ) {
if( $item['id'] == $download_id ) {
// Can be a product directly bought
if( ! empty( $item['item_number']['options'] ) ) {
return (int) $item['item_number']['options']['price_id'];
}
// Can also be part of a bundle
if( $item['in_bundle']==1
&& isset( $item['parent'] )
&& !empty( $item['parent']['options'] ) ) {
return (int) $item['parent']['options']['price_id'];
}
}
}
return false;
}
/***********************************************************************************************************************
*
* Changing License Expiration Length
*
* https://easydigitaldownloads.com/docs/changing-license-expiration-length
*
* License types
* - PRICE 1 = 1 YEAR UPGRADES / SINGLE SITE ACTIVATION
* - PRICE 2 = 2 YEAR UPGRADES / UP TO 5 SITES ACTIVATION
* - PRICE 3 = LIFETIME UPGRADES / UNLIMITED SITES ACTIVATION
*/
function mlabs_edd_license_length( $length, $payment_id, $download_id, $license_id ) {
if( !edd_has_variable_prices( $download_id ) ) {
return $length;
}
$price_id = mlabs_get_product_price_id_for_purchase( $download_id, $payment_id );
if ( $price_id !== false ) {
switch( $price_id ) {
case 0:
return '+1 year'; // single site license
case 1:
return '+2 year'; // up to 5 sites
case 2: {
// Workaround Year2038 PHP's bug
$cur_year = intval( date('Y') );
$max_diff = 2038 - $cur_year - 5;
return '+' . $max_diff . ' year'; // unlimited: AKA year 2037
}
}
}
// Defaults to 1 year otherwise
return '+1 year';
}
add_filter( 'edd_sl_license_exp_length', 'mlabs_edd_license_length', 10, 4 );
function mlabs_edd_get_license_limit( $limit, $download_id, $license_id ) {
if( !edd_has_variable_prices( $download_id ) ) {
return $limit;
}
$payment_id = get_post_meta( $license_id, '_edd_sl_payment_id', true );
$price_id = mlabs_get_product_price_id_for_purchase( $download_id, $payment_id );
switch( $price_id ) {
case 0:
$limit = 1; // single site license
break;
case 1:
$limit = 5; // up to 5 sites
break;
case 2:
$limit = 0; // unlimited
break;
default:
$limit = 1;
}
return $limit;
}
add_filter( 'edd_get_license_limit', 'mlabs_edd_get_license_limit', 10, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment