Skip to content

Instantly share code, notes, and snippets.

@SiR-DanieL
Created March 29, 2016 06:03
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SiR-DanieL/20372ea13fe4bfa784c2 to your computer and use it in GitHub Desktop.
Save SiR-DanieL/20372ea13fe4bfa784c2 to your computer and use it in GitHub Desktop.
add_action( 'woocommerce_order_status_completed', '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( '1', '2', '3' );
foreach ( $items as $item ) {
if ( $order->user_id > 0 && in_array( $item['product_id'], $products_to_check ) ) {
$role = 'new-role';
switch ( $item['product_id' ] ) {
case 123: $role = 'new-role-1'; break;
case 456: $role = 'new-role-2; break;
}
$user = new WP_User( $order->user_id );
// Change role
$user->remove_role( 'customer' );
$user->add_role( $role );
// Exit the loop
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment