2017-08-28 19 views
0

我正嘗試在woocommerce中創建一個下拉列表框,但是它已使用數據庫中的數據填充。woocommerce_wp_select產品屬性條款中的選項數組

我有大部分的代碼工作,但部分填充列表框正在殺死我。這是我迄今爲止所擁有的。

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'=> $ value, 必須從代碼中刪除才能發佈到數據庫。 否則它的作品完美。如果這些線路需要在那裏,我沒有得到它。 – JoeDostie

+0

@JoeDostie **代碼更新:**我的部分出現了一個小錯誤...我糾正了這個問題...... **現在它正在工作**(用'get_post_meta()'代替'update_post_meta()') – LoicTheAztec

相關問題