【问题标题】:Populate woocommerce_wp_select field from database从数据库填充 woocommerce_wp_select 字段
【发布时间】:2018-11-28 18:00:11
【问题描述】:

请帮我解决这个问题..我尝试了太多不同的东西... 我正在尝试使用数据库中的数据填充 woocommerce 选择字段。

// Add fields & settings to the custom product tab
$SQL = "SELECT DISTINCT table_name FROM wp_lbc_prices";
$array = $wpdb->get_results( $SQL, ARRAY_A);


add_action( 'woocommerce_product_data_panels', 
'wcpt_roller_blind_options_product_tab_content' );

function wcpt_roller_blind_options_product_tab_content() {

?><div id='roller_blind_options' class='panel woocommerce_options_panel'><?php
    ?><div class='options_group'><?php
        woocommerce_wp_select( array(
                'id'          => 'roller_blind_tables',
                'label'       => __( 'Price Tables', 'wcpt' ),
                'placeholder' => '',
                'desc_tip'    => 'true',
                'description' => __( 'Select Associated Price Table.', 'wcpt' ),
                'options' => $array
           ));

    ?></div>
</div><?php

当然,对数据库的查询有效并且正在返回结果......但我不确定如何制作一个可接受的数组(我习惯了 asp.net,这似乎使这更简单!) .我返回的数据不需要 ID,因此下拉列表的值和文本可以相同。

【问题讨论】:

    标签: mysql wordpress select woocommerce


    【解决方案1】:
    1. 您需要首先确定table_name 或将其替换为要从数据库表wp_lbc_prices 中查询的正确列slug
    2. 您需要将WPDB 方法get_results() 替换为get_col(),该方法查询单个列并原生提供数组。
    3. 准备数组以使用array_combine() 将值复制到键

    我已经使用添加自定义产品选项卡的挂钩函数完成了您的代码。

    您重新访问的代码将是:

    add_filter( 'woocommerce_product_data_tabs', 'add_roller_blind_product_data_tab' );
    function add_roller_blind_product_data_tab( $tabs ) {
        $tabs['roller_blind'] = array(
            'label' => __( 'Roller blind', 'wcpt' ),
            'target' => 'roller_blind_options', // <== to be used in the <div> class of the content
            //'class' => array('show_if_simple'), // or 'hide_if_simple' or 'show_if_variable'…
        );
    
        return $tabs;
    }
    
    add_action( 'woocommerce_product_data_panels', 'display_roller_blind_product_data_tab_content' );
    function display_roller_blind_product_data_tab_content() {
        global $wpdb;
    
        echo '<div id="roller_blind_options" class="panel woocommerce_options_panel">
        <div class="options_group">';
    
        $sql_query = $wpdb->get_col( "SELECT DISTINCT table_name FROM {$wpdb->prefix}wp_lbc_prices" );
        $options   = sizeof($sql_query) > 0 ? array_combine( $sql_query, $sql_query ) : array();
    
        woocommerce_wp_select( array(
            'id'          => 'roller_blind_tables',
            'label'       => __( 'Price Tables', 'wcpt' ),
            'placeholder' => '',
            'desc_tip'    => 'true',
            'description' => __( 'Select Associated Price Table.', 'wcpt' ),
            'options'     => $options
        ));
    
        echo '</div></div>';
    }
    

    它现在应该可以更好地工作了。但由于这是一个自定义数据库表,因此无法测试。

    【讨论】:

    • 非常感谢!该代码运行良好,您还为我提供了一些额外的方法来研究和使用以供将来使用。也感谢您的解释。
    【解决方案2】:

    get_results 函数返回一个带有索引的数组,因此值可以像这样的对象数组 array([0]=&gt;object,[1]=&gt;object) 你能分享一下你在 $array 变量中得到了什么吗?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-06-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-25
      • 2013-07-03
      • 2016-12-22
      相关资源
      最近更新 更多