2016-09-15 5 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"を選択すると、I値"2"の代わりに"Affenpinscher"の代わりに値を取得します。

私は間違っていますか?どうすればこの問題を解決できますか?

おかげ

+0

あなたの配列は、フラットファイルとは何の関係もない、この質問の名前を変更することを検討してください。 – Devon

答えて

1

- 更新 - (テストや作業)

これは絶対にセレクタ<select>ドロップダウンのための正常な動作です。別の方法で動作させるには、コードに小さなものを追加するだけです。

変更はです:
- まず、外部テキストファイルからの値の配列が利用可能になったとき、私はワードプレスのオプションに保管してください。
- 第二には、最後の保存機能で、Iは、格納された配列を取得し、私は$_POST['_select_breed_key1'];から入手選択keyと、私は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'); 

あなたはプロダクトID(post_idの)ためwp_postmeta表、2 meta_keysにで今持っている:
- その店'_select_breed_value1' - 選択したキー
を格納'_select_breed_key1'対応する値

例えば

使い方(この値を取得する):

<?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

ウーホー!出来た!私は//選択フィールドでは... をしなければならなかっただけで1補正 - 私はちょうど更新していた犬種セクション:を! (!空($のwoocommerce_select))場合(空($のwc_select)場合 します) それ以外の場合はうまくいった! 私はまだアレイとボックスの特性を習得しなければなりません。 ありがとう、それは働いた.... 私はデータベースから引き離そうとするつもりだと思う! – JoeDostie

+0

ああ、申し訳ありません...名前を変更するときにこれを忘れました。更新しました。 – LoicTheAztec

関連する問題