woocommerce_wp_select options array from product attribute terms
Asked Answered
P

2

6

I am trying to create a drop down list box in woocommerce but have it populated with data from the database.

I have the majority of the code working but the portion to populate the list box is killing me. This is what I have so far.

add_action( 'woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields' ); // Display Extra Fields on General Tab Section


add_action( 'woocommerce_process_product_meta', 'woo_add_custom_general_fields_save' ); // Save Fields

function woo_add_custom_general_fields() {
    global $woocommerce, $post, $product;
    echo '<div class="options_group">';

    // Select

    $options = array(
        'hide_empty' => false,
        'order' => 'ASC',
        'fields' => 'names'
        );
    $DogBreeds = get_terms('pa_breed', $options);

    foreach ($DogBreeds as $key => $value) {
        $theArray = "'{$value}'  => __( '{$value}' , 'woocommerce' ), ";
    }

    woocommerce_wp_select( 
        array( 
            'id'      => '_select', 
            'label'   => __( 'My Select Field', 'woocommerce' ),
            'options' =>  $theArray //this is where I am having trouble
            )
        );

        echo $theArray;
        echo '<pre>';
        var_dump($DogBreeds);
        echo '</pre>';

    echo '</div>';
}

// Save Fields
    function woo_add_custom_general_fields_save( $post_id ){

    // Select
        $woocommerce_select = $_POST['_select'];
        if( !empty( $woocommerce_select ) )
            update_post_meta( $post_id, '_select', esc_attr( $woocommerce_select ) );
        else {
            update_post_meta( $post_id, '_select',  Null );
        }
    }

This should pull information from the "ATTRIBUTES" section in WooCommerce. I created a Breed Attribute and put a few dog breeds in there.

Any direction is greatly appreciated!

I know the 'options' section on the array is totally wrong but I put it there so you would know what I am trying to accomplish.

Peltier answered 28/8, 2017 at 2:43 Comment(0)
M
9

I have revisited your code a bit. The main problem was in the select <option> array.

You will see in the code the changes:

// Display Extra Fields on General Tab Section
add_action( 'woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields' );
function woo_add_custom_general_fields() {
    global $post;

    // Set HERE the product attribute taxonomy
    $taxonomy = 'pa_breed';

    // Get the selected value  <== <== (updated)
    $value = get_post_meta( $post->ID, '_select', true );
    if( empty( $value ) ) $value = '';

    $dog_breeds = get_terms( $taxonomy, array(
        'hide_empty' => false,
        'order' => 'ASC',
        'fields' => 'names'
    ) );

    $options[''] = __( 'Select a value', 'woocommerce'); // default value

    foreach ($dog_breeds as $key => $term)
        $options[$term] = $term; //  <===  <===  <===  Here the correct array

    echo '<div class="options_group">';

    woocommerce_wp_select( array(
        'id'      => '_select',
        'label'   => __( 'My Select Field', 'woocommerce' ),
        'options' =>  $options, //this is where I am having trouble
        'value'   => $value,
    ) );

    echo '</div>';
}

// Save Fields
add_action( 'woocommerce_process_product_meta', 'woo_add_custom_general_fields_save' );
function woo_add_custom_general_fields_save( $post_id ){

// Select
    $woocommerce_select = $_POST['_select'];
    if( !empty( $woocommerce_select ) )
        update_post_meta( $post_id, '_select', esc_attr( $woocommerce_select ) );
    else {
        update_post_meta( $post_id, '_select',  '' );
    }
}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

Tested in WooCommerce 3+ and works. You will get something similar to this (with your breeds):

enter image description here

Minni answered 28/8, 2017 at 3:46 Comment(6)
Thanks Loic!!!! It Works! :) Question...what is the reason for the $value array? This code didnt work for me until I removed the $value array.Peltier
I copied the code and pasted and it wouldn't save to the database. The lines: $value = update_post_meta( $post->ID, '_select', true ); if( empty( $value ) ) $value = ''; and 'value' => $value, had to be removed from the code in order for it to post to the database. otherwise it works perfectly. if those lines needed to be there for something I'm not getting it.Peltier
@Peltier Code updated: There was a little mistake of my part… I have corrected the problem… now it's working (Replaced update_post_meta() by get_post_meta())Minni
Awesome, it works perfectly!!! The thing I didn't know was that the SELECT field had the 'value' option, I had never used that before. 'value' => '', // if empty, retrieved from post meta where id is the meta_key So that part was new to me. Thanks again for your help, all the little adoptable pets of C2CND.org will benefit from your assistance! :)Peltier
@Minni any reason for global $post; instead of global $product; ?Graecize
@Graecize $product is not defined in single admin product pages.Minni
C
1

This will work for sure:

woocommerce_wp_select(array(
    'id' => '_select',
    'label' => __('My Select Field', 'woocommerce'),
    'options' => array(
        'red' => 'Red',
        'blue' => 'Blue'
    ),
));
Chromic answered 25/9, 2021 at 12:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.