更改管理区域中 Woocommerce 变量产品的自定义字段顺序

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

我按照本教程将自定义字段添加到 WooCommerce 变体中。

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

一切都很好...

然而,将字段插入仪表板(产品编辑)区域的方法并未提及如何更改仪表板中显示的字段顺序。 .

// 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 ) );
    }
}

如有任何帮助,我们将不胜感激!

  • 使用woocommerce_variation_options_pricing会放在价格后面(可能需要一些CSS)。这是我输入价格后找到的最接近的钩子。
  • 使用woocommerce_variation_options会把它放在价格输入之前
  • 模板中调用的钩子是woocommerce_variation_options_inventory

在这些情况下,您可以通过以下方式检查自己的可能性:

  • 找到相关的 Woocommerce 模板(在插件目录中)
  • 研究代码以找到 do_action(或 apply_filter 变量)以查看 "where and when you can do/update stuff"。在你的情况下 wp-content/plugins/woocommerce/includes/admin/meta-boxes/views/html-variation-admin.php:159
  • 有时你很幸运(仁慈的开发者),有时则不那么幸运:)