Creating custom format for licence keys on WP 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 up on the order and order item id, in case you need a 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;
          
        }

Create a license in format PREFIX-XXXX-XXXX-XXXX-XXXX all uppercase. The used PREFIX is what defined within the product licensing group area.

    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 )
        {
            $order_product_licence_data =   WOO_SL_functions::get_order_product_licence_details( $order_id, $order_item_id, $license_group_id );               
            $license_prefix             = $order_product_licence_data->license_data['licence_prefix'];
            
            //generate a new key
            $license_key_raw            =   md5( microtime() );
            $license_key_raw_chunks     =   str_split( $license_key_raw , 4 );
            $license_key_raw_chunks     =   array_slice( $license_key_raw_chunks, 0, 4);
                 
                //use chunks
            $license_key    =   implode("-", $license_key_raw_chunks );
            $license_key    =   strtoupper( $license_prefix . '-' . $license_key );


            return $license_key;
        }

Category:
Subscribe
Notify of
guest
2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Neil Carthy

In what file do you put this code?

nspcode

You can create a custom plugin, or include within theme functions.php