2017-08-23 54 views
2

在WooCommerce中,當我提交時,如何捕獲添加到訂單中的自定義選擇字段編輯管理頁面?WooCommerce管理員訂單編輯保存帖子

我在文件class-wc-meta-box-order-data.php中添加了此自定義選擇字段。我得到這個:

enter image description here

但我不知道如何捕捉或保存$_POST['vendor']

我試圖在wp-admin/post.php添加$_POST['vendor']但它不工作

這是我添加的代碼:

<select class="wc-customer-search" id="customer_user" name="customer_user" data-placeholder="<?php esc_attr_e('Guest', 'woocommerce'); ?>" data-allow_clear="true"> 
           <option value="<?php echo esc_attr($user_id); ?>" selected="selected"><?php echo htmlspecialchars($user_string); ?></option> 
          </select> 
          <!--/email_off--> 
         </p> 
        <p> <label for="order_status">供應商: </label> 
        <select name="vendor"> 
    <?php 
      global $wpdb; 
    $user_count = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->users"); 
    for($i=1;$i<=$user_count;$i++){ 
    $user_info = get_userdata($i); 

    if (implode(', ', $user_info->roles)=='vendor') 
echo "<option value=".$user_info->user_login.">$user_info->user_login</option>"; 
    } 
        ?> 
</select></p> 

如何,我可以得到提交的值,我怎麼能保存呢?

回答

0

覆蓋核心文件是開發者禁止的東西。 所以這不是正確的做法。

執行此操作的方法是使用源代碼中的可用鉤子,而不是覆蓋此核心文件,因爲在插件更新時您將失去一切。

  1. 全部替換原有的核心文件
  2. 添加該代碼,而不是(我有做一些輕微的必要的修改)

這裏是替換代碼+鉤子將數據保存到訂單元數據:

add_action('woocommerce_admin_order_data_after_order_details', 'custom_code_after_order_details', 10, 1); 
function custom_code_after_order_details ($order) { 


    $value = get_post_meta($order->get_id(), '_vendor', true); 
    ?> 
    <p> <label for="order_status">供應商: </label> 
    <select name="vendor"> 
    <?php global $wpdb; 
     $user_count = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->users"); 
     echo '<option value="">Select a vendor</option>'; 
     for($i=1;$i<=$user_count;$i++){ 
      $user_info = get_userdata($i); 
      if (implode(', ', $user_info->roles)=='customer'){ 
       $user_login = $user_info->user_login; 
       $selected = $value == $user_login ? 'selected' : ''; 
       echo '<option '.$selected.' value="'.$user_login.'">'.$user_login.'</option>'; 
      } 
     } 
    ?> 
    </select></p> 
    <input type="hidden" name="custom_select_field_nonce" value="<?php echo wp_create_nonce(); ?>"> 
    <?php 
} 

add_action('save_post', 'save_custom_code_after_order_details', 10, 1); 
function save_custom_code_after_order_details($post_id) { 

    // We need to verify this with the proper authorization (security stuff). 

    // Check if our nonce is set. 
    if (! isset($_POST[ 'custom_select_field_nonce' ])) { 
     return $post_id; 
    } 
    $nonce = $_REQUEST[ 'custom_select_field_nonce' ]; 

    //Verify that the nonce is valid. 
    if (! wp_verify_nonce($nonce)) { 
     return $post_id; 
    } 

    // If this is an autosave, our form has not been submitted, so we don't want to do anything. 
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { 
     return $post_id; 
    } 

    // Check the user's permissions. 
    if ('page' == $_POST[ 'post_type' ]) { 

     if (! current_user_can('edit_page', $post_id)) { 
      return $post_id; 
     } 
    } else { 

     if (! current_user_can('edit_post', $post_id)) { 
      return $post_id; 
     } 
    } 

    // Update the meta field in the database. 
    update_post_meta($post_id, '_vendor', $_POST[ 'vendor' ]); 
} 

代碼放在您的活動子主題(或主題)的function.php文件或還在任何插件文件中。

此代碼已經過測試並可正常工作。

相關問題