Skip to content

Instantly share code, notes, and snippets.

@pippinsplugins
Last active December 10, 2015 18:38
Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save pippinsplugins/4475625 to your computer and use it in GitHub Desktop.
Save pippinsplugins/4475625 to your computer and use it in GitHub Desktop.
<?php
/*
Plugin Name: Easy Digital Downloads - Variable Pricing License Activation Limits
Plugin URL: http://easydigitaldownloads.com/extension/
Description: Limit the number of license activations permitted based on variable prices
Version: 1.0
Author: Pippin Williamson
Author URI: http://pippinsplugins.com
Contributors: mordauk
*/
function pw_edd_sl_license_at_limit( $ret = false, $license_id = 0, $limit = 0, $download_id = 0 ) {
$purchase_id = get_post_meta( $license_id, '_edd_sl_payment_id', true );
$purchase_date = new DateTime( get_post_field( 'post_date', $purchase_id ) );
$limit_date = new DateTime( '2013-01-01' );
if( $purchase_date < $limit_date ) {
// licenses purchased before January 1, 2013 are unlimited
return false;
}
$purchase_details = edd_get_payment_meta_cart_details( $purchase_id );
$price_id = false;
foreach( $purchase_details as $item ) {
if( $item['id'] == $download_id ) {
if( ! empty( $item['item_number']['options'] ) ) {
foreach( $item['item_number']['options'] as $option ) {
$price_id = (int) $option['price_id'];
}
}
}
}
if( $price_id !== false ) {
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;
}
$site_count = absint( get_post_meta( $license_id, '_edd_sl_site_count', true ) );
// check to make sure a limit is in place
if( $limit > 0 ) {
if( $site_count >= absint( $limit ) ) {
$ret = true; // license is at limit
}
}
}
return $ret;
}
add_filter( 'edd_sl_license_at_limit', 'pw_edd_sl_license_at_limit', 10, 4 );
@thefrosty
Copy link

Getting an error on line 27

[29-Mar-2013 11:13:16 UTC] PHP Warning: Invalid argument supplied for foreach()

I think this was after a user activated one, but couldn't activate the second domain.

@mdrubba
Copy link

mdrubba commented Apr 5, 2013

I changed the switch statement to accept only, 1, 2, 3, 4 or 5 activations. For getting this working I need to set the "Limit number of active licenses allowed" to 0 in the download. Need a little bit of time to figure this out. Thanks for the gist. :)

@thefrosty
Copy link

I just added

if ( !$purchase_details )
    return $ret;

@dannyvankooten
Copy link

This gist only works when "limit number of active licenses allowed" is set to 0 for the download, since the $ret variable is then set to true.

This gist doesn't actually change the $ret value when the license is not yet at its activation limit. I've made a few small edits to get this gist to work without the limit active licenses set to 0.

https://gist.github.com/dannyvankooten/6351861

@timersys
Copy link

timersys commented Dec 5, 2013

In my case I had only one product and price_id is not set in the options. A "Illegal string offset Warning" was breaking json

https://gist.github.com/timersys/7804186#file-gistfile1-php

@onetarek
Copy link

I have updated with user interface to put limitation of website usages.
You are able to set site limit with your every price options.
https://gist.github.com/onetarek/8742904

@onetarek
Copy link

onetarek commented Feb 1, 2014

Hi, I have updated "edd-variable-pricing-license-activation-limits" plugin again, now it is working fine for me,
I have also update edd-jvzoo plugin also,
Variable pricing license is working for purchase through jvzoo too.

https://gist.github.com/onetarek/8758817

@jgalea
Copy link

jgalea commented Feb 6, 2014

@onetarek Nice work, I can still activate a plugin on more than one site however, even with it's license set to only one site via the UI you added.

@DevinWalker
Copy link

Looks promising, I'll give it a shot tomorrow and reply back with my findings as well

@DevinWalker
Copy link

So I've tested it and found the same results as @jgalea - licenses are still activated on my end more than the limit. The UI is nice but the API needs to return better results so we can output the information to the user such as "No activated, you have reach the limits (5) of your license... to purchase more yadda yadda..." and return the number of active licenses currently. Right now I can't rely on this patch to do the job.

@ramshengale
Copy link

Do I still need to add this code when we have the "License Activation Limit" setting in the Download Prices meta box?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment