Creating Custom License Key Logic

This article covers how to replace the built-in logic on whether an order will result in a license key.

By default, Bright runs the following callback after the order is created:

function bright_set_license_field_on_checkout($order_id) {
  $bright = \Bright\Wordpress::getInstance();

  if (get_option(OPTION_ALWAYS_USE_LICENSE_KEYS))
    return set_license_order($order_id,'1');
  if (!empty($_POST['bright_license_check']))
    return set_license_order($order_id,sanitize_text_field($_POST['bright_license_check']));
}

The important thing about this block
* if the site is setup to "ALWAYS USE LICENSE KEYS", the order is flagged as a license order. * otherwise, the value of the bright_license_check input field is used to determine if it is a license order.

First, from a plugin or functions.php, swap out the above function with your own

add_filter('bright_set_license_field_on_checkout', function ($function_name) {
  return 'my_custom_license_key_check';
}, 10, 1);


function my_custom_license_key_check($order_id) {
  $bright = \Bright\Wordpress::getInstance();

  $custom_field_name = 'endastbestallare';
  $value = $_POST[$custom_field_name];

  $multiple_bright_products = \Bright\WC\is_cart_a_probable_license_purchase();

  if ($multiple_bright_products || $value == "Nej")
    return \Bright\Wc\set_license_order($order_id,'1');
  return null;
}

In the above example, we are looking for a "checkout" POST form field "endastbestallare" and using data from there to determine if the license key field should be set.

Note: is_cart_a_probable_license_purchase() returns true if there are Bright courses in the cart AND if the quantity of those products is > 1.