简体   繁体   English

在 WooCommerce 中向管理产品批量编辑表单添加产品自定义字段

[英]Add a product custom field to Admin product bulk edit form in WooCommerce

I have added a custom field in my WooCommerce products like in this question/answer:我在我的 WooCommerce 产品中添加了一个自定义字段,如这个问题/答案:
Display a custom product field before short description in WooCommerce . 在 WooCommerce 中的简短描述之前显示自定义产品字段

Is it possible to add this custom field to the product bulk edit special page (accessible from Admin products list page) ?是否可以将此自定义字段添加到产品批量编辑特殊页面(可从管理产品列表页面访问)

Yes it's possible to bulk edit products for your custom field '_text_field' (as in your linked question/answer) .是的,可以为您的自定义字段'_text_field'批量编辑产品(如您链接的问题/答案)

You can add this custom field at the beginning or at the end of edit page.您可以在编辑页面的开头或结尾添加此自定义字段。

  • For the beginning you will use this hook: woocommerce_product_bulk_edit_start一开始你会使用这个钩子: woocommerce_product_bulk_edit_start
  • For the end this one: woocommerce_product_bulk_edit_end最后这个: woocommerce_product_bulk_edit_end

The code (the custom field is at the beginning here) :代码(自定义字段在此处的开头)

// Add a custom field to product bulk edit special page
add_action( 'woocommerce_product_bulk_edit_start', 'custom_field_product_bulk_edit', 10, 0 );
function custom_field_product_bulk_edit() {
    ?>
        <div class="inline-edit-group">
            <label class="alignleft">
                <span class="title"><?php _e('T. dostawy', 'woocommerce'); ?></span>
                <span class="input-text-wrap">
                    <select class="change_t_dostawy change_to" name="change_t_dostawy">
                    <?php
                        $options = array(
                            ''  => __( '— No change —', 'woocommerce' ),
                            '1' => __( 'Change to:', 'woocommerce' ),
                        );
                        foreach ( $options as $key => $value ) {
                            echo '<option value="' . esc_attr( $key ) . '">' . $value . '</option>';
                        }
                    ?>
                    </select>
                </span>
            </label>
            <label class="change-input">
                <input type="text" name="_t_dostawy" class="text t_dostawy" placeholder="<?php _e( 'Enter Termin dostawy', 'woocommerce' ); ?>" value="" />
            </label>
        </div>
    <?php
}

// Save the custom fields data when submitted for product bulk edit
add_action('woocommerce_product_bulk_edit_save', 'save_custom_field_product_bulk_edit', 10, 1);
function save_custom_field_product_bulk_edit( $product ){
    if ( $product->is_type('simple') || $product->is_type('external') ){
        $product_id = method_exists( $product, 'get_id' ) ? $product->get_id() : $product->id;

        if ( isset( $_REQUEST['_t_dostawy'] ) )
            update_post_meta( $product_id, '_text_field', sanitize_text_field( $_REQUEST['_t_dostawy'] ) );
    }
}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.代码位于活动子主题(或主题)的 function.php 文件或任何插件文件中。

This code is tested and works.此代码经过测试并有效。 You will get this:你会得到这个:

在此处输入图片说明

Yes it is possible, and without any hooks and code:是的,这是可能的,并且没有任何钩子和代码:

  • Install plugin WooCommerce Bulk Editor (WOOBE): https://wordpress.org/plugins/woo-bulk-editor/安装插件 WooCommerce 批量编辑器(WOOBE): https ://wordpress.org/plugins/woo-bulk-editor/
  • Go to tab Meta Fields and add your meta key there, press Save button转到选项卡 Meta Fields 并在那里添加您的元键,按 Save 按钮
  • In tab Settings activate column with that new meta key在选项卡设置中使用该新元键激活列
  • Using tab Bulk Edit do your manipulations使用选项卡批量编辑进行操作

That is all :)就这些 :)

ps docs: https://bulk-editor.com/document/meta-fields/ ps 文档: https : //bulk-editor.com/document/meta-fields/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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