1. Customize
  2. Customizations
  3. Award achievement or rank if user meets any requirement (aka OR requirements)

Award achievement or rank if user meets any requirement (aka OR requirements)

By default, GamiPress just will award an achievement or a rank to the user that meets all requirements (sequentially or not, depending of its configuration).

A desired workflow on some gamification environments is the ability to allow users to earn an achievement or rank if they complete a simple requirement of the list of requirements.

Enabling OR steps for achievements

With the following snippet, you will enable the “OR steps” to the achievement you want:

function my_prefix_award_achievement_if_any_step_completed( $user_id = 0, $step_id = 0 ) {

    // Achievement ID you want to allow to be earned by completing just one of their steps
    $achievement_id = 123;

    if( get_post_type( $step_id ) === 'step' ) {

        $achievement = gamipress_get_step_achievement( $step_id );

        // Check if user has not been already earned this achievement
        if( $achievement->ID === $achievement_id && ! gamipress_has_user_earned_achievement( $achievement->ID, $user_id ) ) {

            // Award the achievement to the user
            gamipress_award_achievement_to_user( $achievement->ID, $user_id );

        }

    }

}
add_action( 'gamipress_award_achievement', 'my_prefix_award_achievement_if_any_step_completed', 10, 2 );

Enabling OR requirements for ranks

There is a similar snippet to the previous one but focused to enable the “OR requirements” to a specific rank:

function my_prefix_award_rank_if_any_requirement_completed( $user_id = 0, $requirement_id = 0 ) {

    // Rank ID you want to allow to be earned by completing just one of their requirements
    $rank_id = 123;

    if( get_post_type( $requirement_id ) === 'rank-requirement' ) {

        $rank = gamipress_get_rank_requirement_rank( $requirement_id );

        // Check if user has not been already earned this rank
        if( $rank->ID === $rank_id && ! gamipress_has_user_earned_rank( $rank->ID, $user_id ) ) {

            // Award the rank to the user
            gamipress_award_rank_to_user( $rank->ID, $user_id );

        }

    }

}
add_action( 'gamipress_award_achievement', 'my_prefix_award_rank_if_any_requirement_completed', 10, 2 );
Was this article helpful to you? Yes No 1

How can we help?