Created
October 31, 2019 11:23
-
-
Save kimcoleman/34d5ba06dfbcb2239ddb3c69be1d4f7a to your computer and use it in GitHub Desktop.
Add myCred points at checkout based on membership level ID.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Add myCred points at checkout based on membership level ID. | |
* Optionally apply points only to members that select a recurring membership. | |
* | |
*/ | |
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 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This recipe is included in the blog post on "Award MyCRED Points for Membership" at Paid Memberships Pro here: https://www.paidmembershipspro.com/award-mycred-points-membership-level-checkout/