Creating custom format for licence keys on WooCommerce Software License plugin

Share on FacebookTweet about this on TwitterShare on Google+Share on LinkedInPrint this page

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;