ulgm_courses_is_purchasable
Filters whether a course is purchasable, allowing modification before purchase checks.
add_filter( 'ulgm_courses_is_purchasable', $callback, 10, 1 );
Description
Filters whether a course is purchasable. Developers can modify the return value to prevent or allow course purchases based on custom logic. By default, courses are purchasable.
Usage
add_filter( 'ulgm_courses_is_purchasable', 'your_function_name', 10, 1 );
Parameters
-
$this(mixed) - This parameter's initial value is `true`, indicating that a course is purchasable by default.
Return Value
The filtered value.
Examples
/**
* Conditionally make a course not purchasable if it's a free trial and the user has already used their trial.
*/
add_filter( 'ulgm_courses_is_purchasable', function( $purchasable, $course_product ) {
// Check if the course has a 'free_trial' meta key set to true.
if ( $course_product && $course_product->get_meta( 'free_trial' ) === 'yes' ) {
// Get the current logged-in user ID.
$user_id = get_current_user_id();
// If the user is logged in, check if they have already used their free trial for this course.
if ( $user_id ) {
// This is a hypothetical meta key stored on the user object or in a custom table.
// In a real-world scenario, you'd likely have a more robust way to track this,
// perhaps by storing a timestamp or a count against the user and course ID.
$user_trials_used = get_user_meta( $user_id, 'used_free_trial_' . $course_product->get_id(), true );
if ( ! empty( $user_trials_used ) ) {
// User has already used their free trial, so make the course not purchasable.
return false;
}
}
}
// Otherwise, return the original purchasable status.
return $purchasable;
}, 10, 2 );
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_courses.php:97
public function is_purchasable() {
return apply_filters( 'ulgm_courses_is_purchasable', true, $this );
}