Increase the License Instances per Key accordingly to purchased quantity

Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInPrint this page

A license can be configured to allow multiple assignments to domains or devices, depending on the Product License setup. By default, when a customer purchases multiple quantities of a product, the system generates a corresponding number of license keys, up to the number of items purchased.

However, there are scenarios where the number of license keys needs to remain fixed (e.g., a single key) regardless of the quantity purchased. In these cases, the number of instances per key should be updated to reflect the total quantity ordered by the customer. This can be particularly useful for software products where a single key should activate multiple installations.

This can be achieved through a programmable filter and a custom code:

<?php

    add_action('woo_sl/generate_licence_keys_count', 'woo_sl_generate_licence_keys_count', 10,  4);
    function woo_sl_generate_licence_keys_count( $generate_keys_count, $order_id, $order_item_id, $license_group_id )
        {
            global $WOO_SL_API;

            //retrieve the order meta data
            $_woo_sl    =   $WOO_SL_API->functions->get_order_item_meta($order_item_id,  '_woo_sl',  TRUE);
            
            //read the max_instances_per_key variable
            $max_instances_per_key      =   $_woo_sl['max_instances_per_key'][$license_group_id];

            $order_data         = new WC_Order($order_id);
            $order_products     = $order_data->get_items();

            $order_item_data    = $order_products[$order_item_id];
            $quantity           = $order_item_data['qty'];
            
            //calculate the required allowed instances per quantity
            $max_instances_per_key = $max_instances_per_key * $quantity;
            
            //update the local variable 
            $_woo_sl['max_instances_per_key'][$license_group_id]    =   $max_instances_per_key;
            
            //update on the server-side
            $WOO_SL_API->functions->update_order_item_meta ( $order_item_id,  '_woo_sl', $_woo_sl );
            
            $WOO_SL_API->functions->update_order_item_meta ( $order_item_id,  '_woo_sl_force_single_key', 'true' );
             
            return $generate_keys_count;
        }


    add_filter ('woo_sl/max_keys/applied_order_quantity', 'woo_sl_max_keys_applied_order_quantity', 99, 2);
    function woo_sl_max_keys_applied_order_quantity( $max_keys, $order_product_data )
        {
            $force_single_key   =   $order_product_data->get_meta('_woo_sl_force_single_key', TRUE);
            if ( $force_single_key == 'true' )
                return 1;
             
            return $max_keys;    
        }

The code should be placed inside a file on /wp-content/mu-plugins/ folder, a custom plugin or theme functions.php

After implementing the above code, placing an order for a licensed product with a quantity of 4 will result in the License Manager interface displaying a follow. This key will allow 4 instances, corresponding to the quantity purchased.


Category:

By woocommerce-sl, posted on May 23, 2024

Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments