chrmrtns

RemoveRoleWhenSubscriptionCancelled

Dec 19th, 2022
1,118
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.36 KB | None | 0 0
  1. <?php
  2.    
  3. /* Add this to your custom functions.php or to
  4. *  your custom plugin to update the Wordpress role
  5. *  or the member type of the user in BuddyBoss plugin
  6. *  after the user has a cancelled subscription.
  7. *
  8. *  The subscription must be in cancelled state in this
  9. *  example, but you can adjust the hook accordingly as
  10. *  seen in https://woocommerce.com/document/subscriptions/develop/action-reference/
  11. */
  12.  
  13.  
  14.   // Update Wordpress Role and Profile Type after end of subscription, WooCommerce
  15.  
  16.   function chrmrtns_remove_role_when_subscription_ends ( $wc_subscription ) {
  17.  
  18.  
  19.         $user_id = $wc_subscription->get_user_id();
  20.         $user = $wc_subscription->get_user();
  21.  
  22.      
  23.     // if the this is a registered user and this user is not an admin
  24.     if( false != $user && !user_can($user, 'administrator') ){
  25.    
  26.        
  27.  
  28.  
  29.       // our new role name
  30.       $role = 'subscriber'; // Can be anything like customer, subscriber, whatever
  31.       $profile = 'member'; // Roles need to be set in BuddyBoss Profile Types first, for example member or vip-meber
  32.       //set the new role to our customer
  33.       $user->set_role($role);
  34.  
  35.       // Set the role in BuddyBoss
  36.       bp_set_member_type($user_id, $profile);
  37.      
  38.     }  
  39.  
  40. }
  41.  
  42.   add_action( 'woocommerce_subscription_status_cancelled', 'chrmrtns_remove_role_when_subscription_ends', 100, 1 );
  43.  
Advertisement
Comments
  • chrmrtns
    3 years
    # text 0.15 KB | 0 0
    1. https://woocommerce.com/document/subscriptions/develop/action-reference/ will give you other hooks instead, if you want to change role on different events
Add Comment
Please, Sign In to add comment