Skip to content

Instantly share code, notes, and snippets.

@Lonsdale201
Last active November 28, 2023 22:10
Show Gist options
  • Save Lonsdale201/7a9642c64e9950fafcce14f6405111ab to your computer and use it in GitHub Desktop.
Save Lonsdale201/7a9642c64e9950fafcce14f6405111ab to your computer and use it in GitHub Desktop.
JetEngine - Learndash - Dynamic visibility - Current user current course purchased
// place the code in the child theme functions.php or a custom code snippets plugin.
// how to use
// Go to the Elementor Course single template
// Select your widget, or container whatever.
// Go to the advanced tab, select the JetEngine Dynamic visibility
// Scroll down under the list, where you find a new Learndash Category.
// Select the Current user purchased this course option
// Setup your logic: Show or hide
// So, this condition, based on the fact that the current user has purchased the course they are on.
// The logic is that if they have purchased, you hide the widget or display it.
// example image: https://prnt.sc/vBB3wiiv2Ukm
// Attention! This only works in the course single template!
add_action( 'jet-engine/modules/dynamic-visibility/conditions/register', function( $conditions_manager ) {
class LD_Current_User_Purchased_Current_Course extends \Jet_Engine\Modules\Dynamic_Visibility\Conditions\Base {
public function get_id() {
return 'ld-current-user-purchased-current-course';
}
public function get_name() {
return __( 'Current User Purchased Current Course', 'jet-engine' );
}
public function get_group() {
return 'learndash';
}
public function check( $args = array() ) {
$user_id = get_current_user_id();
if ( !$user_id ) {
return false;
}
global $post;
$course_id = $post->ID;
if ( 'sfwd-courses' != get_post_type( $course_id ) ) {
return false;
}
$user_courses = ld_get_mycourses( $user_id );
$has_purchased = in_array( $course_id, $user_courses );
$type = isset( $args['type'] ) ? $args['type'] : 'show';
if ( 'hide' === $type ) {
return !$has_purchased;
} else {
return $has_purchased;
}
}
public function is_for_fields() {
return false;
}
public function need_value_detect() {
return false;
}
}
$conditions_manager->register_condition( new LD_Current_User_Purchased_Current_Course() );
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment