1. Home
  2. Docs
  3. WooCommerce License Keys
  4. Developers
  5. Filter Hooks Reference

Filter Hooks Reference

WooCommerce License Keys has WordPress hooks all over the place to allow you full customization. The following are the filter hooks we think you will want to use the most, you are free to take a look at the code to find more.


woocommerce_license_key

Used to filter the License Key data model used by WooCommerce License Key. You will need this in order to apply your own custom model and make it visible by the API.

For more information read how to customize a license key model.

Parameters

$license_key
License Key model
A License Key model already loaded with data.

Return

Filter should return a License Key model back.

Example

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

woocommerce_license_key_before_save

Triggered by the API before a License Key model is going to be saved. For example, the API adds a new activation and triggers this filter before saving the changes in the database.

Parameters

$license_key
License Key model
A License Key model already loaded with data.

Return

Filter should return a License Key model back.

Example

add_filter(
    'woocommerce_license_key_before_save',
    function( $license_key ) {

        // Update custom color property
        $license_key->color = '#FFF';

        // Update last activation id
        $license_key->latest_activation_id = $license_key->uses[ count( $license_key->uses )-1 ]['date'];

        return $license_key;
    },
    99
);

woocommerce_license_key_meta_value

Allows you to filter the metadata assigned to the license key during creation before it is being saved in the database. This is useful if you want to add a default custom value, add a prefix to the customer’s code and more.

Parameters

$meta
array
The license key metadata. Default metadata: $meta['code'] (string customer code),
$meta['expire'] (int expiry unix timestamp), $meta['uses'] (array actiations),
$meta['limit'] (int Premium plugin) and $meta['offline'] (string Premium plugin) .

$item
WC_Order_Item
Order item parent.

$product
WC_Product
Related product.

$order
WC_Order
Related order.

Return

Filter should return the meta array back.

Example

add_filter(
    'woocommerce_license_key_meta_value',
    function( $meta, $item, $product, $order ) {

        // Add default custom color property
        $meta['color'] = '#FFF';

        // Add SKU as prefix to the customer's code.
        $meta['code'] = $product->get_sku() . $meta['code'];

        return $meta;
    },
    99,
    4
);

woocommerce_license_key_length

Allows you to filter the length of random characters in the license key.

Parameters

$length
int
Current length. Default is 26.

Return

Filter should return and int number back.

Example

add_filter(
    'woocommerce_license_key_length',
    function( $length ) {

        // Default length is 26, so a key will look like this
        // a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6-101

        // Returning 15 will make the key look this
        // a1b2c3d4e5f6g7h-101
        return 15;
    }
);

woocommerce_license_key_renew

Only available on WooCommerce License Keys extended plugin.

Triggered before a license key is going to be renewed (semi-automatic subscription).

Parameters

$license_key
LicenseKey model
License key data model already filtered with filter hook `woocommerce_license_key`.

$item
WC_Order_Item
Order item.

$order
WC_Order
Order.

Example

add_filter(
    'woocommerce_license_key_renew',
    function( $license_key, $item, $order ) {

        // Modify renewal expiration date
        $license_key->expire = strtotime( '+1 day' );
        
        return $license_key;
    },
    99,
    3
);

woocommerce_license_key_extend

Only available on WooCommerce License Keys extended plugin.

Triggered before a license key is going to be extended (semi-automatic subscription).

Parameters

$license_key
LicenseKey model
License key data model already filtered with filter hook `woocommerce_license_key`.

$item
WC_Order_Item
Order item.

$order
WC_Order
Order.

Example

add_filter(
    'woocommerce_license_key_extend',
    function( $license_key, $item, $order ) {

        // Modify extension expiration date
        $license_key->expire = strtotime( '+1 day' );
        
        return $license_key;
    },
    99,
    3
);
Was this article helpful to you? Yes No