2017-07-07 44 views
1

關於我的上一個問題已成功地在Retrieving WC Custom Field回答我現在有選擇的字段,並希望添加自動完成的自定義字段(我還沒有研究這個)在後端自動填充WooCommerce自定義產品設置選擇器

問題: 1.)如何自動填充自定義選擇字段?

//Adding the custom field select 
woocommerce_wp_select( 
array( 
'id' => '_select', 
'label' => __('SIM Type', 'woocommerce'), 
'options' => array(
'one' => __('Regular', 'woocommerce'), 
'two' => __('Nano', 'woocommerce'), 
'three' => __('Micro', 'woocommerce') 
) 
) 
); 

//Saving 
$woocommerce_select = $_POST['_select']; 
if(!empty($woocommerce_select)) 
update_post_meta($post_id, '_select', esc_attr($woocommerce_select)); 

// Display Custom Field Value 
echo get_post_meta($post->ID, '_select', true); 

回答

3

更新:設置編程的<option>在選擇字段:

1)您將需要存儲你的選擇關鍵值的關聯數組:

// The associative array to store (once) 
$options_array = array(
    '' => __('Select a value', 'woocommerce'), // default empty value 
    'one' => __('Regular', 'woocommerce'), 
    'two' => __('Nano', 'woocommerce'), 
    'three' => __('Micro', 'woocommerce') 
); 

// Serialize the array as a string 
$option_str = maybe_serialize($options_array); 

// Save this array in Wordpress options 
update_option('my_custom_selector_options', $option_str); 

2)獲取並反序列化您的選擇器選項:

// Get your options select data 
$select_options_str = get_option('my_custom_selector_options'); 

// Unserialize this data: 
$select_options_arr = maybe_unserialize($select_options_str); 

// Get the saved selected 'value' if it exist 
$value = get_post_meta($post->ID, '_select', true); 
if(empty($value)) $value = ''; // When 'value' is not defined 

// 
woocommerce_wp_select(
    array(
     'id' => '_select', 
     'label' => __('SIM Type', 'woocommerce'), 
     'options' => $select_options_arr, 
     'value' => $value, 
    ) 
); 

因此,現在您的字段選擇器選項已被您從WordPress選項獲得的數據填充。


自動填寫的woocommerce_wp_select(),你必須添加一個'value'關鍵這樣:

## 1. The select (dropdown) 

// Get the 'value' data if it exist 
$value = get_post_meta($post->ID, '_select', true); 
if(empty($value)) $value = ''; // When 'value' is not defined 

woocommerce_wp_select(
    array(
     'id' => '_select', 
     'label' => __('SIM Type', 'woocommerce'), 
     'options' => array(
      '' => __('Select a value', 'woocommerce'), // Added a default empty value 
      'one' => __('Regular', 'woocommerce'), 
      'two' => __('Nano', 'woocommerce'), 
      'three' => __('Micro', 'woocommerce') 
     ), 
     'value' => $value, // <=== === === === === HERE set the 'value' key (autofill) 
    ) 
); 

## --------------------------------- 

## 2. SAVING 

$woocommerce_select = $_POST['_select']; 
// The Default empty value is not saved (added in this condition below) 
if(!empty($woocommerce_select) || $woocommerce_select != '') 
    update_post_meta($post_id, '_select', esc_attr($woocommerce_select)); 

快速測試:
要看到它在行動,取代例如:'value' => $value,'value' => 'two',
然後選擇值爲:納米 ...

enter image description here

+0

感謝嗨@LoicTheAztec但我要問的是如何添加值,如「常規」,「納米」和「微「在選擇編程方式像一個for循環。以及如何存儲和獲取?它是否也通過meta?謝謝;) –

+0

感謝您爲此,我會爲默認值執行此操作。 ;)它仍然有幫助。 ;) –

+0

這是很好的去。 :) 謝謝! –

相關問題