首页 > 解决方案 > 为 woocommerce 产品选项卡上的自定义字段设置优先级

问题描述

当产品很简单时,我想在常规数据选项卡中添加两个自定义字段。但是,我希望这些自定义字段出现在列表顶部。

我已经尝试在钩子中设置优先级,但这不起作用。

所以我有以下内容:

function add_options_simple() {
    global $woocommerce, $post;
    $id = $post->ID;
    $product = wc_get_product($id)->get_type();
    if ($product == 'simple'){
    woocommerce_wp_text_input(
        array(
            'id'          => '_brand',
            'label'       => 'Brand',
            'type'        => 'text',
        )
    );
    woocommerce_wp_text_input(
        array(
            'id'          => '_EAN',
            'label'       => 'EAN:',
            'type'        => 'text',
        )
    );
    }
}
add_action( 'woocommerce_product_options_general_product_data', 'add_options_simple',1,3 );

但没有成功。有什么建议么?

标签: woocommercetabscustom-fields

解决方案


无法将这些字段添加到顶部。正如你在这里看到的

但是您可以尝试使用 jQuery。试试下面的代码。

function add_options_simple() {
    global $woocommerce, $post;
    $id = $post->ID;
    $product = wc_get_product($id)->get_type();
    if ($product == 'simple'){ ?>
        <div class="options_group brand_ean show_if_simple hidden">
            <?php
            woocommerce_wp_text_input(
                array(
                    'id'          => '_brand',
                    'label'       => 'Brand',
                    'type'        => 'text',
                )
            );
            woocommerce_wp_text_input(
                array(
                    'id'          => '_EAN',
                    'label'       => 'EAN:',
                    'type'        => 'text',
                )
            ); 
           ?>
        </div>
        <script type="text/javascript">
            var brand_ean = jQuery('.brand_ean')[0].outerHTML;
            jQuery('.brand_ean').remove();
            jQuery( brand_ean ).insertBefore('.options_group.pricing');
        </script>
    <?php }
}
add_action( 'woocommerce_product_options_general_product_data', 'add_options_simple', 1, 3 );

测试和工作

在此处输入图像描述


推荐阅读