如何将所有woocommerce产品作为下拉选择列表

I'm creating a WooCommerce plugin setting page but I failed to get the product title as dropdown list, also the data is not saved anymore (I have tried to use an input field before).

Here is my code:

public function exclutips_create_license_page() {
        // Set class property
        $this->options = get_option('activation-codes-settings');
        ?>
        <div class="wrap">
            <?php screen_icon(); ?>
            <h2>Woocommerce License Manager Settings Page</h2>   
            <form method="post" action="options.php">
                <?php
                // This prints out all hidden setting fields
                settings_fields('exclutips_option_group');
                do_settings_sections('activation-codes-settings-admin');
                submit_button();
                ?>
            </form>
        </div>
        <?php
    }

    /**
     * Register and add settings
     */
    public function exclutips_page_init() {
        register_setting(
                'exclutips_option_group', // Option group
                'activation-codes-settings', // Option name
                array($this, 'sanitize') // Sanitize
        );

        add_settings_section(
                'setting_section_id', // ID
                'Register Product Settings', // Title
                array($this, 'print_section_info'), // Callback
                'activation-codes-settings-admin' // Page
        );

        add_settings_field(
                'ex_product_ids', 'Product Id For License System', array($this, 'ex_product_ids_callback'), 'activation-codes-settings-admin', 'setting_section_id'
        );
    }

    /**
     * Sanitize each setting field as needed
     */
    public function sanitize($input) {
        $new_input = array();
        if (isset($input['ex_product_ids']))
            $new_input['ex_product_ids'] = absint($input['ex_product_ids']);

        return $new_input;
    }

    /**
     * Print the Section text
    */
    public function print_section_info() {
        print 'Please select the product ID for License Code:';
    }

    /**
     * Get the settings option array and print one of its values
    */
    public function ex_product_ids_callback() {
        ?>
        <select name="ex_product_ids"> 
            <option selected="selected" disabled="disabled" value=""><?php echo esc_attr( __( 'Select a Product' ) ); ?></option> 
            <?php
            global $woocommerce;
                $selected_product = get_option( 'ex_product_ids' );
                $products = wc_get_product(); 
                foreach ( $products as $product ) {
                    $option = '<option value="' . $product->id . '" ';
                    $option .= ( $product->id == $selected_product ) ? 'selected="selected"' : '';
                    $option .= '>';
                    $option .= $product->the_title();
                    $option .= '</option>';
                    echo $option;
                }
            ?>
        </select>  

      <?php 
    }

To get a dropdown of any page (cpt, page...), you have the wp_dropdown_pages() WordPress function (more info from the codex).

In your case, adjust $args to get the select dropdown of your dreams, in the ex_product_ids_callback function:

public function ex_product_ids_callback(){
   $option = get_option('activation-codes-settings');

 $args = array(
  'sort_order'   => 'ASC',
  'sort_column'  => 'post_title',
  'hierarchical' => 1,
  'selected' => $option['ex_product_ids'],
  'name' => 'activation-codes-settings[ex_product_ids]',
  'post_type' => 'product'
 );

 wp_dropdown_pages($args);

}

You'll see that you can exclude ids from the dropdown,this is what you are expecting to do. If you want to select more than 1 item, I advice you to use checkbox instead of dropdown, but it depends on the number of product you have.

Hope it helps !