chrmrtns

Update_WP_User_After_Purchase_Adjust_BuddyBoss_Member_Type

Dec 17th, 2022 (edited)
885
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.91 KB | Source Code | 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 purchased a product.
  7. */
  8.  
  9.  
  10. // Update Wordpress Role and Profile Type after purchase, WooCommerce
  11.  
  12.  
  13. function chrmrtns_change_role( $order_id ) {
  14.     // get all the order data
  15.     $order = new WC_Order($order_id);
  16.    
  17.     //get the user email from the order
  18.     $user = $order->get_user();
  19.      
  20.     // if the this is a registered user and this user is not an admin
  21.     if( false != $user && !user_can($user, 'administrator') ){
  22.    
  23.       // our new role name
  24.       $role = 'customer';
  25.       // our new profile type, you need to set a profile type first
  26.       $profile = 'vip-mitglied';
  27.       //set the new role to our customer
  28.       $user->set_role($role);
  29.  
  30.      // Get the user ID
  31.      $user_id = get_post_meta($order_id, '_customer_user', true);
  32.  
  33.        
  34.  
  35.       bp_set_member_type($user_id, $profile);
  36.      
  37.     }  
  38.   }
  39.    
  40.   //add this newly created function to the thank you page
  41.   add_action( 'woocommerce_thankyou', 'chrmrtns_change_role', 100, 1 );
  42.  
  43.  
  44.  
  45.   // You can use other hooks and adjust your function accordingly.
  46.  
  47.   // See: https://woocommerce.com/document/subscriptions/develop/action-reference/
  48.  
  49.   //   Triggered when a payment fails for a subscription. This can be for payment of the initial
  50.   //   order, a switch order or renewal order.
  51.   //   add_action( 'woocommerce_subscription_payment_failed', 'chrmrtns_change_role', 100, 1 );
  52.   //
  53.   //   Unlike the 'woocommerce_subscription_payment_failed' hook, this hook is not triggered for failed payments on the
  54.   //   initial order or a switch order. It is only triggered for payments on renewal orders.
  55.   //   add_action( 'woocommerce_subscription_renewal_payment_failed', 'chrmrtns_change_role', 100, 1 );
  56.   //
  57.   //   Description: Similar to WooCommerce’s 'woocommerce_order_status_changed' hook, this action is triggered
  58.   //   immediately after a subscription’s status has been changed.
  59.   //   It will be triggered for all status changes defined by WooCommerce Subscriptions, including: pending, active,
  60.   //   on-hold, pending-cancel, cancelled, or expired; as well as any custom subscription statuses.
  61.   //   add_action( 'woocommerce_subscription_status_updated', 'chrmrtns_change_role', 100, 1 );
  62.   //  
  63.   //  
  64.   //   add_action( 'woocommerce_order_status_changed', 'chrmrtns_change_role', 100, 1 );
  65.   //
  66.   //  
  67.   //   add_action( 'woocommerce_subscription_status_active', 'chrmrtns_change_role', 100, 1 );
  68.   //
  69.   //  
  70.   //   add_action( 'woocommerce_subscription_status_cancelled', 'chrmrtns_change_role', 100, 1 );
  71.   //
  72.   //  
  73.   //   add_action( 'woocommerce_subscription_status_expired', 'chrmrtns_change_role', 100, 1 );
  74.   //
  75.   //  
  76.   //   add_action( 'woocommerce_subscription_status_on-hold', 'chrmrtns_change_role', 100, 1 );
  77.  
  78.  
  79.  
  80.  
Advertisement
Add Comment
Please, Sign In to add comment