我使用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' );
My breed.txt文件包含3行(项):
Please Select a breed...
Abyssinian
Affenpinscher
生成的数组是这样的:
Array (
[0] => Please Select a breed...
[1] => Abyssinian
[2] => Affenpinscher
)
因此,当我选择 "Affenpinscher"
例如,我得到的值 "2"
而不是 "Affenpinscher"
。
我做错了什么?如何解决这个问题?
谢谢
-已更新- (已测试并正常工作)
这绝对是下拉选择器 <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
表中有一个Product 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;
?>