WooCommerce - 提交后检索选择框的正确数据值

Joe*_*tie 5 php arrays wordpress html-select 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' );
Run Code Online (Sandbox Code Playgroud)

我的breed.txt 文件包含3 行(项目):

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' );
Run Code Online (Sandbox Code Playgroud)

生成的数组如下所示:

Please Select a breed...
Abyssinian
Affenpinscher
Run Code Online (Sandbox Code Playgroud)

因此"Affenpinscher",例如,当我选择时,我得到的"2"不是 "Affenpinscher".

我做错了什么?我该如何解决这个问题?

谢谢

Loi*_*tec 1

\xe2\x80\x94 更新了 \xe2\x80\x94 (已测试且工作)

\n\n

这绝对是下拉选择器的正常行为<select>。您只需要在代码中添加一些小东西即可使其以不同的方式工作。

\n\n
\n

更改是
\n \xe2\x80\x94 首先,当外部文本文件中的值数组可用时,我将其存储在 WordPress 选项中。
\n \xe2\x80\x94 其次,在最后一个保存函数中,我获取存储的数组,并使用key从中获取的选择$_POST[\'_select_breed_key1\'];,检索存储在新条目中的相应值(wp_postmeta 表中的新行)。

\n
\n\n
//Create the fields\nfunction woo_add_custom_admin_fields() {    \n    global $woocommerce, $post;\n\n    echo \'<div id="custom_tab_data" class="panel woocommerce_options_panel">\';\n    echo \'<div class="options_group">\';\n\n    // Select - Breed1\n    if (file_exists ( plugin_dir_path(__FILE__) .\'breed.txt\')) {\n        $breedData = file_get_contents ( plugin_dir_path(__FILE__) .\'breed.txt\');\n        $breedArray = explode ("\\n", $breedData);\n\n        //Storing the array in wp_options table\n        if( get_option( \'wc_product_add_info_tab\' ) )\n            update_option( \'wc_product_add_info_tab\', $breedArray );\n        else\n            add_option( \'wc_product_add_info_tab\', $breedArray );\n    }\n\n    woocommerce_wp_select( array(\n        \'id\'      => \'_select_breed_key1\',\n        \'label\'   => __( \'Select Primary Breed\', \'woocommerce\' ),\n        \'desc_tip\'    => \'true\',\n        \'description\' => __( \'Select the primary breed of the pet.\', \'woocommerce\' ),\n        \'options\' => $breedArray\n    ) );\n\n    echo \'</div>\';\n    echo \'</div>\';\n}\nadd_action( \'woocommerce_product_write_panels\', \'woo_add_custom_admin_fields\' );\n\n\n// Save Created Fields;\nfunction woo_add_custom_general_fields_save( $post_id ){\n\n    // Select Field - Breed\n    $wc_select = $_POST[\'_select_breed_key1\'];\n    if( !empty( $wc_select ) )\n        update_post_meta( $post_id, \'_select_breed_key1\', esc_attr( $wc_select ) );\n\n    // Saving the corresponding value (from "$wc_select" selected key) to database\n    if(get_option(\'wc_product_add_info_tab\')) {\n\n        // Getting the array\n        $breed_arr = get_option(\'wc_product_add_info_tab\');\n\n        // Saving the corresponding value\n        update_post_meta( $post_id, \'_select_breed_value1\', $breed_arr[$wc_select] );\n    }\n}\nadd_action( \'woocommerce_process_product_meta\', \'woo_add_custom_general_fields_save\' );\n
Run Code Online (Sandbox Code Playgroud)\n\n
\n

您现在在wp_postmeta产品 ID (post_id) 表中拥有 2 个元键:
\n -\'_select_breed_key1\'存储选定的键
\n -\'_select_breed_value1\'存储相应的值

\n
\n\n

用法例如(获取该值):

\n\n
<?php\n\n// Third parameter is set to "true" as it is a string (Not an array) \n$breed_value1 = get_post_meta( $post_id, \'_select_breed_value1\', true );\necho $breed_value1;\n\n?>\n
Run Code Online (Sandbox Code Playgroud)\n