WooCommerce License Keys allows for custom activation data to be stored during api calls.
Additional activation data might be needed, for instance, in order to save the serial number of a device or similar.
During activation hook
woocommerce_license_key_activation_meta
This filter hook contains all the meta data, related to an activation, that is going to be saved in the database during the activate endpoint.
Parameters
$meta
array
Array holding all the meta data related to the activation. By default: $meta[‘domain’], $meta[‘ip’] and $meta[‘date’] (unix timestamp which also represents the id).
$request
array
Array holding all the data sent in the request (See Customize API for more info).
Retunrs
Filter expects $meta array to be returned back.
Code Sample
For explanation purposes, the next code snippet will use the hook above to add the serial number, captured in the request (see Customize API Request to understand how to capture additional request data), as part of the activation’s meta data.
add_filter( 'woocommerce_license_key_activation_meta', function( $meta, $request ) { $meta['serial_number'] = $request['serial_number']; return $meta; }, 10, 2 );
During validation hook
woocommerce_license_key_activation_validated
The action hook used during the validation endpoint is different, the API doesn’t save any request data during this endpoint, so you will need to add the logic if you wish to do so.
Parameters
$license_key
object | LicenseKeys\Models\LicenseKey
License Key data model.
$activation_id
long
Activation ID.
Code Sample
For explanation purposes, the next code snippet will use the hook above to update the serial number used in the code sample above.
add_action( 'woocommerce_license_key_activation_validated', function( $license_key, $activation_id ) { $uses = $license_key->uses; foreach ( $uses as $key => $activation_meta ) { if ( $activation_meta['date'] === $activation_id ) { $uses[$key]['serial_number'] = \WPMVC\Request::input( 'serial_number' ); break; } } $license_key->uses = $uses; $license_key->save(); }, 10, 2 );
In templates
Once stored, custom activation data can be used and display in templates (see templating).