skip to Main Content

I have this php script to functions.php file for adding a custom fields (barcode/ean number) to Woocommerce products edit admin panel.

//add barcode field
function add_barcode(){
    woocommerce_wp_text_input(
        array(
            'id' => '_barcode',
            'label' => __( 'Barcode', 'woocommerce' ),
            'placeholder' => '',
            'desc_tip' => 'true',
            'description' => __( "Enter barcode number.", "woocommerce" )
        )
    );
}
add_action('woocommerce_product_options_inventory_product_data','add_barcode');

function add_barcode_save( $product ){
    if( isset( $_POST['_barcode'] ) ) {
        $product->update_meta_data( '_barcode', sanitize_text_field( $_POST['_barcode'] ) );
    } else {
        $product->delete_meta_data( '_barcode' );
    }
}
add_action( 'woocommerce_admin_process_product_object', 'add_barcode_save' );

The custom field it appears correctly and i can save with this php script. But i want to add this custom field to the quick edit panel also for faster editing.

Can anybody write me the additional php script for this?

Thanks in advance.

2

Answers


  1. Chosen as BEST ANSWER

    finally i found a solution after searching on google.

    //add barcode input to quick edit
    function add_barcode_qe()
    {?>
    <label><span class="title"><?php echo esc_html_e('Barcode', 'woocommerce');?></span>
    <span class="input-text-wrap"><?php
    woocommerce_wp_text_input(
            array(
                'id' => '_barcode',
                'desc_tip' => false,
                'class' => 'text custom_field',
                'style' => 'margin-bottom:3px;'
            )
        );
    echo '</span></label>';
    }
    add_action('woocommerce_product_quick_edit_start', 'add_barcode_qe');
    
    //populate barcode quick edit field
    function populate_brcd_field()
    {
    ?>
        <script>
            (function($) {
                $('#the-list').on('click', '.editinline', function() {
    
                    var post_id = $(this).closest('tr').attr('id');
                    post_id = post_id.replace('post-', '');
    
                    var custom_field = $('#jcwc1_product_data_' + post_id).text();
                    $('input[name="_barcode"]', '.inline-edit-row').val(custom_field);
                });
            })(jQuery);
        </script>
    <?php
    }
    add_action('admin_footer', 'populate_brcd_field');
    
    //save barcode input value on submit
    function add_barcode_qe_save($product)
    {
        //$product = wc_get_product();
        $pid = $product->get_id();
        if (isset($_REQUEST['_barcode'])) {
            $brcd = $_REQUEST['_barcode'];
            update_post_meta($pid, '_barcode', wc_clean($brcd));
        } else {
            delete_post_meta($pid, '_barcode');
        }
    }
    
    add_action('woocommerce_product_quick_edit_save', 'add_barcode_qe_save');
    

    the barcode input field appears before SKU. how i can move it bellow the SKU field?


  2. I don’t suppose you know how to populate the text field in the quick edit view to show the barcode? We’ve used this approach but are struggling with the text field population.

    IMG of Barcode Field

    // Add boarcode to products
    
    // Display Fields
    add_action( 'woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields' );
    
    // Save Fields
    add_action( 'woocommerce_process_product_meta', 'woo_add_custom_general_fields_save' );
    
    function woo_add_custom_general_fields() {
    
    global $woocommerce, $post;
    // Text Field
    woocommerce_wp_text_input(
    array(
    'id' => 'barcode',
    'label' => __( 'Barcode', 'woocommerce' ),
    'placeholder' => 'barcode here',
    'desc_tip' => 'true',
    'description' => __( 'Product barcode.', 'woocommerce' )
    )
    );
    }
    
    function woo_add_custom_general_fields_save( $post_id ){
    
    // Textarea
    $woocommerce_barcode = $_POST['barcode'];
    if( !empty( $woocommerce_barcode ) )
    update_post_meta( $post_id, 'barcode', esc_html( $woocommerce_barcode ) );
    
    }
    
    //add barcode input to quick edit
    function add_barcode_qe()
    {?>
    <label><span class="title"><?php echo esc_html_e('Barcode', 'woocommerce');?></span>
    <span class="input-text-wrap"><?php
    woocommerce_wp_text_input(
            array(
                'id' => 'barcode',
                'desc_tip' => false,
                'class' => 'text custom_field',
                'style' => 'margin-bottom:3px;'
            )
        );
    echo '</span></label>';
    }
    add_action('woocommerce_product_quick_edit_start', 'add_barcode_qe');
    
    //populate barcode quick edit field
    function populate_brcd_field()
    {
    ?>
        <script>
            (function($) {
                $('#the-list').on('click', '.editinline', function() {
                    var post_id = $(this).closest('tr').attr('id');
                    post_id = post_id.replace('post-', '');
    
                    var custom_field = $('#jcwc1_product_data_' + post_id).text();
                    $('input[name="barcode"]', '.inline-edit-row').val(custom_field);
                });
            })(jQuery);
        </script>
    <?php
    }
    add_action('admin_footer', 'populate_brcd_field');
    
    //save barcode input value on submit
    function add_barcode_qe_save($product)
    {
        //$product = wc_get_product();
        $pid = $product->get_id();
        if (isset($_REQUEST['barcode'])) {
            $brcd = $_REQUEST['barcode'];
            update_post_meta($pid, 'barcode', wc_clean($brcd));
        } else {
            delete_post_meta($pid, 'barcode');
        }
    }
    
    add_action('woocommerce_product_quick_edit_save', 'add_barcode_qe_save');
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search