2016-09-15 51 views
4

我正在使用Woocommerce,並在管理面板中創建了一個選擇框。我通過一個平面文件填充選擇框中的信息。 一切工作正常(幾乎)。WooCommerce - 提交後檢索選擇框的正確數據值

我堅持的部分上後,我選擇「選擇」我要和保存我收到陣列$key位置,而不是實際$value。我很近,但我不能把它放在手指上。

更新:這裏是我的全碼:

function woo_add_custom_admin_product_tab() { 
?> 
    <li class="custom_tab"><a href="#custom_tab_data"><?php _e('Additional Information', 'woocommerce'); ?></a></li> 
<?php 
} 
add_action('woocommerce_product_write_panel_tabs', 'woo_add_custom_admin_product_tab'); 


function woo_add_custom_admin_fields() {  
    global $woocommerce, $post; 

    echo '<div id="custom_tab_data" class="panel woocommerce_options_panel">'; 
    echo '<div class="options_group">'; 

    // Select - Breed1 
    if (file_exists (plugin_dir_path(__FILE__) .'breed.txt')) { 
     $breedData = file_get_contents (plugin_dir_path(__FILE__) .'breed.txt'); 
     $breedArray = explode ("\n", $breedData); 
    } 

    woocommerce_wp_select(array(
     'id'  => '_select_breed1', 
     'label' => __('Select Primary Breed', 'woocommerce'), 
     'desc_tip' => 'true', 
     'description' => __('Select the primary breed of the pet.', 'woocommerce'), 
     'options' => $breedArray 
    )); 
    echo '</div>'; 
    echo '</div>'; 
} 
add_action('woocommerce_product_write_panels', 'woo_add_custom_admin_fields'); 


// Save Fields; 
function woo_add_custom_general_fields_save($post_id){ 
    // Text Field - Pet Name 
    $woocommerce_text_field = $_POST['_pet_name']; 
    if(!empty($woocommerce_text_field)) 
     update_post_meta($post_id, '_pet_name', esc_attr($woocommerce_text_field)); 

    // Select Field - Breed 
    $woocommerce_select = $_POST['_select_breed1']; 
    if(!empty($woocommerce_select)) 
     update_post_meta($post_id, '_select_breed1', esc_attr($woocommerce_select)); 
} 
add_action('woocommerce_process_product_meta', 'woo_add_custom_general_fields_save'); 

我breed.txt文件包含3條線(項目):

Please Select a breed... 
Abyssinian 
Affenpinscher 

而產生的陣列看起來像這樣:

Array ( 
    [0] => Please Select a breed... 
    [1] => Abyssinian 
    [2] => Affenpinscher 
) 

所以當我選擇"Affenpinscher"例如,我得到價值"2"而不是"Affenpinscher"

我做錯了什麼?我該如何解決這個問題?

感謝

+0

您的數組與平面文件無關,請考慮重命名此問題。 – Devon

回答

1

- 更新 - (測試工作)

這絕對是一個下拉選擇<select>一種正常的行爲。你只需要在你的代碼中添加一些小東西來使它以不同的方式工作。

的變化是
- 首先,當值從外部文本文件中的陣列是可用的,我把它保存在WordPress的選項。
- 其次,在過去的節電功能,我得到的存儲陣列,並與選定的key,我從$_POST['_select_breed_key1'];搞定了,我找回我存儲在一個新項目(新行中wp_postmeta表中的相應值。

//Create the fields 
function woo_add_custom_admin_fields() {  
    global $woocommerce, $post; 

    echo '<div id="custom_tab_data" class="panel woocommerce_options_panel">'; 
    echo '<div class="options_group">'; 

    // Select - Breed1 
    if (file_exists (plugin_dir_path(__FILE__) .'breed.txt')) { 
     $breedData = file_get_contents (plugin_dir_path(__FILE__) .'breed.txt'); 
     $breedArray = explode ("\n", $breedData); 

     //Storing the array in wp_options table 
     if(get_option('wc_product_add_info_tab')) 
      update_option('wc_product_add_info_tab', $breedArray); 
     else 
      add_option('wc_product_add_info_tab', $breedArray); 
    } 

    woocommerce_wp_select(array(
     'id'  => '_select_breed_key1', 
     'label' => __('Select Primary Breed', 'woocommerce'), 
     'desc_tip' => 'true', 
     'description' => __('Select the primary breed of the pet.', 'woocommerce'), 
     'options' => $breedArray 
    )); 

    echo '</div>'; 
    echo '</div>'; 
} 
add_action('woocommerce_product_write_panels', 'woo_add_custom_admin_fields'); 


// Save Created Fields; 
function woo_add_custom_general_fields_save($post_id){ 

    // Select Field - Breed 
    $wc_select = $_POST['_select_breed_key1']; 
    if(!empty($wc_select)) 
     update_post_meta($post_id, '_select_breed_key1', esc_attr($wc_select)); 

    // Saving the corresponding value (from "$wc_select" selected key) to database 
    if(get_option('wc_product_add_info_tab')) { 

     // Getting the array 
     $breed_arr = get_option('wc_product_add_info_tab'); 

     // Saving the corresponding value 
     update_post_meta($post_id, '_select_breed_value1', $breed_arr[$wc_select]); 
    } 
} 
add_action('woocommerce_process_product_meta', 'woo_add_custom_general_fields_save'); 

你現在有在wp_postmeta表的產品ID(POST_ID),2個meta_keys:
- 存儲所選擇的關鍵
'_select_breed_key1' - '_select_breed_value1',存儲相應的值

使用範例(獲取該值):

<?php 

// Third parameter is set to "true" as it is a string (Not an array) 
$breed_value1 = get_post_meta($post_id, '_select_breed_value1', true); 
echo $breed_value1; 

?> 
+0

非常感謝Loic! 看起來我正朝着正確的方向前進,我需要獲得數組的[$ value]。 不幸的是它沒有像我期望的那樣工作。當我單擊更新時,該值不會保存到數據庫中。只是一個參考,我也使用函數來完成這個,這裏是我的完整代碼: – JoeDostie

+0

Woo Hoo!有效! (!empty($ wc_select)) if(!empty($ wc_select))如果) 但是,否則它的工作! 我仍然需要學習數組和選擇框的特徵。 但是,謝謝,它的工作.... 我想我會嘗試從數據庫拉! – JoeDostie

+0

哦,是的,抱歉......重命名時忘記了這一個。更新。 – LoicTheAztec