简体   繁体   English

从WooCommerce结帐中的自定义数据库表中获取选择字段选项

[英]Get select field options from a custom DB table in WooCommerce checkout

Within the wordpress database I have a table called tab_clients In this table I have the following columns: id, name, cpf, cnpj, ie, razao_social, email, phone, mobile, representative (wordpress user id), status, photo, date_since, data_cadastro 在wordpress数据库中,我有一个名为tab_clients的表。在此表中,我有以下几列:id,name,cpf,cnpj,即razao_social,电子邮件,电话,手机,代表,(wordpress用户ID),状态,照片,date_since, data_cadastro

I need to bring these values into a select field in the checkout form of woocommerce, but when I declare the variable 'options' => array ($ client-> name), it only brings me a single result. 我需要将这些值带入woocommerce的结帐形式的选择字段中,但是当我声明变量'options'=> array($ client-> name)时,它只带给我一个结果。

add_action( 'woocommerce_after_order_notes', 'cliente_woocommerce' ); 
function cliente_woocommerce( $checkout ) { 
echo '<div id="cliente_woocommerce"><h2>' . __('Cliente') . '</h2>'; 
$clientes = $wpdb->get_results( "SELECT id, nome, cnpj FROM tab_clientes" );
foreach($clientes as $cliente);

woocommerce_form_field( 'cliente', array( 
    'type' => 'select', 
    'class' => array('cliente form-row-wide'), 
    'label' => __('Campo de Teste (Cliente)'), 
    'placeholder' => __('Selecione o cliente'), 
    'options'   =>  array($cliente->nome), 
), $checkout->get_value( 'cliente' ) );

echo '</div>';

Try the following revisited code that will give you the correct options array with multiple values for this checkout custom select field: 尝试以下重新访问的代码,该代码将为您为该签出自定义选择字段提供具有多个值的正确选项数组:

add_action( 'woocommerce_after_order_notes', 'cliente_woocommerce' ); 
function cliente_woocommerce( $checkout ) 
{ 
    global $wpdb;

    $results = $wpdb->get_results( "SELECT id, nome, cnpj FROM tab_clientes" );
    $otions  = array( '' => __('Selecione o cliente') );

    // Loop through the data query results
    foreach($results as $result) {
        $options[$result->id] = $result->nome;
    }

    echo '<div id="cliente_woocommerce"><h2>' . __('Cliente') . '</h2>';

    woocommerce_form_field( 'cliente', array( 
        'type' => 'select', 
        'class' => array('cliente form-row-wide'), 
        'label' => __('Campo de Teste (Cliente)'), 
        'options'   =>  $options, 
    ), $checkout->get_value( 'cliente' ) );

    echo '</div>';
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM