Filter uncanny-learndash-groups

ulgm_license_group_courses_linked_in_order

Filters the order of courses linked within a license group for a specific group ID.

add_filter( 'ulgm_license_group_courses_linked_in_order', $callback, 10, 3 );

Description

This filter hook, `ulgm_license_group_courses_linked_in_order`, allows developers to modify the list of courses linked to a license group before they are processed. It fires after the initial retrieval of linked courses. Developers can use this to add, remove, or reorder courses based on custom logic, such as user roles or specific course availability. The original array of course IDs is passed as the first parameter.


Usage

add_filter( 'ulgm_license_group_courses_linked_in_order', 'your_function_name', 10, 3 );

Parameters

$courses_linked (mixed)
This parameter contains an array of courses that are linked to a specific license group and are intended to be included in an order.
$group_id (mixed)
This parameter contains an array of course IDs that are linked to a specific license group.
$this (mixed)
This parameter holds the ID of the course group being processed.

Return Value

The filtered value.


Examples

add_filter( 'ulgm_license_group_courses_linked_in_order', 'my_custom_ulgm_courses_linked', 10, 3 );

/**
 * Custom function to modify the list of courses linked to a license group for an order.
 *
 * This example adds a specific course to the list if a certain condition is met,
 * for instance, if a custom meta field is set on the order or if the group ID
 * indicates a special type of license.
 *
 * @param array $courses_linked An array of course IDs linked to the license group.
 * @param int   $group_id       The ID of the license group.
 * @param object $instance       The instance of the class that called the filter.
 *
 * @return array The modified array of course IDs.
 */
function my_custom_ulgm_courses_linked( $courses_linked, $group_id, $instance ) {
    // Assume $order is available in this scope or can be retrieved.
    // For demonstration, let's assume we have an order ID and can get the order object.
    // In a real scenario, you'd likely access this from $instance or a global.
    $order_id = 123; // Replace with actual order ID retrieval logic
    $order = wc_get_order( $order_id );

    // If the order doesn't exist, return the original array.
    if ( ! $order ) {
        return $courses_linked;
    }

    // Example condition: Add a specific course if the order has a custom meta key.
    if ( $order->get_meta( '_is_premium_bundle_order' ) === 'yes' ) {
        $premium_course_id = 456; // Replace with the actual ID of the premium course
        if ( ! in_array( $premium_course_id, $courses_linked ) ) {
            $courses_linked[] = $premium_course_id;
        }
    }

    // Example condition: If it's a specific group type, ensure a foundational course is included.
    if ( $group_id === 789 ) { // Replace with a specific group ID
        $foundational_course_id = 101; // Replace with the actual ID of the foundational course
        if ( ! in_array( $foundational_course_id, $courses_linked ) ) {
            $courses_linked[] = $foundational_course_id;
        }
    }

    // You can also remove courses based on conditions.
    // For instance, if a certain add-on course should not be included for specific groups.
    $addon_course_to_remove = 112;
    $key_to_remove = array_search( $addon_course_to_remove, $courses_linked );
    if ( $key_to_remove !== false && $group_id === 1314 ) { // Replace with a specific group ID
        unset( $courses_linked[ $key_to_remove ] );
        $courses_linked = array_values( $courses_linked ); // Re-index array
    }

    return $courses_linked;
}

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-license.php:1030

'language_code'        => $language_code,
				'source_language_code' => null,
			);

			do_action( 'wpml_set_element_language_details', $language_args );
		}

		$courses_linked = apply_filters( 'ulgm_license_group_courses_linked_in_order', $courses_linked, $group_id, $this );

		if ( $courses_linked ) {
			foreach ( $courses_linked as $course_id ) {
				$course_id = apply_filters( 'ulgm_license_group_linked_course_id', $course_id, $group_id, $this );

				ld_update_course_group_access( (int) $course_id, (int) $group_id, false );


Scroll to Top