Viewing 3 replies - 1 through 3 (of 3 total)
  • Hi pmagony,

    You can use the following snippet in your child theme’s functions.php.

    To be honest, i haven’t tested it but it should do the job. In product_id you should put the id of the product you are interested and in $group_id the id of the group you want to add the user to.

    add_action( 'woocommerce_payment_complete', 'gt_add_user_to_group', 10, 1);
    function gt_add_user_to_group( $order_id ) {
        $order = new WC_Order( $order_id );
        $myuser_id = $order->user_id;
        $user_info = get_userdata( $myuser_id );
        $items = $order->get_items();
        foreach ( $items as $item ) {
            if ($item['product_id']==24) {
                Groups_User_Group::create( array( 'user_id' => $user_id, 'group_id' => $group_id ) );
            }
        }
    }

    Kind regards
    George

    Hi again,

    Sorry for the follow-up comment but i’ve found something better to suggest.
    You can use the hook woocommerce_order_status_completed, so when the order is complete the user involved will be added to a group.

    Try this snippet below and don’t forget to change product and group ids to your preference.

    add_action( 'woocommerce_order_status_completed', 'gt_add_user_to_group' );
    function gt_add_user_to_group( $order_id ) {
    
    	$order = new WC_Order( $order_id );
    	$my_user_id = $order->user_id;
    	$items = $order->get_items();
    	$group_id = 4;
    	foreach ( $items as $item ) {
            	if ($item['product_id']==2399) {
            		Groups_User_Group::create( array( 'user_id' => $my_user_id, 'group_id' => $group_id ) );
            	}
        	}
    }

    Cheers

    Thread Starter F C

    (@pmagony)

    Thanks, I found a plug-in at WooThemes that manages subscriptions:
    https://www.woothemes.com/products/groups-woocommerce/

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Add to Group from Purchase’ is closed to new replies.