我想添加一些自定义字段到管理WooCommerce产品批量编辑.

我使用以下内容将一些字段添加到管理产品快速编辑:

// 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 custom fields HTML in product BULK Edit
add_action( 'woocommerce_product_bulk_edit_end', 'mix_max_step_desc_bulk_edit_fields', );
function mix_max_step_desc_bulk_edit_fields() {
    $options_data = '';
    $options      = array(
        ''  => __( '— No change —', 'woocommerce' ),
        '1' => __( 'Change to:', 'woocommerce' ),
    );
   
    foreach ( $options as $option => $value ) {
        $options_data .= '<option value="' . esc_attr( $option ) . '">' . $value . '</option>';
    }

    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('<div class="inline-edit-group"><label>
        <span class="title">%s %s</span>
        <span class="select-wrap">
            <select class="change_%s change_to" name="change_%s">%s</select>
        </span></label>
        <label class="change-input input-text-wrap">
            <input id="%s" type="text" name="%s" class="text" value="" />
        </span></div>', $title, __('Qty'), $key, $key, $options_data, $key, $key );
    }

    // For the product description
    $key = 'description';
    printf('<div class="inline-edit-group"><label>
    <span class="title">%s</span>
    <span class="textarea-wrap">
        <select class="change_%s change_to" name="change_%s">%s</select>
    </span></label>
    <label class="change-input input-text-wrap">
        <textarea id="%s" name="%s" class="textarea" cols="22" rows="1"></textarea>
    </span></div>', __('Description'), $key, $key, $options_data, $key, $key );

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

// Save BULK edit custom fields values
add_action( 'woocommerce_product_bulk_edit_save', 'mix_max_step_desc_bulk_edit_save' );
function mix_max_step_desc_bulk_edit_save( $product ){
    $updated = false;
    
    // Loop through custom fields settings array
    foreach ( get_alg_wc_pq_fields() as $key => $title ) {
        if ( isset($_REQUEST['change_'.$key]) && $_REQUEST['change_'.$key] == 1 && isset($_REQUEST[$key]) ) {
            $product->update_meta_data($key, (!empty($_REQUEST[$key]) ? intval($_REQUEST[$key]) : ''));
            $updated = true;
        }
    }

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

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

代码位于您的子主题的unctions.php文件中(或在插件中).经过测试,效果良好.

Php相关问答推荐

Symfony Monolog:使用多个格式化程序

将带有值的属性添加到WooCommerce产品,保留现有属性

如何将对我的域的请求重定向到子文件夹中的索引,同时保留域URL并使用.htaccess?

在没有symfony应用程序的情况下使用安全Bundle 包时,缺少配置构建器类

Mod_重写子目录中相应的php文件

在PHP中,如何将介于0和1之间(包括0和1)的浮点概率转换为数组索引?

WHERE方法不能与有效查询Laravel一起使用

如何在php网站代码中清理浏览器缓存

Laravel处理belongToMany和额外字段

更改WooCommerce checkout 中的错误消息以获得不可用的送货方式

如何在codeigniter中将order_by RAND与下面的代码一起使用

在WooCommercel邮箱通知上添加来自Apaczka插件的选定交付点

wp_insert_post() 和 WordPress 中查询字符串的多个帖子

对 WooCommerce 购物车和 checkout 中显示的运输方式进行排序

如何使用不同的方法编写嵌套连接查询并将两列连接成一列在 laravel 查询生成器中

保留 .htaccess 中的符号登录 URL

如何通过额外的属性和本地化来使用 laravel 枚举

如果有很多重定向,PHP curl 的行为不像命令行 curl

PDO 和 SQL 查询结果中日期格式不同的原因可能是什么?

如果 static:: 被解析为B,为什么 PHP 会执行A的方法