Action uncanny-learndash-groups

ulgm_after_license_product_is_created

Fires after a new license product is created, providing access to its ID, object, and array.

add_action( 'ulgm_after_license_product_is_created', $callback, 10, 3 );

Description

Fires after a new license product has been successfully created in WooCommerce. Developers can use this hook to perform custom actions such as adding custom meta data, sending notifications, or integrating with other systems based on the newly created product's ID, the product object, and its array representation.


Usage

add_action( 'ulgm_after_license_product_is_created', 'your_function_name', 10, 3 );

Parameters

$new_product_id (mixed)
The `$new_product_id` parameter contains the unique identifier for the newly created WooCommerce product.
$product (mixed)
The ID of the newly created license product.
$new_product_array (mixed)
This parameter contains the WooCommerce product object that was just created.

Examples

/**
 * Example function to hook into ulgm_after_license_product_is_created.
 * This function demonstrates how to access the newly created product ID,
 * the WooCommerce product object, and the array representation of the product.
 *
 * @param int           $new_product_id       The ID of the newly created product.
 * @param WC_Product    $product              The WC_Product object of the newly created product.
 * @param array         $new_product_array    An array representation of the new product data.
 */
function my_ulgm_handle_new_license_product( $new_product_id, $product, $new_product_array ) {

    // Log the details of the newly created product for debugging purposes.
    error_log( "A new license product has been created with ID: " . $new_product_id );
    error_log( "Product Title: " . $product->get_name() );
    error_log( "Product Type: " . $product->get_type() );
    // error_log( "New Product Array: " . print_r( $new_product_array, true ) ); // Uncomment for detailed array logging

    // Example: Update a custom meta field based on the product type.
    if ( $product->is_type( 'simple' ) ) {
        update_post_meta( $new_product_id, '_my_custom_license_meta', 'simple_product_handled' );
    } elseif ( $product->is_type( 'variable' ) ) {
        update_post_meta( $new_product_id, '_my_custom_license_meta', 'variable_product_handled' );
    }

    // Example: Fetch and potentially modify the license group courses if available in the array.
    if ( isset( $new_product_array['_group_courses'] ) ) {
        $group_courses = $new_product_array['_group_courses'];
        error_log( "Group courses found for product ID " . $new_product_id . ": " . implode( ',', $group_courses ) );
        // You could potentially add logic here to process or modify these group courses.
    }

    // Example: Check if a specific meta field was set in the original array and update accordingly.
    if ( ! empty( $new_product_array['license_expiry_days'] ) ) {
        $expiry_days = intval( $new_product_array['license_expiry_days'] );
        update_post_meta( $new_product_id, 'license_expiry_days', $expiry_days );
        error_log( "Set license expiry days to " . $expiry_days . " for product ID " . $new_product_id );
    }
}
add_action( 'ulgm_after_license_product_is_created', 'my_ulgm_handle_new_license_product', 10, 3 );

Placement

This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.


Source Code

src/classes/woocommerce/woocommerce-buy-courses.php:232

}

		//New product ID
		$new_product_id = $product->get_id();

		update_post_meta( $new_product_id, '_ulgm_version', Utilities::get_version() );

		do_action( 'ulgm_after_license_product_is_created', $new_product_id, $product, $new_product_array );

		update_post_meta( $new_product_id, SharedFunctions::$license_meta_field, $group_courses );
		update_post_meta( $new_product_id, '_uo_custom_buy_product', 'yes' );
		update_post_meta( $new_product_id, '_group_name', $group_name );

		if ( ! empty( $new_min ) ) {
			update_post_meta( $new_product_id, 'ulgm_license_min_qty', $new_min );

Scroll to Top