Auto-fill with select Woocommerce checkout

Asked

Viewed 41 times

1

I have the following code to generate a select and bring values within options:

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

$results = $wpdb->get_results( "SELECT * FROM tab_clientes" );
$options  = array( '' => __('Selecione o cliente') );

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

}

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

woocommerce_form_field( 'nome', array( 
    'type' => 'text', 
    'class' => array('nome form-row-wide'), 
    'label' => __('Razão Social'), 
    'default'   =>  '',

), $checkout->get_value( 'nome' ) );
woocommerce_form_field( 'cnpj', array( 
    'type' => 'text', 
    'class' => array('cnpj form-row-wide'), 
    'label' => __('CNPJ'), 
    'default'   =>  '',


), $checkout->get_value( 'cnpj' ) );


echo '</div>';

with the following script:

       <script>
        $(document).ready(function()
        {
            $('#cliente').change(function() {
                $('#nome').val( $( this ).val() );
            });
            $('#nome').change(function() {
                $('#cnpj').val( $( this ).val() );
            });
        });
        </script>

When I select the client, the #name (social reason - in the table = social razao_social) field appears with the correct value, but the value repeats inside CNPJ.

What I’m doing wrong?

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.