woo_sl/generate_licence_keys_count

Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInPrint this page

Name
woo_sl/generate_licence_keys_count

Type
Filter

Arguments
(int) $keys_count
(int) $order_id
(int) $order_item_id
(int) $license_group_id

Description
Out of the box, only one license key is generated per order item ( $keys_count = 1;). When WooCommerce completes an order, this filter fires just before key generation. Anything you return here replaces that default license keys count.
Return an integer indicating how many license keys you’d like to generate for that line item.

Example of usage
Generate a specific number of license key based on product “Maximum Licence Keys” license option value:

/**
 * Use the 'max_keys' value from the licensing data to determine how many license keys to generate.
 *
 * @param int    $generate_keys_count Number of license keys to generate (default 1).
 * @param int    $order_id            WooCommerce order ID.
 * @param int    $order_item_id       WooCommerce order item ID.
 * @param int $license_group_id                 The base license key string/pattern.
 * @return int                        Number of license keys to generate.
 */
add_filter( 'woo_sl/generate_licence_keys_count', function( $generate_keys_count, $order_id, $order_item_id, $license_group_id ) {
    global $WOO_SL_API;

    // Load the order
    $order = wc_get_order( $order_id );
    if ( ! $order || ! $WOO_SL_API ) {
        return $generate_keys_count;
    }

    // Retrieve the licensing data stored on the order item
    $_woo_sl = $WOO_SL_API->functions->get_order_item_meta( $order_item_id, '_woo_sl', true );

    // Validate and extract the 'max_keys' value for this license key
    if ( is_array( $_woo_sl ) && isset( $_woo_sl['max_keys'][ $license_group_id ] ) ) {
        $max_keys = intval( $_woo_sl['max_keys'][ $license_group_id ] );

        // Ensure a positive key count
        if ( $max_keys > 0 ) {
            return $max_keys;
        }
    }

    // Fallback to default if no valid max_keys found
    return $generate_keys_count;
}, 10, 4 );

Generate a specific number of license key based on purchased quantity:

/**
 * Generate one license key per item quantity.
 *
 * @param int    $generate_keys_count Number of license keys to generate (default 1).
 * @param int    $order_id            WooCommerce order ID.
 * @param int    $order_item_id       WooCommerce order item ID.
 * @param int $license_group_id                 The base license key string/pattern.
 * @return int                        The number of keys to generate (equals item quantity).
 */
add_filter( 'woo_sl/generate_licence_keys_count', function( $generate_keys_count, $order_id, $order_item_id, $license_group_id ) {
    // Load the order
    $order = wc_get_order( $order_id );
    if ( ! $order ) {
        return $generate_keys_count;
    }

    // Get the specific line-item
    $item = $order->get_item( $order_item_id );
    if ( ! $item ) {
        return $generate_keys_count;
    }

    // Get the quantity for this line-item
    $quantity = intval( $item->get_quantity() );

    // Ensure at least one key is generated
    return max( 1, $quantity );
}, 10, 4 );

Generate a specific number of license key for a give Product ID:

/**
 * Generate 5 license keys for a particular product ID.
 */
add_filter( 'woo_sl/generate_licence_keys_count', function( $count, $order_id, $item_id, $license_group_id ) {
    $item = wc_get_order_item_meta( $item_id, '_product_id', true );
    
    // If the product is ID 123, generate 5 keys instead of 1.
    if ( intval( $item ) === 123 ) {
        return 5;
    }

    // Leave all other items at the default.
    return $count;
}, 10, 4 );

By woocommerce-sl, posted on August 2, 2017

Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments