You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在WooCommerce 3.2+后台批量编辑产品中添加自定义字段

在WooCommerce后台批量编辑中添加自定义字段(Min/Max/Step+产品描述)

你已经实现了快速编辑中的自定义字段,要扩展到批量编辑功能,只需新增以下代码并整合到现有代码中:

1. 在批量编辑面板添加字段HTML

通过钩子将自定义字段插入批量编辑表单:

// 批量编辑面板添加自定义字段
add_action( 'bulk_edit_custom_box', 'min_max_step_desc_bulk_edit_fields', 10, 2 );
function min_max_step_desc_bulk_edit_fields( $column, $post_type ) {
    if ( $post_type !== 'product' || $column !== 'name' ) return;

    echo '<div class="inline-edit-col">';

    // 循环添加Min/Max/Step字段
    foreach ( get_alg_wc_pq_fields() as $key => $title ) {
        printf(
            '<label><span class="title">%s %s</span>
            <span class="input-text-wrap">
                <input type="text" name="%s" class="text bulk-edit-input" value="" />
            </span></label><br class="clear">',
            $title, __('Qty'), $key
        );
    }

    // 添加产品描述字段
    printf(
        '<label><span class="title">%s</span>
        <span class="textarea-wrap">
            <textarea name="description" class="textarea bulk-edit-textarea" cols="22" rows="1"></textarea>
        </span></label>',
        __('Description')
    );

    echo '</div>';
}

2. 处理批量编辑的保存逻辑

通过批量操作钩子处理选中产品的字段更新:

// 保存批量编辑的自定义字段值
add_filter( 'handle_bulk_actions-edit-product', 'min_max_step_desc_bulk_edit_save', 10, 3 );
function min_max_step_desc_bulk_edit_save( $redirect_to, $action, $post_ids ) {
    if ( $action !== 'edit' ) return $redirect_to;

    // 检查权限
    if ( ! current_user_can( 'edit_products' ) ) return $redirect_to;

    // 循环处理每个选中的产品
    foreach ( $post_ids as $post_id ) {
        $product = wc_get_product( $post_id );
        if ( ! $product ) continue;

        $updated = false;

        // 更新Min/Max/Step元数据
        foreach ( get_alg_wc_pq_fields() as $key => $title ) {
            if ( isset( $_REQUEST[$key] ) && $_REQUEST[$key] !== '' ) {
                $product->update_meta_data( $key, intval( $_REQUEST[$key] ) );
                $updated = true;
            }
        }

        // 更新产品描述
        if ( isset( $_REQUEST['description'] ) && $_REQUEST['description'] !== '' ) {
            $product->set_description( sanitize_textarea_field( $_REQUEST['description'] ) );
            $updated = true;
        }

        if ( $updated ) {
            $product->save();
        }
    }

    return $redirect_to;
}

完整整合代码

将上述新增代码与你原有的代码合并,最终完整代码如下:

// Min Max Step fields settings
function get_alg_wc_pq_fields() {
    return array(
        '_alg_wc_pq_min'  => __('Min'), 
        '_alg_wc_pq_max'  => __('Max'), 
        '_alg_wc_pq_step' => __('Step')
    );
}

// Add hidden custom field data to admin product list
add_action( 'manage_product_posts_custom_column', 'admin_product_list_hidden_custom_field_data', 100, 2 );
function admin_product_list_hidden_custom_field_data( $column, $post_id ) {
    global $product;

    if( $column === 'name' ) {
        echo '<div class="hidden" id="cfields_inline_'.$post_id.'">';

        // Loop through custom fields settings array
        foreach ( get_alg_wc_pq_fields() as $key => $title ) {
            printf('<div class="%s">%s</div>', 
            $key, $product->get_meta($key));
        }

        // For the product description
        printf('<div class="description">%s</div></div>', $product->get_description());
    }
}

