Creating custom format for licence keys on WooCommerce Software License plugin

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

By woocommerce-sl,posted on August 1, 2017

Name
woo_sl/generate_license_key

Type
Filter

Arguments
(text) $license_key
(int) $order_id
(int) $order_item_id
(int) $license_group_id

Description
To create a custom licence key.

Example of usage
A simple example to create keys in a form of XXXX-XXXX-XXXX :

    add_filter('woo_sl/generate_license_key', 'my_custom_licence_keys', 10, 4);
    function my_custom_licence_keys( $license_key, $order_id, $order_item_id, $license_group_id )
        {
            
            //at this point you should place your code to check upon the order and order item id, in case you need different format or anything else
            /**
            * 
            *  custom code
            */
            
            
            //generate a new key
            $license_key_raw            =   md5(microtime() . $order_id . $order_item_id . $license_group_id);
            $license_key_raw_chunks     =   str_split($license_key_raw, 4);
            $license_key_raw_chunks     =   array_slice($license_key_raw_chunks, 0, 3);
            
            //use chunks
            $license_key = implode("-", $license_key_raw_chunks);


           return $license_key;
          
        }


Category: