In WooCommerce product options edit page display a custom field before the SKU

274
April 30, 2017, at 04:40 AM

So I've got the following function to add a barcode field in the Product Inventory tab. However this field is being added after all the other content, and I would like to have this before the SKU code.

function add_barcode(){
    global $woocommerce,$post;
    woocommerce_wp_text_input(
        array(
            'id'          => '_barcode',
            'label'       => __('Barcode','woocommerce'),
            'placeholder' => 'Scan Barcode',
            'desc_tip'    => 'true',
            'description' => __('Scan barcode.','woocommerce')
        ));
}
add_action('woocommerce_product_options_inventory_product_data','add_barcode');

Is there anyway to place the function/field before the SKU aka before the actual hook, like something in terms of woocommerce_before_product_options_inventory_product_data?

Thanks for any suggestions in advance.

Answer 1

IS NOT POSSIBLE TO PLACE ANY CUSTOM FIELD BEFORE THE SKU

You can take a look to html-product-data-inventory.php source code file which displays product inventory fields.

But you can display your 'Barcode' custom field just after the SKU field (for example)

For that you have to hook your custom function in woocommerce_product_options_sku action hook instead. There is also some missing things in your code to display a saved value.

And finally you need another function to save that value when product is saved or updated.

Here is that complete code:

add_action('woocommerce_product_options_sku','add_barcode', 10, 0 );
function add_barcode(){
    global $woocommerce, $post;
    // getting the barcode value if exits
    $product_barcode = get_post_meta( $post->ID, '_barcode', true );
    if(! $product_barcode) $product_barcode = '';
    // Displaying the barcode custom field
    woocommerce_wp_text_input( array(
        'id'          => '_barcode',
        'label'       => __('Barcode','woocommerce'),
        'placeholder' => 'Scan Barcode',
        'desc_tip'    => 'true',
        'description' => __('Scan barcode.','woocommerce')
    ), $product_barcode); // <== added "$product_barcode" here to get the value if exist
}
add_action( 'woocommerce_process_product_meta', 'save_barcode', 10, 1 );
function save_barcode( $post_id ){
    $product_barcode_field = $_POST['_barcode'];
    if( !empty( $product_barcode_field ) )
        update_post_meta( $post_id, '_barcode', esc_attr( $product_barcode_field ) );
}

This code goes in function.php file of your active child theme (or theme) or also in any plugin file.

This code is tested and works for WooCommerce version 2.6+ and 3.0+

READ ALSO
mysqli error: Database not selected

mysqli error: Database not selected

I'm trying to create an specific class to manage common database functionsThis class looks like this:

297
how to insert value into column of existing table

how to insert value into column of existing table

how to insert value into column of existing table like

244
Warning error on JSON decode

Warning error on JSON decode

I'm getting a warning on a json decode foreach within a foreach (although the code works which is strange) the warning is: Warning: Invalid argument supplied for foreach() it is referring to this line: foreach ($value as $val) {

352
How do I link a businesses menu to my website so I can add items for the business to make?

How do I link a businesses menu to my website so I can add items for the business to make?

Ok, so the question might be a bit confusing but basically this is my problem

183