// Add custom fields HTML in product Quick Edit
add_action( 'woocommerce_product_quick_edit_end',  'mix_max_step_desc_quick_edit_fields' );
function mix_max_step_desc_quick_edit_fields() {
    echo '<br class="clear"><br><fieldset class="inline-edit-col-wide">
    <div class="inline-edit-col">';

    // Loop through custom fields settings array
    foreach ( get_alg_wc_pq_fields() as $key => $title ) {
       
        printf('<label><span class="title">%s %s</span>
        <span class="input-text-wrap">
            <input id="%s" type="text" name="%s" class="text" value="" />
        </span></label><br class="clear">', $title, __('Qty'), $key, $key );
    }

    // For the product description
    printf('<label><span class="title">%s</span>
    <span class="textarea-wrap">
        <textarea id="%s" name="%s" class="textarea" cols="22" rows="1"></textarea>
    </span></label>', __('Description'), 'description', 'description' );

    echo '</div></fieldset>';
}

// Save quick edit custom fields values
add_action( 'woocommerce_product_quick_edit_save', 'min_max_step_desc_quick_edit_save' );
function min_max_step_desc_quick_edit_save( $product ){
    $updated = false;

    // Loop through custom fields settings array
    foreach ( get_alg_wc_pq_fields() as $key => $title ) {
        if ( isset($_REQUEST[$key]) ) {
            $product->update_meta_data($key, (!empty($_REQUEST[$key]) ? intval($_REQUEST[$key]) : ''));
            $updated = true;
        }
    }

    // For the product description
    if ( isset($_REQUEST['description']) ) {
        $product->set_description(!empty($_REQUEST['description']) ? sanitize_textarea_field($_REQUEST['description']) : '');
        $updated = true;
    }

    if ( $updated ) {
        $product->save(); // save product meta data
    }
}

// Javascript: Populate quick edit additional fields
add_action( 'admin_footer', 'min_max_step_desc_quick_edit_js');
function min_max_step_desc_quick_edit_js(){
    global $pagenow, $typenow;

    if( 'edit.php' === $pagenow && 'product' === $typenow ) :
    ?>
    <script id="woocommerce-quickedit-custom">
    jQuery( function($){
        $('#the-list').on('click', '.editinline', function(){
            inlineEditPost.revert();
            var post_id = $(this).closest('tr').attr('id');
            post_id = post_id.replace('post-', '');
    <?php $i = 0; 
        // Loop through custom fields settings array
        foreach ( get_alg_wc_pq_fields() as $key => $title ) : $i++; ?>
            const value<?php echo $i; ?> = $('#cfields_inline_'+post_id+' .<?php echo $key; ?>').text();
            $('input[name="<?php echo $key; ?>"]', '.inline-edit-row').val(value<?php echo $i; ?>);
    <?php endforeach; ?>
            // For the product description
            const value4 = $('#cfields_inline_'+post_id+' .description').text();
            $('textarea[name="description"]', '.inline-edit-row').val(value4);
        });
    });
    </script>
    <?php
    endif;
}

// 批量编辑面板添加自定义字段
add_action( 'bulk_edit_custom_box', 'min_max_step_desc_bulk_edit_fields', 10, 2 );
function min_max_step_desc_bulk_edit_fields( $column, $post_type ) {
    if ( $post_type !== 'product' || $column !== 'name' ) return;

    echo '<div class="inline-edit-col">';

    // 循环添加Min/Max/Step字段
    foreach ( get_alg_wc_pq_fields() as $key => $title ) {
        printf(
            '<label><span class="title">%s %s</span>
            <span class="input-text-wrap">
                <input type="text" name="%s" class="text bulk-edit-input" value="" />
            </span></label><br class="clear">',
            $title, __('Qty'), $key
        );
    }

    // 添加产品描述字段
    printf(
        '<label><span class="title">%s</span>
        <span class="textarea-wrap">
            <textarea name="description" class="textarea bulk-edit-textarea" cols="22" rows="1"></textarea>
        </span></label>',
        __('Description')
    );

    echo '</div>';
}

// 保存批量编辑的自定义字段值
add_filter( 'handle_bulk_actions-edit-product', 'min_max_step_desc_bulk_edit_save', 10, 3 );
function min_max_step_desc_bulk_edit_save( $redirect_to, $action, $post_ids ) {
    if ( $action !== 'edit' ) return $redirect_to;

    // 检查权限
    if ( ! current_user_can( 'edit_products' ) ) return $redirect_to;

    // 循环处理每个选中的产品
    foreach ( $post_ids as $post_id ) {
        $product = wc_get_product( $post_id );
        if ( ! $product ) continue;

        $updated = false;

        // 更新Min/Max/Step元数据
        foreach ( get_alg_wc_pq_fields() as $key => $title ) {
            if ( isset( $_REQUEST[$key] ) && $_REQUEST[$key] !== '' ) {
                $product->update_meta_data( $key, intval( $_REQUEST[$key] ) );
                $updated = true;
            }
        }

        // 更新产品描述
        if ( isset( $_REQUEST['description'] ) && $_REQUEST['description'] !== '' ) {
            $product->set_description( sanitize_textarea_field( $_REQUEST['description'] ) );
            $updated = true;
        }

        if ( $updated ) {
            $product->save();
        }
    }

    return $redirect_to;
}

说明

  • 批量编辑字段会出现在批量编辑面板的「名称」列下方,与快速编辑字段样式保持一致
  • 批量保存时仅更新填写了内容的字段,空字段不会覆盖产品原有值
  • 保留了你原有的快速编辑功能,两者可独立使用

内容的提问来源于stack exchange,提问作者AlexTSF

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 02:27:08