2017-09-05 128 views
1

我在我的WooCommerce產品中添加了一個自定義字段,例如在此問題/答案中:
Display a custom product field before short description in WooCommerce在WooCommerce中將產品自定義字段添加到管理產品批量修改表單中

是否有可能這個自定義字段添加到產品批量編輯專頁(從管理員的產品列表頁面訪問)?

+0

你是什麼意思的質量? – Reigel

+0

@Reigel我選擇了幾種產品,我從水平菜單中選擇編輯。你看:1. https://ibb.co/jqd0wv - 選擇並點擊「編輯」按鈕。 2 - 我可以一次編輯多個產品 - https://ibb.co/cT0KpF – interlaw

+0

在谷歌搜索'wordpress批量操作tut'。 – Reigel

回答

2

是有可能大批量編輯產品,爲您的自定義字段'_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文件中,或者也在任何插件文件中。

此代碼已經過測試並可正常工作。你會得到這個:

enter image description here

+0

謝謝!!! :) – interlaw

相關問題