dongpo1599 2017-09-05 09:27
浏览 152
已采纳

在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)?

  • 写回答

2条回答 默认 最新

  • doubi1910 2017-09-05 21:26
    关注

    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.

    • For the beginning you will use this hook: woocommerce_product_bulk_edit_start
    • For the end this one: 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.

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

    enter image description here

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?