Support » Plugin: Restrict User Access - Membership Plugin with Force » Auto Add Member After Buy Product.

  • Resolved nico89s

    (@nico89s)


    Hi, first of all I would like to thank you for building this plugin. Everything is works well on my side.

    I need help from you guys if there’s any possibilities of auto add member after user purchase product.

    I do add action on my function.php to change user role after buy specific product.
    I do exactly from this link:
    https://stackoverflow.com/questions/46823309/change-role-after-purchasing-a-specific-product-on-woocommerce

    add_action( 'woocommerce_order_status_completed', 'misha_change_role_on_purchase' );
    
    function misha_change_role_on_purchase( $order_id ) {
    
    // get order object and items
    $order = new WC_Order( $order_id );
    $items = $order->get_items();
    
    $product_id = 55; // that's a specific product ID
    
    foreach ( $items as $item ) {
    
            if( $product_id == $item['product_id'] && $order->user_id ) {
                $user = new WP_User( $order->user_id );
    
                // Remove role
                $user->remove_role( 'customer' ); 
    
                // Add role
                $user->add_role( 'subscriber' );
            }
    
    }
    
    }

    And now I’m wondering, is there any way instead of changing user role, will auto add user to specific member level that already created.

    What I want to create is something like buying premium user access with limited duration, monthly for example.

    Thank you, hope someone can help me with this.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Joachim Jensen

    (@intoxstudio)

    With the RUA API, you can add something like this to the snippet:

    
    rua_get_user($order->user_id)->add_level($level_id);
    

    where $level_id is the ID of the Access Level you want to add to this user.

    I hope this helps!

    Thanks, will try.

    guidonietmann

    (@guidonietmann)

    How works this RUA API? Do i have to install something else, or just add this code with the correct level to the functions.php?
    Thanks in advance!

Viewing 3 replies - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.