I got this code and its working great.
add_action( 'woocommerce_order_status_processing','change_role_on_purchase' ); function change_role_on_purchase( $ order_id ) { $ order = wc_get_order( $ order_id ); $ items = $ order->get_items(); $ products_to_check = array( '2205','2207','2209','2211','2213','2226','2227','2230','2450','2444','2235','2237','2238','2239','2240' ); foreach ( $ items as $ item ) { if ( $ order->user_id > 0 && in_array( $ item['product_id'], $ products_to_check ) ) { $ user = new WP_User( $ order->user_id ); // Change role $ user->remove_role( 'Customer' ); $ user->add_role( 'Distributor' ); // Exit the loop break; } } }
But, I want to add:- If user purchase more than 9, change role to Distributor.