WordPress WooCommerce text attribute

I am using WooCommerce plugin in WordPress. In that plugin I need to add text input for each product where in clients can fill in the additional product details.

The problem I am facing is that whenever I Add Text Attribute, it changes to a select box. Please suggest some solution. Thanks!

Related posts

Leave a Reply

1 comment

  1. Do you mean that your clients should have this text input field available on frontend?

    If it’s just backend, you can add this to your functions.php:

    if (is_admin()){     
    // Add the Meta Box  
    function add_textfield() {  
        add_meta_box(  
            'textfield', // $id  
            'Custom textfield', // $title   
            'show_textfield_backend', // $callback  
            'product', // $page  
            'normal', // $context  
            'high'  // $priority
        ); 
    }  
    add_action('add_meta_boxes', 'add_textfield');
    
    // The Callback  
    function show_textfield_backend() {  
        global $post;  
        global $wpdb;
    
        $meta = get_post_meta($post->ID, 'textfield', true);
    
        // Use nonce for verification  
        echo '<input type="hidden" name="textfield_nonce" value="'.wp_create_nonce(basename(__FILE__)).'" />';  
    
        _e('Custom textfield: ');
        echo '<input type="text" placeholder="text" id="textfield" class="textfield" name="textfield" ', $meta ? " value='$meta'" : "",' />';       
    }
    
    // Save the Data  
    function save_textfield($post_id) {  
        global $wpdb;
    
        // verify nonce  
        if (!wp_verify_nonce($_POST['textfield_nonce'], basename(__FILE__)))   
            return $post_id;
        // check autosave  
        if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)  
            return $post_id;
        // check permissions
        if ('page' == $_POST['post_type']) {  
            if (!current_user_can('edit_page', $post_id))  
                return $post_id;  
        } elseif (!current_user_can('edit_post', $post_id)) {  
            return $post_id;  
        }
    
        if ( !wp_is_post_revision( $post_id ) ) {
    
            $textfield = $_POST['textfield'];
            update_post_meta($post_id, 'textfield', $textfield);
    
        }  
    }
    add_action('save_post', 'save_textfield');
    }