如何在WooCommerce中添加仅在产品管理端可见的标签?

时间:2016-04-05 作者:Dipika

enter image description here

我是wordpress开发的新手。我想在新选项卡的产品页中添加设置。

如何添加设置

我试图添加选项卡的代码

 //add product tab link in admin
        add_action( \'woocommerce_product_write_panel_tabs\', array($this,\'woocommerce_product_write_panel_tabs\' ));
        //add product tab content in admin
        add_action(\'woocommerce_product_write_panels\', array($this,\'woocommerce_product_write_panels\'));

   /**
 * woocommerce_product_write_panel_tabs
 * Used to add a product custom tab to product edit screen
 * @return void
 */
function woocommerce_product_write_panel_tabs(){
    ?>
    <li class="custom_tab">
        <a href="#custom_tab_data_ctabs">
            <?php _e(\'Customstock Tabs\', \'GWP\'); ?>
        </a>
    </li>
    <?php
}

/**
 * woocommerce_product_write_panels
 * Used to display a product custom tab content (fields) to product edit screen
 * @return void
 */
function woocommerce_product_write_panels() {
    global $post,$woocommerce;
    $fields = array(
        array(
            \'key\'   => \'custom_tabs_ids\',
            \'label\' => __( \'Select Custom Tabs\', \'GWP\' ),
            \'desc\'  => __( \'Start typing the Custom Tab name, Used for including custom tabs.\', \'GWP\' )
        ),
        array(
            \'key\'   => \'exclude_custom_tabs_ids\',
            \'label\' => __( \'Select Global Tabs to exclude\', \'GWP\' ),
            \'desc\'  => __( \'Start typing the Custom Tab name. used for excluding global tabs.\', \'GWP\' )
        ),
        array(
            \'key\'   => \'id\',
            \'label\' => __( \'Select Global Tabs to eclude\', \'GWP\' ),
            \'desc\'  => __( \'Start typing the Custom Tab name. used for excluding global tabs.\', \'GWP\' )
        )
    );
}

1 个回复
最合适的回答,由SO网友:Mehul Gohil 整理而成

我一直在研究你的问题,并在谷歌搜索后找到了解决方案。

Note: 将下面提到的代码添加到主题functions.php 或任何插件文件。

Code:

此筛选函数将向Products Data 代谢箱

<?php  
add_filter( \'woocommerce_product_data_tabs\', \'add_my_custom_product_data_tab\' , 99 , 1 );
function add_my_custom_product_data_tab( $product_data_tabs ) {
    $product_data_tabs[\'my-custom-tab\'] = array(
        \'label\' => __( \'My Custom Tab\', \'my_text_domain\' ),
        \'target\' => \'my_custom_product_data\',
    );
    return $product_data_tabs;
}
此操作将向下添加的自定义选项卡添加自定义字段Products Data 代谢箱

add_action( \'woocommerce_product_data_panels\', \'add_my_custom_product_data_fields\' );
function add_my_custom_product_data_fields() {
    global $woocommerce, $post;
    ?>
    <!-- id below must match target registered in above add_my_custom_product_data_tab function -->
    <div id="my_custom_product_data" class="panel woocommerce_options_panel">
        <?php
        woocommerce_wp_checkbox( array( 
            \'id\'            => \'_my_custom_field\', 
            \'wrapper_class\' => \'show_if_simple\', 
            \'label\'         => __( \'My Custom Field Label\', \'my_text_domain\' ),
            \'description\'   => __( \'My Custom Field Description\', \'my_text_domain\' ),
            \'default\'       => \'0\',
            \'desc_tip\'      => false,
        ) );
        ?>
    </div>
    <?php
}
?>
保存产品的自定义字段数据选项卡:

add_action( \'woocommerce_process_product_meta\', \'woocommerce_process_product_meta_fields_save\' );
function woocommerce_process_product_meta_fields_save( $post_id ){
    // This is the case to save custom field data of checkbox. You have to do it as per your custom fields
    $woo_checkbox = isset( $_POST[\'_my_custom_field\'] ) ? \'yes\' : \'no\';
    update_post_meta( $post_id, \'_my_custom_field\', $woo_checkbox );
}
希望这有帮助!

相关推荐

如何在WordPress开发中添加带有ACF自定义字段ID的自定义metabox字段

我是wordpress开发的新手,我在我的项目中安装了高级自定义字段插件,并创建了两个文本字段名称&;我还创建了一个插件,可以在帖子中创建一个带有文本框的元框。现在在帖子中,我将获得自定义字段名称(&A);电子邮件和我的自定义元框旁边将出现,但我必须将我的元框附加到名称字段旁边,即在名称字段和电子邮件字段之间。我的metabox代码如下。请任何人帮帮我//Creating the custom meta box function my_notice_meta_box() {