• 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)
Viewing 2 replies - 1 through 2 (of 2 total)

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