title: Change user role if checkout custom checkbox is checked in WooCommerce
body: <p>I'm trying to add a custom function to my child-themes functions.php file where a checkbox is added to the bottom of the billing details form on the checkout page. </p>
  
  <p>This checkbox asks the customer if they'd like to become a wholesale customer. </p>
  
  <pre><code>function customise_checkout_field_with_wholesale_option($checkout) {
  
  echo '&lt;div id="wholesale_checkbox_wrap"&gt;';
  woocommerce_form_field('wholesale_checkbox', array(
      'type' =&gt; 'checkbox',
      'class' =&gt; array('input-checkbox'),
      'label' =&gt; __('Would you like to apply for a wholesale account?'),
      'placeholder' =&gt; __('wholesale'),
      'required' =&gt; false,
      'value'  =&gt; true
  ), $checkout-&gt;get_value('wholesale_checkbox'));
  echo '&lt;/div&gt;';
  
  }
  </code></pre>
  
  <p>This works fine, however I'm having trouble with this next part.. </p>
  
  <p>I would like the customers user role to save as "wholesale_customer" instead of "customer" if they check the checkbox. </p>
  
  <pre><code>add_action('woocommerce_after_checkout_billing_form', 'customise_checkout_field_with_wholesale_option');
  
  function wholesale_customer( $order_id ) {
  
  $order = new WC_Order( $order_id );
  
  if (isset($_POST['wholesale_checkbox'])) {
  
      if ($order-&gt;user_id &gt; 0) {
          $user = new WP_User($order-&gt;user_id);
          // Remove role
          $user-&gt;remove_role('customer');
          // Add role
          $user-&gt;add_role('wholesale_customer');
      }
  }
  }
  add_action( 'woocommerce_thankyou', 'wholesale_customer' );
  </code></pre>
  
  <p>The above function works by saving the customer as a "wholesale_customer" when I remove the wholesale_checkbox if statement. But with the if statement included, it always saves the role as "customer".</p>
  
  <p>Where am I going wrong? Cheers</p>