phpwordpresswoocommercecustom-fieldsbulkupdate

Add custom fields to admin products Bulk Edit in WooCommerce 3.2+


I would like to add some custom fields to the Admin WooCommerce Products Bulk Edit.

I am using the following to add some fields to Admin products Quick Edit:

// 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;
}

The code works, but I would like to have these fields populated also in Admin Product bulk edit.


Solution

  • Following and using Add custom fields to admin products Quick Edit in WooCommerce 3.2+ answer code to one of your previous questions, the code below will display the required custom fields in Admin Products BULK Edit and will allow saving the changed data from the selected products:

    // 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
        }
    }
    

    Code goes in functions.php file of your child theme (or in a plugin). Tested and works.