Title: code do not save input in customer profile
Last modified: July 3, 2020

---

# code do not save input in customer profile

 *  Resolved [Patrick Horemans](https://wordpress.org/support/users/winock/)
 * (@winock)
 * [5 years, 10 months ago](https://wordpress.org/support/topic/code-do-not-save-input-in-customer-profile/)
 * Hi,
    i hope this is the right forum to ask my issue. i have some code for registration
   in Woocommerce to change a user_role and fii a conditional text field, but the
   code do not saving. can somebody help me out?
 * First i make the new role
 *     ```
       function uiwc_new_role() {  
   
         //add the special customer role
         add_role(
           'kundenkarte',
           "Kundenkarte",
           array(
             'read'         => true,
             'delete_posts' => false
           )
         );
       }
       add_action('admin_init', 'uiwc_new_role');
       ```
   
 * then the code for make selecting the checkbox and textfield
 *     ```
       add_action( 'woocommerce_after_order_notes', 'custom_checkout_field_with_wholesale_option' );
       function custom_checkout_field_with_wholesale_option( $checkout ) {
   
           if( current_user_can( 'wholesale_customer' ) ) return; // exit if it is "wholesale customer"
   
           echo '<style> #wholesale_card_field.hidden { display:none; }</style>
           <div id="wholesale_checkbox_wrap">';
   
           woocommerce_form_field('wholesale_checkbox', array(
               'type' => 'checkbox',
               'class' => array('input-checkbox'),
               'label' => __('Do you have a Customer Card?'),
               'placeholder' => __('card'),
               'required' => false,
               'value'  => true
           ), '');
   
           woocommerce_form_field('wholesale_card', array(
               'type' => 'text',
               'class' => array('hidden'),
               'placeholder' => __('Customer card Id'),
               'required' => true,
           ), '');
   
           echo '</div>';
   
           ?>
           <script>
           jQuery(function($){
               $('#wholesale_checkbox_field input').click(function(){
                   if( $(this).is(':checked')) {
                       $('#wholesale_card_field').css('display', 'none').removeClass('hidden').show();
                   } else if ( ! $(this).is(':checked') && $('#wholesale_card_field').css('display') !== 'none' ) {
                       $('#wholesale_card_field').hide();
                   }
               });
           });
           </script>
           <?php
   
       }
   
       // Validation
       add_action( 'woocommerce_checkout_process', 'wholesale_option_validation' );
       function wholesale_option_validation() {
           if ( isset($_POST['wholesale_checkbox']) && isset($_POST['wholesale_card']) && empty($_POST['wholesale_card']) ) {
               wc_add_notice( __("Please fill in your customer card Id"), "error" );
           }
       }
   
       // Conditionally change customer user role and add customer card as order and user meta
       add_action( 'woocommerce_checkout_update_order_meta', 'wholesale_option_update_user_meta' );
       function wholesale_option_update_user_meta( $order_id ) {
           if ( isset($_POST['wholesale_checkbox']) ) {
               $user_id = get_post_meta( $order_id, '_customer_user', true ); // Get user ID
               if( $user_id > 0 ){
                   $user = new WP_User($user_id);
                   $user->remove_role('customer');
                   $user->add_role('kundenkarte');
               }
               if ( isset($_POST['wholesale_card']) ) {
                   update_post_meta( $order_id, 'wholesale_card', sanitize_text_field( $_POST['wholesale_card'] ) );
   
                   if( $user_id > 0 )
                       update_user_meta( $user_id, 'wholesale_card', sanitize_text_field( $_POST['wholesale_card'] ) );
               }
           }
       }
       ```
   
 * Why is it not saving?
 * thanks

Viewing 2 replies - 1 through 2 (of 2 total)

 *  [Peter Lawrenson](https://wordpress.org/support/users/lorro/)
 * (@lorro)
 * [5 years, 10 months ago](https://wordpress.org/support/topic/code-do-not-save-input-in-customer-profile/#post-13075103)
 * $user->add_role() changes only the user object in memory. To subsequently update
   the database you would use: wp_update_user();
    [https://developer.wordpress.org/reference/functions/wp_update_user/](https://developer.wordpress.org/reference/functions/wp_update_user/)
 *  Plugin Support [Tseten a11n](https://wordpress.org/support/users/tibetanitech/)
 * (@tibetanitech)
 * [5 years, 9 months ago](https://wordpress.org/support/topic/code-do-not-save-input-in-customer-profile/#post-13160401)
 * We haven’t heard back from you in a while, so I’m going to mark this as resolved–
   if you have any further questions, you can start a new thread.

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘code do not save input in customer profile’ is closed to new replies.

 * ![](https://ps.w.org/woocommerce/assets/icon.svg?rev=3234504)
 * [WooCommerce](https://wordpress.org/plugins/woocommerce/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/woocommerce/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/woocommerce/)
 * [Active Topics](https://wordpress.org/support/plugin/woocommerce/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/woocommerce/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/woocommerce/reviews/)

 * 2 replies
 * 3 participants
 * Last reply from: [Tseten a11n](https://wordpress.org/support/users/tibetanitech/)
 * Last activity: [5 years, 9 months ago](https://wordpress.org/support/topic/code-do-not-save-input-in-customer-profile/#post-13160401)
 * Status: resolved