1. Home
  2. Docs
  3. WooCommerce License Keys
  4. Developers
  5. License Key Data Model

License Key Data Model

License Keys data can be accessed and manipulated using the License Key data model.


How to use it?

Add the following use statement at the beginning of your php script:

use LicenseKeys\Models\LicenseKey;

Use the hook woocommerce_license_key after retrieving the data into a variable to ensure you are using the extended features and your own customized properties.

$license_key = apply_filters( 'woocommerce_license_key', $license_key );

More examples of this in the methods.


Properties

the_key
string
Customer license key code.
Usage: $license_key->the_key
READ-ONLY

expire
int
Expiration timestamp (unix time).
Usage: $license_key->expire

expire_date
string
Expiration date (WordPress configured format).
Usage: $license_key->expire_date
READ-ONLY

url
string
“My Account” license key url.
Usage: $license_key->url
READ-ONLY

order_id
int
Order ID.
Usage: $license_key->order_id
READ-ONLY

is_valid
bool
Flag that indicates if license key is valid (active or inactive).
Usage: $license_key->is_valid
READ-ONLY

status
string
‘active’ or ‘inactive’.
Usage: $license_key->status
READ-ONLY

uses
array
Array (collection) of activations.
Usage: $license_key->uses

product
WC_Product
The related product.
Usage: $license_key->product->get_id()
READ-ONLY


Extended Properties

limit
int
Limit string rule.
Usage: $license_key->limit

offline
string
Offline string rule.
Usage: $license_key->offline

subscription_id
int
Subscription ID if using a 3rd party subscription plugin.
Usage: $license_key->subscription_id

subscription_ref
string
Subscription reference if using a 3rd party subscription plugin (i.e. ‘woocommerce-subscription’ or ‘subscriptio’).
Usage: $license_key->subscription_ref

renew_url
string
Action URL used to add a renewal in the cart.
Usage: $license_key->renew_url

extend_url
string
Action URL used to add an extension in the cart.
Usage: $license_key->extend_url


Activations

Activations are stored in the uses property. The following code will show you how to modify them:

$uses = $license_key->uses;
for ( $i = 0; $i <= count( $uses ); $i++ ) {
    if ( $uses[$i]['domain'] == 'http://domain.com' ) {
        $uses[$i]['domain'] = 'https://domain.com';
    }
}
$license_key->uses = $uses;
$license_key->save();

Methods

::find()

Finds a license key by a customer code.

$license_key = LicenseKey::find( $customer_code );

$license_key = apply_filters( 'woocommerce_license_key', $license_key );

Parameters

$customer_code
string
A customer license key code.

Returns

A LicenseKey data model or null if not found.


::fromUser()

Returns all the license keys related to a user ID.

$license_keys = LicenseKey::fromUser( $user_id );

Parameters

$user_id
int
A user ID.

Returns

A Collection of LicenseKey data models

Code Sample

foreach ( LicenseKey::fromUser( $user_id ) as $license_key ) {
    $license_key = apply_filters( 'woocommerce_license_key', $license_key );
    echo $license_key->the_key;
}

::fromOrder()

Returns all the license keys related to an order ID.

$license_keys = LicenseKey::fromOrder( $order_id );

Parameters

$order_id
int
An order ID.

Returns

A Collection of LicenseKey data models

Code Sample

foreach ( LicenseKey::fromOrder( $order_id ) as $license_key ) {
    $license_key = apply_filters( 'woocommerce_license_key', $license_key );
    echo $license_key->the_key;
}

::save()

Saves any changes made to the license key.

$license_key = LicenseKey::find( $customer_code );
$license_key->expire = strtotime( '+1 month' );
$license_key->save()

Customizations

There will be times when you may want to customize the LicenseKey model so it support additional data, or to include additional dynamic properties.

To do this, you will need to create a class that extends from a License Key model:

<?php

use LicenseKeys\Models\LicenseKey;
use LicenseKeys\Traits\FindTrait;

// If you have the premium plugin use these instead:
use LicenseKeysExtended\Models\LicenseKey;
use LicenseKeysExtended\Traits\FindTrait;

class MyLicenseKey extends LicenseKey
{
    use FindTrait;
}

Then make sure you load the script and add it using the license key filter:

add_filter( 'woocommerce_license_key', function( $license_key ) {
    return $license_key ? new MyLicenseKey( $license_key->get_data() ) : $license_key;
}, 99 );

Adding custom data properties

The model will be pretty flexible, it will hold any data given assign to it, but will not save it unless you tell it to.

For example you can call to the property $license_key->color and it will not give you any error:

// A HEX color is assigned
$license_key->color = '#F00';

// #F00 is echoed.
echo $license_key->color;

// THIS WILL NOT SAVE THE DATA
// BECAUSE THE MODEL DOES NOT SUPPORT IT
$license_key->save();

To make your model store the color property you will need to override the to_raw() method in your model, like this:

class MyLicenseKey extends LicenseKey
{
    use FindTrait;
   /**
     * Returns model data to be stored in the database
     * Overrides parent.
     *
     * @return array
     */
    public function to_raw()
    {
        $output = parent::to_raw();
        $output['color'] = $this->color;
        return $output;
    }
}

With this, your model will have the structure needed to support custom properties.

Adding data on creation

While the model allows you and the plugin to manipulate the data, there are times when you may want to add custom data to your properties when the license key is created.

To do this use the following filter hook (data is stored in form of an array):

add_filter(
    'woocommerce_license_key_meta_value',
    function( $meta, $item, $product, $order ) {
        // Default color set to white
        $meta['color'] = '#FFF';
        return $meta;
    },
    90,
    4
);

Was this article helpful to you? Yes No 1