• Resolved Edvard

    (@ecsanyi)


    I’m using a recipe from Paid Memberships Pro (PMPRO) that allows visitors to assign a membership level to members when purchasing an EDD product.

    Will this code work on EDD’s side after upgrading to 3.0? Just to note that this code currently works like a charm.

    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 );
    	
    	// 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'		=> $payment_meta['user_info']['id'],
    			'billing_amount' 	=> '156.00', // float (string)
    			'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'		=> $payment_meta['user_info']['id'],
    			'billing_amount' 	=> '120.00', // float (string)
    			'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'		=> $payment_meta['user_info']['id'],
    			'billing_amount' 	=> '96.00', // float (string)
    			'startdate' 		=> current_time( 'mysql' ), // string (date)
    			'enddate' 			=> date( "Y-m-d 00:00:00", strtotime( "+1 year" ) )
     		),
     	);
    	
    	$user_id = $payment_meta['user_info']['id'];
    
    	if ( $new_status == 'publish' ) {
    		// Order is complete.
    		foreach ( $payment_meta['cart_details'] as $key => $item ) {
    			if ( ! empty( $product_levels[$item['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 {
    		// Remove the level for any of the other statuses: pending, processing, revoked, failed, abandoned, preapproval, cancelled, refunded
    		pmpro_changeMembershipLevel( 0, $user_id );
    	}	
    }
    add_action( 'edd_update_payment_status', 'my_pmpro_change_level_edd_update_payment_status', 10, 3 );
Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Chris Klosowski

    (@cklosows)

    Hi @ecsanyi

    From the EDD site that ‘recipe’ does need some updates.

    Ideally this whole thing should be rewritten to work with our new hook called
    edd_transition_order_item_status, which is when individual order items are transitioned, instead of the whole order, since you can now do partially refunded orders and only refund certain items from the orders.

    But that would be an entirely new recipe I think. We can suggest some changes to the PMPro team for this one while they work on that new hook.

    What you see here is in no way tested with PMPro and you should run it by their team, and test in a local or staging environment, but I believe these updates should make it compatible from the EDD side of things.

    function my_pmpro_change_level_edd_update_payment_status( $payment_id, $new_status, $old_status ) {
    
    	// Get the order from EDD.
    	$order = edd_get_order( $payment_id );
    
    	// 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,
    			'billing_amount' => '156.00', // float (string).
    			'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,
    			'billing_amount' => '120.00', // float (string).
    			'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,
    			'billing_amount' => '96.00', // float (string).
    			'startdate'      => current_time( 'mysql' ), // string (date).
    			'enddate'        => date( 'Y-m-d 00:00:00', strtotime( '+1 year' ) ),
    		),
    	);
    
    	if ( 'complete' === $new_status ) {
    		// Order is complete.
    		foreach ( $order->items 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.
    			}
    		}
    	} else {
    		// Remove the level for any of the other statuses: pending, processing, revoked, failed, abandoned, preapproval, cancelled, refunded.
    		pmpro_changeMembershipLevel( 0, $order->user_id );
    	}
    }
    add_action( 'edd_update_payment_status', 'my_pmpro_change_level_edd_update_payment_status', 10, 3 );

    Cheers!

    Thread Starter Edvard

    (@ecsanyi)

    Thank you very much, Chris, I’ll check the code with PMPro developers and let you know.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Does assign a membership level when purchasing an EDD product work in 3.0?’ is closed to new replies.