Add myCred points at checkout based on membership level ID in Paid Memberships Pro

Add myCred points at checkout based on membership level ID in Paid Memberships Pro on child sites.

Snippet Type

Execute on Child Sites

Snippet

function add_my_cred_to_pmpro_checkout( $user_id, $morder ) {
	// Bail if myCred doesn't exist.
	if ( ! function_exists( 'mycred_add' ) ) {
		return;
	}

	// Get the user's level and expiration date.
	$level = pmpro_getMembershipLevelForUser( $user_id );
	$level_expiration = $level->enddate;

	// Add these lines to give points to recurring members only.
	if ( ! empty( $level->enddate ) ) {
		return;
	}

    $points = 10; // default to 10 points. If level ID is blank 10 points would be awarded.
    $reference = 'Successful Membership Payment';
    $entry = 'Paid Memberships Pro - level: ' . $level->ID;
    switch( $level->ID ) {
        case 1:
            $points = 20;
            break;
        case 2:
            $points = 15;
            break;
    }

    // Add points to myCred.
    mycred_add( $reference, $user_id, $points, $entry );
    
    // Write to order notes.
    $morder->notes .= ' MyCred points for this order: ' . $points;
    $morder->saveOrder();
    
}
add_action( 'pmpro_after_checkout', 'add_my_cred_to_pmpro_checkout', 10, 2 );