将产品自定义字段添加到WooCommerce中的“管理产品"批量编辑表单中 [英] Add a product custom field to Admin product bulk edit form in WooCommerce

查看:106
本文介绍了将产品自定义字段添加到WooCommerce中的“管理产品"批量编辑表单中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在WooCommerce产品中添加了一个自定义字段,例如此问题/答案:

I have added a custom field in my WooCommerce products like in this question/answer:
Display a custom product field before short description in WooCommerce.

是否可以将此自定义字段添加到产品批量修改特殊页面(可从管理产品列表页面访问)?

Is it possible to add this custom field to the product bulk edit special page (accessible from Admin products list page)?

推荐答案

是的,可以批量编辑自定义字段 '_text_field' 的产品(如链接的问题/答案中所示) .

Yes it's possible to bulk edit products for your custom field '_text_field' (as in your linked question/answer).

您可以在编辑页面的开头或结尾添加此自定义字段.

You can add this custom field at the beginning or at the end of edit page.

  • 首先,您将使用以下挂钩: woocommerce_product_bulk_edit_start
  • 最后一个: woocommerce_product_bulk_edit_end

代码(自定义字段位于此处的开头):

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

代码会出现在您活动的子主题(或主题)的function.php文件或任何插件文件中.

此代码已经过测试并且可以正常工作.您会得到的:

This code is tested and works. You will get this:

这篇关于将产品自定义字段添加到WooCommerce中的“管理产品"批量编辑表单中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