title: Change role and user meta value after purchase of specific product on WooCommerce?
body: <p>On our WooCommerce store we have a specific product that when purchased and the order confirmed changes the user role to a 'club' member (premium membership).</p>
  
  <p>We also need to now assign a unique member ID to the user which I am attempting to assign through copying the number from the order ID. The meta key 'club_member_id' has been added as a custom field for users with a current default value of 1, through the Advanced Custom Fields plugin.</p>
  
  <p>The source code below is changing the user role to 'club' but does not change the meta value for 'club_member_id'. Can anybody shed some light on this?</p>
  
  <pre><code>//Change role and add Club ID if club membership is bought
  
  function lgbk_add_member( $order_id ) {
  
  $order = new WC_Order( $order_id );
  $items = $order-&gt;get_items();
  
  foreach ( $items as $item ) {
  
      $product_name = $item['name'];
      $product_id = $item['product_id'];
      $product_variation_id = $item['variation_id'];
      $original_cid = get_user_meta('club_member_id');
      $meta_key = 'club_member_id';
      $meta_value = $order_id;
  
  if ( $order-&gt;user_id &gt; 0 &amp;&amp; $product_id == '7968' ) {
  
      update_user_meta( $order-&gt;user_id, 'paying_customer', 1 );
      $user = new WP_User( $order-&gt;user_id );
      // Remove role
      $user-&gt;remove_role( 'customer' );
      // Add role
      $user-&gt;add_role( 'club' );
      // Remove club member ID
      $user-&gt;delete_user_meta($user, $meta_key);
      // Add club member ID
      $user-&gt;add_user_meta($user, $meta_key, $meta_value);
  
      } 
  
    }
  
  } add_action( 'woocommerce_order_status_completed', 'lgbk_add_member' );
  </code></pre>
  
  <p>This code has been placed in our theme's functions.php file. Thanks in advance.</p>