Support » Plugin: License Keys for WooCommerce » Set the Activation max to number of licences purchase

  • Resolved merlinxl

    (@merlinxl)


    Hi,
    In my scenario I aim to sell to corporations where 1 person will buy all the licences for the users. This may be any number between 1 and say 250+.
    I would like to be able to give that user 1 key that can be activated x times, rather than x keys that can only be activated 1 time. For the purchaser it is much simpler to distribute 1 key to x users than to manage the distribution of x keys.

    Is it possible to link the number of licences purchased to the maximum activation limit?

    Thanks

Viewing 15 replies - 1 through 15 (of 20 total)
  • Plugin Author 10Quality

    (@10quality)

    The extended (premium) plugin adds the limit option you are looking for. Although, you are also free to look at our code and WP hooks to code it yourself.

    https://www.10quality.com/docs/woocommerce-license-keys/woocommerce/extended-features/

    Regards.

    Thank you, I have purchased the premium plug-in.
    However your answer does not address my question.
    It appears that the activation limit is pre-set and fixed for the product.

    I would like the activation limit set to the number of licences the user purchases.
    That way the user purchases 1 licence key that can be activated for the number of licences they purchase.

    In your system, if a user wants to buy 100 licences, then they have to manage 100 different licence keys. For corporate clients, buying 100 licences is not uncommon.

    I know I could set up multiple products, each with a different number of activations, but it would be simpler for the customer (and me) if there was a way for the user to buy a variable activation licence.

    Thanks

    merlinxl

    (@merlinxl)

    I have looked at the hooks but not being a WP developer I am not sure how to modify the code
    I presume I would use something like

    add_filter(
    ‘woocommerce_license_key_meta_value’,
    function( $meta, $item, $product, $order ) {
    // set activation limit to number of items ordered
    $meta[‘uses’] = ‘xxx’;
    return $meta;
    },
    90,
    4
    );

    I am unsure how to specify the number of products ordered (xxx) and there where to place this code. Unfortunately the link at the top of the filter hooks reference page to customize a license key returns a 404 error.
    Thanks

    Plugin Author 10Quality

    (@10quality)

    You want to set the limit based on the quantity purchased in the order, right?

    Yes it is possible.

    Before looking at the hook below, please read the following article, it will guide you and indicate how to customize the plugin properly:
    https://www.10quality.com/2018/12/17/how-to-customize-wordpress-the-right-way/

    Then apply the following hook:

    
    add_filter(
        'woocommerce_license_key_meta_value',
        function( $meta, $item ) {
    
            // Set limit based on purchased quantity
            $meta['limit'] = $item->get_quantity();
    
            return $meta;
        },
        99,
        2
    );
    

    Documentation hook.

    • This reply was modified 4 months, 3 weeks ago by 10Quality.
    Plugin Author 10Quality

    (@10quality)

    You will also need a way to break the loop, so the plugin does not create more than 1 license keys. For that we will have to add a new hook for you to use. We will be adding it in the following days and notify you through this thread.

    Hi, I had to get a consultant to fix an issue with licence-keys which prevented it working with php7.1. The API was not working and returning the message that the site was experiencing technical difficulties.
    He changed the following line: ~~~
    $uses = $request[‘licensekey’]->uses;
    ~~~ To this: ~~~
    $uses = $request[‘licensekey’]->uses;
    $uses = isarray($uses) ? $uses : array();
    ~~~ This is in file _app/Controllers/ValidatorController.php, around line 89.

    Will you be including this in future updates so that I don’t have to reapply this fix after I upgrade.

    Also any word on the hook you were going to add as mentioned in your previous post? I can’t go live with my product until I have customised the number of uses to match the number of licences purchased.
    Thanks

    @10quality I’m the developer who tweaked file ValidatorController.php. As @merlinxl pointed out, the issue was caused by a missing intialisation of variable $uses.

    In older versions of PHP, using that variable as an array without initialising it first would work, because PHP would try to “fix” the issue. Since PHP 7.1, that is no longer allowed. An array has to be initialised as such, to work correctly. If you have any questions about the patch, please feel free to ask.

    I’m also looking forward to the update for the plugin that allows to generate a single licence key for a product, with multiple activations. Thanks.

    Plugin Author 10Quality

    (@10quality)

    @daigo75 @merlinxl

    Property uses is initialized as an empty array at WooCommerceController.php not ValidatorController.php.

    https://plugins.trac.wordpress.org/browser/woo-license-keys/trunk/app/Controllers/WooCommerceController.php#L197

    There is no need from us to change anything in our code, since vanilla version works great on php 7.1, 7.2 or later.

    If you were getting an error, it is probably because you are replacing the initial value, to a non array, using hook woocommerce_license_key_meta_value.

    In regards to the other hook. We will try to release the update today, regards.

    • This reply was modified 4 months, 1 week ago by 10Quality.
    Plugin Author 10Quality

    (@10quality)

    @merlinxl

    Review the post above in regards to the uses issue, because most likely you are initialized the property as null, causing ValidatorController to break.

    In regards to the new hook, we have added woocommerce_license_key_generate_loop_index filter that will allow you to stop the generation loop right after a license key has been created.

    Sample:

    
    add_filter(
        'woocommerce_license_key_generate_loop_index',
        function( $i, $item ) {
    
            // We set the index to be higher than the product quantity to force
            // the loop to end, preventing it to generate a second license key.
            return $i + $item->get_quantity();
        },
        99,
        2
    );
    

    @10quality I’m afraid that I would have to disagree with you on most points.

    Property uses is initialized as an empty array at WooCommerceController.php not ValidatorController.php.

    Actually, we’re referring to a variable called $uses, which is set in ValidatorController at line 89.

    If you were getting an error, it is probably because you are replacing the initial value, to a non array, using hook woocommerce_license_key_meta_value.

    That’s incorrect. “We” are not replacing anything. The issue occurs because, in some cases, property $request['license_key']->uses is not an array when line 92 of ValidationController.php is reached.

    As far as I can see, the $request array is initialised at line 41 of the same file, and can be altered via filters for hooks woocommerce_license_keys_activate_request and woocommerce_license_keys_activate_request_preval. “We” didn’t write any filter for those hooks, and “we” are not touching the $request array or the value assigned to its uses key.

    I can see that the client didn’t post the full error log, perhaps it might help you finding out what the issue could be and the best way to prevent it:

    
    2019-06-24T16:37:29+00:00 CRITICAL Uncaught Error: [] operator not supported for strings in /path/wp-content/plugins/woo-license-keys/app/Controllers/ValidatorController.php:91
    Stack trace:
    #0 [internal function]: LicenseKeys\Controllers\ValidatorController->activate('')
    #1 /path/wp-content/plugins/woo-license-keys/vendor/10quality/wpmvc-mvc/src/Engine.php(150): call_user_func_array(Array, Array)
    #2 /path/wp-content/plugins/woo-license-keys/vendor/10quality/wpmvc-mvc/src/Engine.php(85): WPMVC\MVC\Engine->run('ValidatorContro...', Array)
    #3 /path/wp-content/plugins/woo-license-keys/vendor/10quality/wpmvc-core/src/psr4/Bridge.php(211): WPMVC\MVC\Engine->call_args('ValidatorContro...', Array)
    #4 /path/wp-includes/class-wp-hook.php(286): WPMVC\Bridge->__call('ValidatorContro...', Array)
    #5 /path/wp-includes/class-wp-hook.php(310): WP_Hook->apply_filters('', Array)
    #6 /path/wp-includes/plugin.php(465): WP_Hook->do_action(Array)
    #7 /path/wp-admin/admin-ajax.php(188): do_action('wp_ajaxnopriv. in /var/www/web/wp-content/plugins/woo-license-keys/app/Controllers/ValidatorController.php on line 91
    
    Plugin Author 10Quality

    (@10quality)

    @daigo75 variable uses in line 92 of ValidationController.php is a property of the license key data model, which is initialized as an empty array when the license key is created, this is done in WooCommerceController.php. Through out our code (regular and paid plugins), we treat this variable as an array; so, from our end, there is no way this variable could have been change to something different than an array.

    This issue points to be caused by a customization you guys made, and we have no reason to patch something that in vanilla (used by 400+ active installs and without your customizations) is working. You need to find out where in your code you guys are replacing this property and provoking the whole thing to crash.

    “We” didn’t write any filter for those hooks, and “we” are not touching the $request array or the value assigned to its uses key.

    Good, still, you may be replacing the initial value of the uses property with the hook woocommerce_license_key_meta_value. The license key data model in the $request is not generated/created in ValidatorController, it is loaded during the endpoint calls within ValidatorController and created on WooCommerceController.php when an order is completed.

    I can see that the client didn’t post the full error log, perhaps it might help you finding out what the issue could be and the best way to prevent it

    As the error suggests, the property is being initialized as a string. So most likely you are guys are overriding the initial empty array value with a string or null during the creation of the license key.

    If you still need to patch the uses, you can always use woocommerce_license_keys_activate_request_preval and make sure that uses is an array.

    As I already explained, we (or, at least, I) didn’t write any code, of any kind, in relation to your solution. I was simply asked to fix the error I forwarded you. That’s my only involvement so far.

    @merlinxl you might want to check if someone else is interfering with the initialisation process. Perhaps there’s another plugin, or some custom code that interferes with the initialisation. Removing it might fix the error you experienced, without having to patch the licensing plugin.

    Hi,
    I do apologise, I appear to have caused some confusion. I expect that when I was trying to figure out how to add a custom hook to amend the uses I must have added some code that caused the API error that ultimately Diego fixed. Initially I tried to follow examples I found online but it always failed when I tried to save it so I assumed I hadn’t amended the code.
    I did not think that my attempts to add a custom filter had broken the API call so when I asked Diego to fix the API issue I did not mention that it might have been me that broke it in the first place.
    I’m pleased to hear that there won’t be an issue when I upgrade, and I am looking forward to the new hook that will allow us to modify the number of uses to the number of licences purchased.
    Thanks for your assistance and understanding, and sorry if I have caused this confusion.

    Plugin Author 10Quality

    (@10quality)

    @merlinxl

    No problem!

    Good thing you found out what was causing the issue.

    Once you fix that, you may still encounter problems with any license keys created prior the fix, since their uses property was initialized as a string|null. So we recommend to add the patch/fix you were suggesting us to apply to our code using hooks.

    Sample:

    
    add_filter( 'woocommerce_license_keys_activate_request_preval', function( $request ) {
        if ( ! is_array( $request['license_key']->uses ) )
            $request['license_key']->uses = [];
        return $request;
    } );
    

    Finally, review the other hook indicated above, to stop loop from iterating and allowing you to just create one license key, as wanted.

    Thanks for all the information. If I may, I would have a question about the solution you suggested to set the activation limit for a licence. I checked the database in a “default” setup, and the $meta['limit'] element is stored as an array, not as an integer. Example

    
    array (
      'code' => 'XXXXXXXXXXXXXXXXXXXX',
      'expire' => 1594236875,
      'uses' => 
      array (
      ),
      'limit' => 
      array (
        'count' => 10,
      ),
      'offline' => 'days,7',
      'ctoken' => 'XXXXXXXXXXXX',
    )
    

    If we try to return an integer, as per your example, the plugin shows “None” for the activation limit (see https://prnt.sc/oelrvu).

    Do we actually have to return an array, such as $meta['limit']['count'] = <activation limit> to make it work?

Viewing 15 replies - 1 through 15 (of 20 total)
  • You must be logged in to reply to this topic.