Skip to content

Instantly share code, notes, and snippets.

@JarrydLong
Last active August 26, 2022 06:37
Show Gist options
  • Save JarrydLong/2f870f552c769a7f8da2c8cbefbd1591 to your computer and use it in GitHub Desktop.
Save JarrydLong/2f870f552c769a7f8da2c8cbefbd1591 to your computer and use it in GitHub Desktop.
<?php //do not copy
/**
* This recipe will allow you to assign a membership level to members when purchasing an EDD product.
*/
function my_pmpro_change_level_edd_update_payment_status( $order_id, $payment, $customer ) {
global $wpdb;
// Get the order from EDD.
$order = edd_get_order( $order_id );
$sql = "SELECT * FROM `$wpdb->edd_order_items` WHERE `order_id` = '". intval( $order_id )."'";
$results = $wpdb->get_results( $sql );
// Product ID with custom Membership Level Array - Change parameters for that level on the fly such as an expiration date.
$product_levels = array(
86150 => array(
'membership_id' => 9, // integer - Required.
'user_id' => $order->user_id,
'initial_payment' => '156.00', // float (string).
'billing_amount' => '156.00', // float (string).
'cycle_period' => 'Year',
'cycle_number' => 1,
'startdate' => current_time( 'mysql' ), // string (date).
'enddate' => date( 'Y-m-d 00:00:00', strtotime( '+1 year' ) ),
),
86194 => array(
'membership_id' => 8, // integer - Required.
'user_id' => $order->user_id,
'initial_payment' => '120.00', // float (string).
'billing_amount' => '120.00', // float (string).
'cycle_period' => 'Year',
'cycle_number' => 1,
'startdate' => current_time( 'mysql' ), // string (date).
'enddate' => date( 'Y-m-d 00:00:00', strtotime( '+1 year' ) ),
),
86195 => array(
'membership_id' => 7, // integer - Required.
'user_id' => $order->user_id,
'initial_payment' => '96.00', // float (string).
'billing_amount' => '96.00', // float (string).
'cycle_period' => 'Year',
'cycle_number' => 1,
'startdate' => current_time( 'mysql' ), // string (date).
'enddate' => date( 'Y-m-d 00:00:00', strtotime( '+1 year' ) ),
),
);
$create_pmpro_order = false;
$pmpro_level = '';
// Order is complete.
if( ! empty( $results ) ) {
foreach ( $results as $order_item ) {
if ( ! empty( $product_levels[ $order_item->product_id ] ) ) {
pmpro_changeMembershipLevel( $product_levels[ $order_item->product_id ], $order->user_id ); // If a user buys the product with ID of 27, change their level to 1.
$create_pmpro_order = true;
$pmpro_level = $product_levels[ $order_item->product_id ];
}
}
} else {
if ( ! empty( $product_levels[ $order->product_id ] ) ) {
pmpro_changeMembershipLevel( $product_levels[ $order->product_id ], $order->user_id ); // If a user buys the product with ID of 27, change their level to 1.
$create_pmpro_order = true;
$pmpro_level = $product_levels[ $order->product_id ];
}
}
if( $create_pmpro_order ) {
$morder = new MemberOrder();
$morder->status = 'success';
$morder->payment_type = 'Easy Digital Downloads'; //Change this to Stripe maybe?
$morder->code = $morder->getRandomCode();
$morder->CardType = '';
$morder->cardtype = '';
$morder->user_id = $pmpro_level['user_id'];
$morder->membership_id = $pmpro_level['membership_id'];
$morder->discount_code = '';
$morder->InitialPayment = pmpro_round_price( $pmpro_level['initial_payment'] );
$morder->PaymentAmount = pmpro_round_price( $pmpro_level['billing_amount'] );
$morder->ProfileStartDate = date_i18n("Y-m-d\TH:i:s");
$morder->BillingPeriod = $pmpro_level['cycle_period'];
$morder->BillingFrequency = $pmpro_level['cycle_number'];
$morder->Email = $_REQUEST['edd_email'];
$morder->saveOrder();
}
}
add_action( 'edd_complete_purchase', 'my_pmpro_change_level_edd_update_payment_status', 999, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment