dongzhang5006 2016-10-17 18:56
浏览 24

如何将所有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 
    }
  • 写回答

1条回答 默认 最新

  • duan201444 2016-10-18 08:18
    关注

    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 !

    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c