Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WooCommerce - Retrieving the correct data value for select box after submitting

I am using Woocommerce and I have built a select box in the Admin Panel. I populate the information in the select box via a flat file. Everything works fine (almost).

The part I am stuck on is after I select the "choice" I want and the save I am getting the array $key position and not the actual $value. I'm close but I just can't put my finger on it.

Update: Here is my full code:

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 file contains 3 lines (items):

Please Select a breed...
Abyssinian
Affenpinscher

And the generated array looks like this:

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

So when I select "Affenpinscher" for example, I get as value "2" instead of "Affenpinscher" .

What I am doing wrong? How can I solve this issue?

Thanks

like image 832
JoeDostie Avatar asked Nov 08 '22 09:11

JoeDostie


1 Answers

— Updated — (tested and working)

This is absolutely a normal behavior for a drop down selector <select>. You just need to add some little things in your code to make it work in a different way.

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

Usage for example (to get this 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;

?>
like image 196
LoicTheAztec Avatar answered Nov 15 '22 03:11

LoicTheAztec