wordpress 在WooCommerce 3.2+中添加自定义字段到管理产品批量编辑

hivapdat  于 2023-10-17  发布在  WordPress
关注(0)|答案(1)|浏览(115)

我想添加一些自定义字段到管理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;
}

该代码的工作原理,但我想有这些领域也在管理产品批量编辑填充。

omtl5h9j

omtl5h9j1#

按照并使用添加自定义字段到管理产品快速编辑在WooCommerce 3.2+回答代码到您以前的问题之一,下面的代码将显示所需的自定义字段在管理产品批量编辑,并将允许保存更改的数据从选定的产品:

// 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文件中(或插件中)。测试和作品。

相关问题