0

I followed this quick/well laid out wordpress tutorial for adding custom fields to WooCommerce variations.

http://www.remicorson.com/woocommerce-custom-fields-for-variations/

Everything worked great...

However the method for inserting the field into the dashboard (product edit) area doesn't mention how to change the order of the field as seen in the dashboard. . enter image description here

// Add Variation Settings
add_action( 'woocommerce_product_after_variable_attributes', 'variation_settings_fields', 10, 3 );
// Save Variation Settings
add_action( 'woocommerce_save_product_variation', 'save_variation_settings_fields', 10, 2 );

function variation_settings_fields( $loop, $variation_data, $variation ) {
    // Text Field
    woocommerce_wp_text_input( 
        array( 
            'id'          => '_text_field[' . $variation->ID . ']', 
            'label'       => __( 'My Text Field', 'woocommerce' ), 
            'placeholder' => 'http://',
            'desc_tip'    => 'true',
            'description' => __( 'Enter the custom value here.', 'woocommerce' ),
            'value'       => get_post_meta( $variation->ID, '_text_field', true )
        )
    );

}

function save_variation_settings_fields( $post_id ) {
    // Text Field
    $text_field = $_POST['_text_field'][ $post_id ];
    if( ! empty( $text_field ) ) {
        update_post_meta( $post_id, '_text_field', esc_attr( $text_field ) );
    }
}

Any help would be much appreciated!

closed as off-topic by Mark Kaplun, Jacob Peattie, fuxia Aug 12 '18 at 23:04

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

1

The meta box template for the variations is defined in the file html-variation-admin.php. There you will find the hook "woocommerce_variation_options_pricing". With this hook you can add your input field to the desired place.

function variation_settings_fields($loop, $variation_data, $variation)
{
    // Your Text Field
    woocommerce_wp_text_input(
        array(
            'id' => '_text_field[' . $variation->ID . ']',
            'label' => __('My Text Field', 'woocommerce'),
            'placeholder' => 'http://',
            'wrapper_class' => 'form-row',
            'desc_tip' => 'true',
            'description' => __('Enter the custom value here.', 'woocommerce'),
            'value' => get_post_meta($variation->ID, '_text_field', true)
        )
    );
}

add_action('woocommerce_variation_options_pricing','variation_settings_fields');

WooCommerce variations meta box with the custome field between price and the stock field

Not the answer you're looking for? Browse other questions tagged or ask your own question.