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 allow multiple assignments to domains/devices, depending on the Product License set-up. As a default, when purchasing multiple quantities, the system allows generating multiple license keys, up to the number of the purchased items.

In certain scenarios, the number of keys is required to stay fixed ( e.g. a single key ) independently of the purchased quantity, but the number of the Instances per Keys to update accordingly for the customer order. 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 ('woocommerce_order_item_get_quantity', 'woo_sl_woocommerce_order_item_get_quantity', 99, 2);
    function woo_sl_woocommerce_order_item_get_quantity( $quantity, $order_item_product)
        {
            $force_single_key   =   $order_item_product->get_meta('_woo_sl_force_single_key', TRUE);
            if ( $force_single_key == 'true' )
                return 1;
            
            return $quantity;    
        }

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

After the above code used, when placing an order for a licensed product with 4 quantity, the License Manager interface becomes as follow, there is a single License Key, but allows 4 instances per key:


Category:

By woocommerce-sl, posted on January 22, 2021

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments