WooCommerce Integration

Connect WooCommerce products to LicenceForge price tiers so that licenses are created automatically when customers complete a purchase.

Linking Products to Price Tiers

Each LicenceForge price tier has a wc_product_id field that maps it to a WooCommerce product. When an order containing that product is completed, LicenceForge creates a license for the corresponding price tier.

Price tier editor showing the wc_product_id field
The wc_product_id field on a price tier links it to a WooCommerce product or variation.
Product variations

LicenceForge handles both simple product IDs and variation IDs. When the order line item has a variation_id, that value is matched against wc_product_id first. If no match is found, the parent product_id is tried.

Setup steps

  1. Navigate to LicenceForge > Products and select your product.
  2. Open the price tier you want to link.
  3. Enter the WooCommerce product ID (or variation ID) in the WC Product ID field.
  4. Save the price tier.
  5. Create a test order in WooCommerce to verify the license is generated.

Order Triggers

LicenceForge listens to two WooCommerce order status hooks to trigger license creation:

Hook Fires when
woocommerce_order_status_completed Order status changes to Completed
woocommerce_order_status_processing Order status changes to Processing

Both hooks invoke the same license creation routine, so the license is generated whichever status the order reaches first. This ensures customers receive their license key promptly, even if the order is not immediately marked as completed.

License Creation Flow

When an order triggers license creation, LicenceForge follows this sequence:

  1. Acquire lock — A MySQL named lock is obtained via GET_LOCK('wplf_order_{order_id}', 10) to prevent concurrent requests from creating duplicate licenses for the same order.
  2. Check for existing license — LicenceForge queries for a license already associated with this order. If one exists, the process exits early (idempotent).
  3. Match line items — Each order line item is checked against LicenceForge price tiers by comparing the item's product ID and variation ID to wc_product_id values.
  4. Create license — A new license record is created for each matched price tier with the customer's email, product, and tier details.
  5. Store order meta — Metadata is written to the WooCommerce order for reference.
  6. Release lock — The MySQL named lock is released via RELEASE_LOCK().
  7. Fire action — The wplf_license_created_from_woocommerce hook fires, passing the new license and order objects.

Duplicate prevention

Two mechanisms work together to guarantee idempotency:

  • MySQL named lockGET_LOCK() serialises concurrent requests for the same order, preventing race conditions during high traffic or webhook retries.
  • Existence check — Before creating a license, the handler queries for an existing license linked to the order. If found, it skips creation.
// Simplified duplicate prevention logic
$lock_name = 'wplf_order_' . $order_id;
$locked    = $wpdb->get_var(
    $wpdb->prepare( "SELECT GET_LOCK(%s, 10)", $lock_name )
);

if ( ! $locked ) {
    return; // Could not acquire lock
}

try {
    // Check for existing license
    $existing = WPLF_License::find_by_order_id( $order_id );
    if ( $existing ) {
        return; // Already created — idempotent exit
    }

    // Create the license...
} finally {
    $wpdb->query(
        $wpdb->prepare( "SELECT RELEASE_LOCK(%s)", $lock_name )
    );
}

Order Metadata

After license creation, LicenceForge stores the following metadata on the WooCommerce order:

Meta key Description
_wplf_license_id The internal LicenceForge license ID
_wplf_license_key The generated license key string
_wplf_product_name The LicenceForge product name associated with the license

This metadata can be accessed in custom code or viewed in the WooCommerce order admin screen under Custom Fields.

Customer-Facing License Display

License information is surfaced to the customer in three locations using WooCommerce display hooks:

Hook Location Priority
woocommerce_email_order_details Order confirmation and processing emails 20
woocommerce_thankyou Thank-you page shown after checkout Default
woocommerce_order_details_after_order_table My Account > Orders > order detail page Default
License key displayed after purchase
Customers see their license key immediately after completing checkout.

Action Hook

After a license is created from a WooCommerce order, the following action fires:

do_action( 'wplf_license_created_from_woocommerce', $license, $order );
Parameter Type Description
$license WPLF_License The newly created license object
$order WC_Order The WooCommerce order that triggered creation

Use this hook to perform additional processing after license creation, such as sending a custom notification or syncing with an external CRM.

add_action( 'wplf_license_created_from_woocommerce', function ( $license, $order ) {
    // Example: log the new license
    error_log( sprintf(
        'License %s created for order #%d',
        $license->get_key(),
        $order->get_id()
    ) );
}, 10, 2 );
Next step

If you sell subscriptions, see WooCommerce Subscriptions for renewal and status mapping details.