By default, the Billing Details will be hidden using the first two blocks of code.
But if there is a missing field to be filled, I would like to check the checkbox automatically.
But the last code is not working.
/* Displays the checkbox to Show / Hide billing details */
function checkbox_exibir_detalhes_faturamento_f( $checkout ) {
echo ' <div id="div_checkbox_faturamento"> ';
woocommerce_form_field(
'checkbox_faturamento', array(
'type' => 'checkbox',
'label' => __('Exibir Detalhes de Faturamento', 'woocommerce'),
'class' => array('form-row-wide'),
'clear' => true
),
$checkout->get_value( 'checkbox_faturamento' )
);
echo ' </div> ';
}
add_action( 'woocommerce_before_checkout_billing_form', 'checkbox_exibir_detalhes_faturamento_f' );
/* Function that displays / hides billing details when clicking on the checkbox */
function conditionally_hide_show_new_field_f() {
wc_enqueue_js( "
jQuery('input#checkbox_faturamento').change(function(){
if (! this.checked) {
// Oculta se n?o marcado
jQuery('.woocommerce-billing-fields__field-wrapper').fadeOut();
} else {
// Exibe se marcado
jQuery('.woocommerce-billing-fields__field-wrapper').fadeIn();
}
}).change();
");
}
add_action( 'woocommerce_after_checkout_form', 'conditionally_hide_show_new_field_f', 9999 );
/* Viewing billing details in case of errors */
function ajustando_mensagens_erro_checkout_f( $fields, $errors ){
// Se houver erros de valida??o
if( !empty( $errors->get_error_codes() ) ) {
// Exibe detalhes de faturamento
wc_enqueue_js( "jQuery('input#checkbox_faturamento').prop('true');" );
return;
}
}
add_action( 'woocommerce_after_checkout_validation', 'ajustando_mensagens_erro_checkout_f', 9999, 2);