向管理产品添加自定义字段在 WooCommerce 3+ 中批量编辑

问题描述 投票:0回答:1

我想向管理 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;
}

代码有效,但我希望在管理产品批量编辑中也填充这些字段。

php wordpress woocommerce custom-fields bulkupdate
1个回答
0
投票

遵循并使用 在 WooCommerce 3+ 中向管理产品添加自定义字段快速编辑回答您之前问题之一的代码,下面的代码将在管理产品批量编辑中显示所需的自定义字段,并允许从所选产品:

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

代码位于子主题的functions.php 文件中(或插件中)。已测试并有效。

© www.soinside.com 2019 - 2024. All rights reserved.