1. Home
  2. Docs
  3. WooCommerce License Keys
  4. Developers
  5. Use License Keys within WordPress

Use License Keys within WordPress

Use global functions if you plan to use this plugin to validate license keys within the same WordPress setup.

The functions you will need are:


License Key activation

The function wc_license_key_activate() allows you to pass either a LicenseKey data model or an array with arguments as the parameter.

Using License Key model

You can pass a license key model if you have the SKU and domain validations turned off. Otherwise, the response will return with errors.

// Get a license key data model
$license_key = wc_find_license_key( 'aCustomerKey-777' );

// Activate
$response = wc_license_key_activate( $license_key );

Using an array

You should pass an array as parameter if your configuration expects multiple validations on multiple data (for example SKU or domain).

$response = wc_license_key_activate( [
    'license_key' => 'aCustomerKey-777',
    'sku' => 'PRODUCTSKU',
    'domain' => 'www.customer.domain',
] );

The response

The function will return a response object (WPMVC\Response). The response contains a flag indicating if the operation was successful, the list of errors, and any data associated (see activation endpoint response for more information).

Usage example:

// Activate
$response = wc_license_key_activate( $license_key );

// Check if activation was successful
if ( $response->success ) {

    // License Key has been ACTIVATED

    // The data is an array property
    var_dump( $response->data );

    // Message to user
    echo $response->message;

    // Get the activation ID
    echo $response->data['activation_id'];

} else {

    // The errors is an array property with the list of errors
    // returned by the API's validation.
    var_dump( $response->errors );

}

License Key validation

The function wc_license_key_validate() allows you to pass either a LicenseKey data model or an array with arguments as the first parameter and an activation ID as second (required only if a data model is used).

Using License Key model

You can pass a license key model if you have the SKU and domain validations turned off. Otherwise, the response will return with errors.

// Get a license key data model
$license_key = wc_find_license_key( 'aCustomerKey-777' );

// Validates activation ID 456513547 
$response = wc_license_key_validate( $license_key, 456513547 );

Using an array

You should pass an array as the parameter if your configuration expects multiple validations on multiple data (for example SKU or domain).

$response = wc_license_key_activate( [
    'license_key' => 'aCustomerKey-777',
    'activation_id' => 456513547,
    'sku' => 'PRODUCTSKU',
    'domain' => 'www.customer.domain',
] );

The response

The function will return a response object (WPMVC\Response). The response contains a flag indicating if the operation was successful, the list of errors, and any data associated (see validation endpoint response for more information).

See the usage example for wc_license_key_validate().


License Key deactivation

The function wc_license_key_deactivate() allows you to pass either a LicenseKey data model or an array with arguments as the first parameter and an activation ID as second (required only if a data model is used).

Using License Key model

You can pass a license key model if you have the SKU and domain validations turned off. Otherwise, the response will return with errors.

// Get a license key data model
$license_key = wc_find_license_key( 'aCustomerKey-777' );

// Deactivates activation ID 456513547 
$response = wc_license_key_deactivate( $license_key, 456513547 );

Using an array

You should pass an array as the parameter if your configuration expects multiple validations on multiple data (for example SKU or domain).

$response = wc_license_key_deactivate( [
    'license_key' => 'aCustomerKey-777',
    'activation_id' => 456513547,
    'sku' => 'PRODUCTSKU',
    'domain' => 'www.customer.domain',
] );

The response

The function will return a response object (WPMVC\Response). The response contains a flag indicating if the operation was successful, the list of errors, and any data associated (see deactivation endpoint response for more information).

See the usage example for wc_license_key_validate().

Was this article helpful to you? Yes No