Skip to content

Instantly share code, notes, and snippets.

@mrgarry043
Created February 11, 2019 05:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrgarry043/ad89891d8090956592a70aafd23200c4 to your computer and use it in GitHub Desktop.
Save mrgarry043/ad89891d8090956592a70aafd23200c4 to your computer and use it in GitHub Desktop.
user role
add_action( 'woocommerce_order_status_completed', 'wpglorify_change_role_on_purchase' );
function wpglorify_change_role_on_purchase( $order_id ) {
// get order object and items
$order = new WC_Order( $order_id );
$items = $order->get_items();
$product_id = 56; // 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( 'subscriber' );
// Add role
$user->add_role( 'editor' );
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment