• Resolved kmcwepc

    (@kmcwepc)


    Hi there, could you give me an example of how to add a PHP code snippet that takes an action when someone purchases a subscription (and how to check which subscription/item exactly they’ve purchased)?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter kmcwepc

    (@kmcwepc)

    After some digging around the code I found the solution:

    function _dbg_log( $data ) {
    	// error_log( print_r( $data, true ) );
    }
    
    function get_membership_roles() {
    	return array(
    		// <name of item> => <slug of wordpress user role>
    		"Class Membership" => "class_member_gp",
    		"Centre Membership" => "centre_member_gp",
    		"Study Membership" => "study_member_gp",
    		"Great Benefactor" => "great_benefactor_gp",
    	);
    }
    
    function edit_member_role( $method, $subscription ) {
    	$roles = array(
    		// <name of item> => <slug of wordpress user role>
    		"Class Membership" => "class_member_gp",
    		"Centre Membership" => "centre_member_gp",
    		"Study Membership" => "study_member_gp",
    		"Great Benefactor" => "great_benefactor_gp",
    	);
    	
    	$user_id = $subscription->get_customer_id();
    	$mem_product = get_the_title( $subscription->get_product_id() );
    
    	_dbg_log( "Called edit_member_role with {$method}, {$user_id}, {$mem_product}" );
    	$user = get_userdata( $user_id );
    	if ( $user == false ) {
    		return;
    	}
    	_dbg_log( "edit_member_role found a user with ID {$user_id}" );
    	
    	if ( !array_key_exists( $mem_product, $roles ) ) {
    		_dbg_log( "edit_member_role did not find a user role for product {$mem_product}"  );
    		return;
    	}
    	
    	_dbg_log( "edit_member_role found a user role for product {$mem_product} => {$roles[$mem_product]}. Using {$method}"  );
    	$user->$method( $roles[$mem_product] );
    }
    
    /*
    add_action( 'getpaid_new_subscription', 'set_user_role_on_new_subscription', 10, 1 );
    function set_user_role_on_new_subscription( $subscription ) {
    	_dbg_log( 'Called set_user_role_on_new_subscription' );
    	edit_member_role( 'add_role', $subscription );
    }
    */
    
    add_action( 'getpaid_subscription_status_changed', 'set_user_role_on_change_subscription_status', 10, 3 );
    function set_user_role_on_change_subscription_status( $subscription, $from, $to ) {
    	_dbg_log( 'Called set_user_role_on_change_subscription_status' );
    	
    	if ( in_array( $to, array('active', 'trialling')) ) {
    		$method = 'add_role';
    	} else {
    		$method = 'remove_role';
    	}
    	edit_member_role( $method, $subscription );
    }
    
    // do we need to hook into both of these, or just getpaid_delete_subscription?
    add_action( 'getpaid_delete_subscription', 'set_user_role_on_delete_subscription', 10, 1 );
    add_action( 'getpaid_authenticated_admin_action_subscription_manual_delete', 'set_user_role_on_delete_subscription', 10, 1 );
    function set_user_role_on_delete_subscription( $subscription ) {
    	_dbg_log( 'Called set_user_role_on_delete_subscription' );
    	
    	edit_member_role( 'remove_role', $subscription );
    }
    
    /*
    add_action( 'getpaid_update_subscription', 'set_user_role_on_update_subscription', 10, 1 );
    function set_user_role_on_update_subscription( $subscription ) {
    }
    */
    • This reply was modified 1 year, 5 months ago by kmcwepc.
    Thread Starter kmcwepc

    (@kmcwepc)

    Please consider implementing such a feature: assigning a given user role when a specific subscription is purchased, and removing it when the subscription is no longer active.

    Plugin Contributor Brian Mutende

    (@picocodes)

    Yeah we’re working on such functionality. It will be available soon. Your code should work though.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How to add a hook that takes action when a user purchases a subscription?’ is closed to new replies.