Last active
March 28, 2024 14:46
-
-
Save andrewlimaza/87f0851beb5b4ba8490beffbf02b7c77 to your computer and use it in GitHub Desktop.
Change user's Membership Level when a product is purchased in Easy Digital Downloads
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 | |
/** | |
* This recipe will allow you to assign a membership level to members when purchasing an EDD product. | |
* You can do this one of two ways: | |
* | |
* OPTION 1: Product ID and Membership Level | |
* $product_levels = array( | |
27 => 1, //27 is the EDD product ID. 1 is the membership level they get when purchasing product 27 | |
); | |
* | |
* OPTION 2: Product ID with custom Membership Level Array - Change parameters for that level on the fly such as an expiration date. | |
* $product_levels = array( | |
37 => array( | |
'membership_id' => 2, // integer - Required | |
'user_id' => $payment_meta['user_info']['id'], | |
'code_id' => '0', //Discount code | |
'initial_payment' => '0', // float (string) | |
'billing_amount' => '0', // float (string) | |
'cycle_number' => '0', // integer | |
'cycle_period' => '', // string (enum) | |
'billing_limit' => '0', // integer | |
'trial_amount' => '0', // float (string) | |
'trial_limit' => '0', // integer | |
'startdate' => current_time( 'mysql' ), // string (date) | |
'enddate' => '2021-12-31' // string (date) | |
), | |
); | |
* | |
*/ | |
function my_pmpro_change_level_edd_update_payment_status( $payment_id, $new_status, $old_status ) { | |
// Basic payment meta. | |
$payment_meta = edd_get_payment_meta( $payment_id ); | |
// OPTION 1: Set the relationship between EDD product ID and level ID. | |
$product_levels = array( | |
20 => 1, //27 is the EDD product ID. 1 is the membership level they get when purchasing product 27 | |
); | |
$user_id = $payment_meta['user_info']['id']; | |
// Allowed statuses to let people keep their level. | |
if ( $new_status == 'complete' || $new_status == 'publish' || $new_status == 'partially_refunded' ) { | |
// Order is completed or partially refunded. Make sure the person gets/keeps their level. | |
foreach ( $payment_meta['cart_details'] as $key => $item ) { | |
if ( ! empty( $product_levels[$item['id']] ) && ! pmpro_hasMembershipLevel( $product_levels[$item['id']], $payment_meta['user_info']['id'] ) ) { | |
pmpro_changeMembershipLevel( $product_levels[$item['id']], $payment_meta['user_info']['id'] ); // If a user buys the product with ID of 27, change their level to 1. | |
} | |
} | |
} else { | |
foreach ( $payment_meta['cart_details'] as $key => $item ) { | |
if ( ! empty( $product_levels[$item['id']] ) ) { | |
pmpro_cancelMembershipLevel( $product_levels[$item['id']], $payment_meta['user_info']['id'] ); // If a user buys the product with ID of 27, change their level to 1. | |
} | |
} | |
} | |
} | |
add_action( 'edd_update_payment_status', 'my_pmpro_change_level_edd_update_payment_status', 10, 3 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See full guide here (with revised code): https://www.paidmembershipspro.com/give-customers-membership-level-after-edd-purchase/