簡體   English   中英

在 WooCommerce 中向管理產品批量編輯表單添加產品自定義字段

[英]Add a product custom field to Admin product bulk edit form in WooCommerce

我在我的 WooCommerce 產品中添加了一個自定義字段,如這個問題/答案:
在 WooCommerce 中的簡短描述之前顯示自定義產品字段

是否可以將此自定義字段添加到產品批量編輯特殊頁面(可從管理產品列表頁面訪問)

是的,可以為您的自定義字段'_text_field'批量編輯產品(如您鏈接的問題/答案)

您可以在編輯頁面的開頭或結尾添加此自定義字段。

  • 一開始你會使用這個鈎子: woocommerce_product_bulk_edit_start
  • 最后這個: woocommerce_product_bulk_edit_end

代碼(自定義字段在此處的開頭)

// Add a custom field to product bulk edit special page
add_action( 'woocommerce_product_bulk_edit_start', 'custom_field_product_bulk_edit', 10, 0 );
function custom_field_product_bulk_edit() {
    ?>
        <div class="inline-edit-group">
            <label class="alignleft">
                <span class="title"><?php _e('T. dostawy', 'woocommerce'); ?></span>
                <span class="input-text-wrap">
                    <select class="change_t_dostawy change_to" name="change_t_dostawy">
                    <?php
                        $options = array(
                            ''  => __( '— No change —', 'woocommerce' ),
                            '1' => __( 'Change to:', 'woocommerce' ),
                        );
                        foreach ( $options as $key => $value ) {
                            echo '<option value="' . esc_attr( $key ) . '">' . $value . '</option>';
                        }
                    ?>
                    </select>
                </span>
            </label>
            <label class="change-input">
                <input type="text" name="_t_dostawy" class="text t_dostawy" placeholder="<?php _e( 'Enter Termin dostawy', 'woocommerce' ); ?>" value="" />
            </label>
        </div>
    <?php
}

// Save the custom fields data when submitted for product bulk edit
add_action('woocommerce_product_bulk_edit_save', 'save_custom_field_product_bulk_edit', 10, 1);
function save_custom_field_product_bulk_edit( $product ){
    if ( $product->is_type('simple') || $product->is_type('external') ){
        $product_id = method_exists( $product, 'get_id' ) ? $product->get_id() : $product->id;

        if ( isset( $_REQUEST['_t_dostawy'] ) )
            update_post_meta( $product_id, '_text_field', sanitize_text_field( $_REQUEST['_t_dostawy'] ) );
    }
}

代碼位於活動子主題(或主題)的 function.php 文件或任何插件文件中。

此代碼經過測試並有效。 你會得到這個:

在此處輸入圖片說明

是的,這是可能的,並且沒有任何鈎子和代碼:

  • 安裝插件 WooCommerce 批量編輯器(WOOBE): https ://wordpress.org/plugins/woo-bulk-editor/
  • 轉到選項卡 Meta Fields 並在那里添加您的元鍵,按 Save 按鈕
  • 在選項卡設置中使用該新元鍵激活列
  • 使用選項卡批量編輯進行操作

就這些 :)

ps 文檔: https : //bulk-editor.com/document/meta-fields/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM