Support » Plugin: Private groups » Automatically Assign Woocommerce Customer to Group after Purchase

  • Resolved pyoil

    (@pyoil)


    Trying to automatically assign a user to a group when they purchase a woocommerce product.

    I have this code that I got from the internet that changes the user role after the customer purchases something on woocommerce. How do I modify it so that a customer is automatically assigned to a group. I will insert it into functions.php

    function change_role_on_purchase( $order_id ) {
    
        $order = new WC_Order( $order_id );
        $items = $order->get_items();
    
        foreach ( $items as $item ) {
            $product_name = $item['name'];
            $product_id = $item['product_id'];
            $product_variation_id = $item['variation_id'];
    
            if ( $order->user_id > 0 && $product_id == '416' ) {
                update_user_meta( $order->user_id, 'paying_customer', 1 );
                $user = new WP_User( $order->user_id );
    
                // Remove role
                $user->remove_role( 'subscriber' ); 
    
                // Add role
                $user->add_role( 'premium' );
            }
        }
    }
    
    add_action( 'woocommerce_order_status_processing', 'change_role_on_purchase' );
Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter pyoil

    (@pyoil)

    I figured it out. Here is the below code snippet that I had to insert into my functions.php for the groups to automatically be assigned to a user if he makes a Woocommerce product purchase. This makes access to the Private forum self serve.

    function change_privategroup_on_purchase( $order_id ) {
    
        $order = new WC_Order( $order_id );
        $items = $order->get_items();
    
        foreach ( $items as $item ) {
            $product_name = $item['name'];
            $product_id = $item['product_id'];
            $product_variation_id = $item['variation_id'];
    
            if ( $order->user_id > 0 ) {
    
                      // Insert the Product IDs Here. If Multiple Products seperate by ||
    		  if ( $product_id == '7026' || $product_id == '7027' || $product_id == '7028' || $product_id == '7029') {
    
                          // Mention the group number below. Here it is group1.
                          update_user_meta( $order->user_id, 'private_group', 'group1');
    
    		  }
            }
        }
    }
    
    add_action( 'woocommerce_order_status_processing', 'change_privategroup_on_purchase' );

    Thank you for building this elegant plugin for bbPress. Paying for a premium membership plugin like Woocommerce Memberships to just have this one feature made no sense.

    • This reply was modified 2 years, 10 months ago by pyoil.
    Plugin Author Robin W

    (@robin-w)

    Hey great – glad you worked it out, and thanks for posting the solution which will help others !!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Automatically Assign Woocommerce Customer to Group after Purchase’ is closed to new replies.