Advertisement
vapvarun

Change Role with WooCommerce at Order Mark Complete

Feb 28th, 2023
828
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.60 KB | None | 0 0
  1. add_action( 'woocommerce_order_status_completed', 'vap_change_role_on_purchase_gold' );
  2.  
  3. function vap_change_role_on_purchase_gold( $order_id ) {
  4.  
  5. // get order object and items
  6.     $order = new WC_Order( $order_id );
  7.     $items = $order->get_items();
  8.  
  9.     $product_id = 2355; // that's a specific product ID
  10.  
  11.     foreach ( $items as $item ) {
  12.  
  13.         if( $product_id == $item['product_id'] && $order->user_id ) {
  14.             $user = new WP_User( $order->user_id );
  15.  
  16.             // Remove old role
  17.             $user->remove_role( 'customer' );
  18.  
  19.             // Add new role
  20.             $user->add_role( 'pmpro_role_3' );
  21.         }
  22.     }
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement