Filter uncanny-learndash-groups

ulgm_license_is_downloadable

Filters whether a license is downloadable by modifying the downloadable status before it's determined.

add_filter( 'ulgm_license_is_downloadable', $callback, 10, 2 );

Description

Filters whether a product license is considered downloadable. Developers can use this hook to programmatically control the downloadable status of a license, for example, to disallow downloads under specific conditions. The original downloadable status and the product license object are passed as arguments.


Usage

add_filter( 'ulgm_license_is_downloadable', 'your_function_name', 10, 2 );

Parameters

$this (mixed)
This parameter represents the initial downloadable status of the license.
$this (mixed)
This parameter holds the current downloadable status of the product license.

Return Value

The filtered value.


Examples

/**
 * Example of using the ulgm_license_is_downloadable filter.
 * This example checks if the product is downloadable and also if the user has
 * purchased a specific course associated with this license. If both conditions are met,
 * it allows the product to be considered downloadable.
 *
 * @param bool $is_downloadable The current downloadable status of the product.
 * @param WC_Product_License $license_product The WC_Product_License object.
 * @return bool The modified downloadable status.
 */
add_filter( 'ulgm_license_is_downloadable', function( $is_downloadable, $license_product ) {
    // Ensure we are working with a valid license product object.
    if ( ! is_a( $license_product, 'WC_Product_License' ) ) {
        return $is_downloadable;
    }

    // Get the underlying WooCommerce product.
    $product_id = $license_product->get_product_id();
    $product = wc_get_product( $product_id );

    // If the product is not downloadable by default, no need to proceed.
    if ( ! $product || ! $product->is_downloadable() ) {
        return $is_downloadable;
    }

    // --- Realistic Logic Example ---
    // Let's assume we only want to allow downloads for a specific course ID
    // if the user has also purchased that course.
    // Replace 123 with the actual course product ID.
    $required_course_product_id = 123;

    // Check if the current license product is associated with our specific course.
    // This is a hypothetical check, you'd likely have a more robust way
    // to link licenses to courses in your actual implementation.
    // For this example, we'll assume the product_id of the license *is* the course ID.
    if ( $product_id === $required_course_product_id ) {
        // Check if the current user has purchased this course.
        // You would typically get the current user ID.
        $user_id = get_current_user_id();

        if ( $user_id > 0 ) {
            // Check if the user has purchased the required course.
            // This is a simplified check. In a real scenario, you'd query
            // WooCommerce order data or a custom course management system.
            // For demonstration, we'll use a hypothetical function.
            if ( user_has_purchased_course( $user_id, $required_course_product_id ) ) {
                // User has purchased the course, so the license is downloadable.
                return true;
            } else {
                // User has not purchased the course, so deny download even if product is downloadable.
                return false;
            }
        } else {
            // User is not logged in, cannot confirm purchase, so deny download.
            return false;
        }
    }
    // --- End Realistic Logic Example ---

    // If it's not our specific course or any other conditions apply, return the original status.
    return $is_downloadable;

}, 10, 2 ); // Priority 10, accepts 2 arguments.

// --- Helper function for the example (replace with your actual implementation) ---
/**
 * Hypothetical function to check if a user has purchased a course.
 * In a real plugin, this would query your order data.
 *
 * @param int $user_id The ID of the user.
 * @param int $course_product_id The ID of the course product.
 * @return bool True if the user has purchased the course, false otherwise.
 */
function user_has_purchased_course( $user_id, $course_product_id ) {
    // This is a placeholder. You would need to implement this based on
    // how your plugin tracks course purchases. For example, you might query
    // WC_Order or a custom post type.

    // For demonstration purposes, let's say a user with ID 1 always has purchased course 123.
    if ( $user_id === 1 && $course_product_id === 123 ) {
        return true;
    }

    // Otherwise, assume they haven't purchased it.
    return false;
}

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/includes/woocommerce/wc_product_license.php:77
src/classes/woocommerce/woocommerce-buy-courses.php:338

public function is_downloadable( $context = 'view' ) {
		return apply_filters( 'ulgm_license_is_downloadable', $this->get_prop( 'downloadable', $context ), $this );
	}


Scroll to Top