Creating custom format for licence keys on WooCommerce Software License plugin
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; }
what file do you add this code to?
This should be placed within theme functions.php Remember this will work if the plugin cache functionality is turned off.
Thanks. I’ll try it.
There must be a php syntax error in your code, make sure you replace that line with the following:
$license_key_raw_chunks = array_slice($license_key_raw_chunks, 0, 4);