License Key Integration with WooCommerce Checkout
DEPRECATION WARNING - These steps described herein are no longer necessary. Use the WooCommerce settings page in Bright to configure checkout.
Overview
Bright includes the ability to convert orders into multi-seat license keys.
Read This First
No matter what, make sure any customized fields you add are inside the checkout form.
For example a hook like this:
add_action( 'woocommerce_before_checkout_form', 'BrightCustomizations::bright_license_field' );
will not work, as the data won't be posted in the checkout form.
Checkout Customization
The order itself must specify this, and the following adds a field to the checkout allowing the buyer to purchase a license key, as opposed to just gaining access for themselves:
The following can be added to any WordPress plugin to add this field:
add_action( 'woocommerce_after_order_notes', 'bright_license_field' );
function bright_license_field($checkout) {
echo '<div id="bright_license_field"><h2>' . __('License Purchase') . '</h2>';
if (empty($checkout->posted))
$license_key_setting = BrightWoocommerceIntegration::is_cart_a_probable_license_purchase();
else
$license_key_setting = $checkout->get_value( 'bright_license_check' );
woocommerce_form_field( 'bright_license_check', array(
'type' => 'checkbox',
'class' => array('bright_license_check input-checkbox'),
'label' => __('<span title="This creates a reusable license key from your purchase. If it is checked for you, you are purchasing multiple registrations with this order and we recommend you not uncheck this, or you may not be able to transfer your purchased registrations. If it unchecked, checking it will allow you to transfer your purchase to anyone you provide the license key to.">Purchase a shareable license key.</span>'),
'required' => false,
), $license_key_setting);
echo '</div>';
}
Effect on the Checkout Page
With the above code executed via functions.php or plugin code, the license key field becomes available:
WooCommerce Callback Required
NOTE: This is deprecated as is no longer necessary in the latest versions of the plugin.
This bit sets it up so that the order is of type "bright_license" if the above checkbox is checked:
add_action( 'woocommerce_checkout_update_order_meta', 'bright_license_field_update_order_meta' );
function bright_license_field_update_order_meta($order_id) {
if (!empty($_POST['bright_license_check']))
update_post_meta($order_id, 'bright_license', sanitize_text_field($_POST['bright_license_check']));
}
NOTE: This is deprecated as is no longer necessary in the latest versions of the plugin.
How to setup the license key checkbox to default when appropriate.
In the example above on checkout customization, you can define a "default value", which is the last field passed to woocommerce_form_field.
Use the function:
$license_key_setting = BrightWoocommerceIntegration::is_cart_a_probable_license_purchase();
to inquire of Bright whether it expects this to be a license purchase. Any order that resolves to a multiple quantity for linked Bright courses is expected to be a license purchase, and this makes an intelligent default for this field.
Generally it makes sense to default the checkbox for "Purchase a shareable license key" if the shopping cart includes multiple registrations to a course, either by way of the item quantity or any Bright metadata you've defined. The example above checks first that the field hasn't been unset by the user.
Generating A Radio Selection Box With Built-in Documentation
You can generate a radio select box with documentation:
BrightWoocommerceIntegration::addLicenseFieldToCheckout(
$checkout,
array('title' => 'Please select from the following options for using license keys:',
'singletext' => '<strong>I\'m buying training for myself.</strong> (No license key necessary - courses will automatically be added to your My Account Page)',
'multitext' => '<strong>I\'m buying online training for others.</strong> I will receive a license key to share with them, and I can redeem it for myself as well. (My learners will redeem the License Key from the link that I will give to them and register an account if necessary.)',
'tooltip' => 'This provides you with the option to create a license key for the online courses in your cart.<br/><br/>If you select <strong>I\'m buying online training for others</strong>, you will receive a link for these courses that you can share with your learners on the checkout page.<br/><br/>If you want to just take a course yourself directly, select <strong>I\'m buying training for myself</strong>.'
// 'includeMultiMyself' => true
));