woocommerce_wp_select 来自产品属性术语的选项数组

woocommerce_wp_select options array from product attribute terms

我正在尝试在 woocommerce 中创建一个下拉列表框,但它填充了数据库中的数据。

我的大部分代码都可以正常工作,但是填充列表框的部分让我很难受。这是我目前所拥有的。

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 );
        }
    }

这应该从 WooCommerce 的 "ATTRIBUTES" 部分提取信息。我创建了一个 Breed 属性并在其中放置了一些犬种。

非常感谢任何方向!

我知道数组中的 'options' 部分是完全错误的,但我把它放在那里是为了让您知道我想要完成什么。

我重新审视了您的代码。主要问题出在 select <option> 数组中。

您将在代码中看到更改:

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

代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。

在 WooCommerce 3+ 中测试并有效。你会得到类似的东西 (与你的品种):

这肯定有效:

woocommerce_wp_select(array(
    'id' => '_select',
    'label' => __('My Select Field', 'woocommerce'),
    'options' => array(
        'red' => 'Red',
        'blue' => 'Blue'
    ),
));