Home / Admin / Add User to RCP Subscription on Purchase
Duplicate Snippet

Embed Snippet on Your Site

Add User to RCP Subscription on Purchase

Add customer to an RCP subscription level when they purchase a specific product.

Code Preview
php
<?php
function pw_edd_add_customer_to_level( $payment_id = 0 ) {
	$user_id = edd_get_payment_user_id( $payment_id );
	if( $user_id <= 0 ) {
		return;
	}
	$downloads = edd_get_payment_meta_downloads( $payment_id );
	if( $downloads ) {
		$level = false;
		foreach( $downloads as $download ) {
			// Set the subscription level based on the product ID(s) purchased
			switch( $download['id'] ) {
				case 45:
					// Set the subscription level to add the user to
					$level = 4;
					break;
				case 742:
					break;
			}
		}
		if( ! empty( $level ) && function_exists( 'rcp_set_status' ) ) {
			// Give user one month of access
			$expiration = date( 'Y-m-d H:i:s', strtotime( '+1 month' ) );
			update_user_meta( $user_id, 'rcp_subscription_level', $level );
			update_user_meta( $user_id, 'rcp_expiration', $expiration );
			rcp_set_status( $user_id, 'active' );
		}
	}
}
add_action( 'edd_complete_purchase', 'pw_edd_add_customer_to_level' );

Comments

Add a Comment