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

To create a custom licence key, the woo_sl/generate_license_key filter can be used. This send along 4 arguments:

$license_key
$order_id
$order_item_id
$license_group_id

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:
  • Neil Carthy

    In what file do you put this code?

    • nspcode

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