简体   繁体   中英

Change the order of custom field for Woocommerce variable products in admin area

I followed this tutorial for adding custom fields into 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 . . 在此处输入图片说明

// 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!

  • Using woocommerce_variation_options_pricing will put it after the price (it may need some CSS). It's the closest hook I found after price input.
  • Using woocommerce_variation_options will put it before price input
  • Following called hook in template is woocommerce_variation_options_inventory

In those cases, you can check yourself what is possible by:

  • finding the related Woocommerce template (in plugin directory)
  • studying the code to find do_action (or apply_filter for variables) to see "where and when you can do/update stuff". In your case wp-content/plugins/woocommerce/includes/admin/meta-boxes/views/html-variation-admin.php:159
  • sometimes your are lucky (merciful devs), sometimes less :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM