2017-08-28 5 views
0

woocommerceでドロップダウンリストボックスを作成しようとしていますが、データベースのデータが入力されています。woocommerce_wp_select options製品属性の用語からの配列

私はコードの大半を使用していますが、リストボックスを埋める部分が私を殺しています。これは私がこれまで持っていたものです。

add_action('woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields'); // Display Extra Fields on General Tab Section 


add_action('woocommerce_process_product_meta', 'woo_add_custom_general_fields_save'); // Save Fields 

function woo_add_custom_general_fields() { 
    global $woocommerce, $post, $product; 
    echo '<div class="options_group">'; 

    // Select 

    $options = array(
     'hide_empty' => false, 
     'order' => 'ASC', 
     'fields' => 'names' 
     ); 
    $DogBreeds = get_terms('pa_breed', $options); 

    foreach ($DogBreeds as $key => $value) { 
     $theArray = "'{$value}' => __('{$value}' , 'woocommerce'), "; 
    } 

    woocommerce_wp_select( 
     array( 
      'id'  => '_select', 
      'label' => __('My Select Field', 'woocommerce'), 
      'options' => $theArray //this is where I am having trouble 
      ) 
     ); 

     echo $theArray; 
     echo '<pre>'; 
     var_dump($DogBreeds); 
     echo '</pre>'; 

    echo '</div>'; 
} 

// Save Fields 
    function woo_add_custom_general_fields_save($post_id){ 

    // Select 
     $woocommerce_select = $_POST['_select']; 
     if(!empty($woocommerce_select)) 
      update_post_meta($post_id, '_select', esc_attr($woocommerce_select)); 
     else { 
      update_post_meta($post_id, '_select', Null); 
     } 
    } 

これは、WooCommerceの「ATTRIBUTES」セクションの情報を取得するはずです。私は繁殖属性を作成し、そこにいくつかの犬の品種を入れます。

いずれの方向も大変ありがとうございます!

私は配列の「オプション」セクションが完全に間違っていることを知っていますが、私はそこに配置して、私が達成しようとしていることを知っています。

答えて

1

私はあなたのコードを少し改訂しました。主な問題は、選択<option>アレイにありました。あなたは、コードに変更を確認します

// Display Extra Fields on General Tab Section 
add_action('woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields'); 
function woo_add_custom_general_fields() { 
    global $post; 

    // Set HERE the product attribute taxonomy 
    $taxonomy = 'pa_breed'; 

    // Get the selected value <== <== (updated) 
    $value = get_post_meta($post->ID, '_select', true); 
    if(empty($value)) $value = ''; 

    $dog_breeds = get_terms($taxonomy, array(
     'hide_empty' => false, 
     'order' => 'ASC', 
     'fields' => 'names' 
    )); 

    $options[''] = __('Select a value', 'woocommerce'); // default value 

    foreach ($dog_breeds as $key => $term) 
     $options[$term] = $term; // <=== <=== <=== Here the correct array 

    echo '<div class="options_group">'; 

    woocommerce_wp_select(array(
     'id'  => '_select', 
     'label' => __('My Select Field', 'woocommerce'), 
     'options' => $options, //this is where I am having trouble 
     'value' => $value, 
    )); 

    echo '</div>'; 
} 

// Save Fields 
add_action('woocommerce_process_product_meta', 'woo_add_custom_general_fields_save'); 
function woo_add_custom_general_fields_save($post_id){ 

// Select 
    $woocommerce_select = $_POST['_select']; 
    if(!empty($woocommerce_select)) 
     update_post_meta($post_id, '_select', esc_attr($woocommerce_select)); 
    else { 
     update_post_meta($post_id, '_select', ''); 
    } 
} 

コードは、あなたのアクティブな子テーマ(またはテーマ)のfunction.phpファイルやも任意のプラグインファイルになります。

WooCommerce 3+でテストされ、動作します。あなたは(あなたの犬種との)このに似た何かを取得します:

enter image description here

+0

おかげロイック!!!!できます! :) 質問... $ value配列の理由は何ですか? $ value配列を削除するまで、このコードは私のためには機能しませんでした。 – JoeDostie

+0

コードをコピーして貼り付け、データベースに保存しませんでした。行: $ value = update_post_meta($ post-> ID、 '_select'、true); if(empty($ value))$ value = ''; 'value' => $値、 をデータベースから削除する必要がありました。 それ以外の場合は完全に動作します。もしそれらの行が何かのためにそこにいる必要があれば、私はそれを得ていない。 – JoeDostie

+0

@JoeDostie **コードが更新されました:**私の一部の間違いがありました...問題を修正しました... **現在動作中です**(update_post_meta()を 'get_post_meta()'に置き換えました) – LoicTheAztec

関連する問題