ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

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

2019-07-06 04:29:56  阅读:189  来源: 互联网

标签:wordpress php arrays 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' );

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

Please Select a breed...
Abyssinian
Affenpinscher

生成的数组如下所示:

Array ( 
    [0] => Please Select a breed... 
    [1] => Abyssinian 
    [2] => Affenpinscher 
)

因此,当我选择“Affenpinscher”时,我得到的值是“2”而不是“Affenpinscher”.

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

谢谢

解决方法:

– 更新 – (测试和工作)

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

The changes are:
— first, when the array of values from the external text file is available, I store it in wordpress options.
— Second, In the last saving function, I get the stored array and with the selected key that I get from $_POST['_select_breed_key1'];, I retrieve the corresponding value that I store in a new entry (new row in wp_postmeta table.

//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' );

You have now in in wp_postmeta table for a Product ID (post_id), 2 meta_keys:
'_select_breed_key1' that stores the selected key
'_select_breed_value1' that stores the corresponding value

用法例如(获取此值):

<?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;

?>

标签:wordpress,php,arrays,html-select,woocommerce
来源: https://codeday.me/bug/20190706/1393973.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有