Display Woocommerce checkout custom field - checkbox - in admin order and admin email

Viewed 158

I've setup 4 fields in the woocommerce checkout form which are displaying correctly in the edit order page and customer panel, but I'm not able to display correctly the checkbox as a yes or no (results in a 1 or 0) and can't get those custom fields in the new order admin email. I've tried different codes to add the fields in the new order email, but all resulted in PHP errors or just didn't show the fields.

The code I've used is:

// Campi extra checkout

add_filter( 'woocommerce_billing_fields' , 'add_billing_field_extra' );

 function add_billing_field_extra( $fields ) {
 $new_order_fields = array();

 foreach($fields as $key => $value){

    if($key == 'billing_country') {
    
            $new_order_fields['billing_fattura'] = array(
            'type'      => 'checkbox',
            'label'     => __('Vuoi la fattura?', 'woocommerce'),
            'class'     => array('form-row-wide'),
            'clear'     => true
            );
            
            $new_order_fields['billing_piva'] = array(
            'label' => __('Partita Iva', 'woocommerce'),
            'placeholder' => _x('Partita iva', 'placeholder', 'woocommerce'),
            'required' => false,
            'class' => array('form-row-wide'),
            'show' => true
            );
            
            $new_order_fields['billing_cf'] = array(
            'label' => __('Codice Fiscale', 'woocommerce'),
            'placeholder' => _x('Codice Fiscale', 'placeholder', 'woocommerce'),
            'required' => false,
            'class' => array('form-row-wide'),
            'show' => true
            );
            
            $new_order_fields['billing_sdi'] = array(
            'label' => __('SDI', 'woocommerce'),
            'placeholder' => _x('SDI', 'placeholder', 'woocommerce'),
            'required' => false,
            'class' => array('form-row-wide'),
            'show' => true
            );
        }

     $new_order_fields[$key] = $value;
     }
     return $new_order_fields;
     }
     
     // Aggiungere il campo Piva AMMINISTRAZIONE BACKEND
        add_filter( 'woocommerce_admin_billing_fields' , 'add_admin_field_cfpiva' );

        function add_admin_field_cfpiva( $fields ) {
            $fields['fattura'] = array(
                'label' => __('Fattura', 'woocommerce'),
                'show'  => true
            );
        
            $fields['piva'] = array(
                'label' => __('P.IVA', 'woocommerce'),
                'show'  => true
            );
            $fields['cf'] = array(
                'label' => __('Codice Fiscale', 'woocommerce'),
                'show'  => true
            );
            $fields['sdi'] = array(
                'label' => __('SDI', 'woocommerce'),
                'show'  => true
            );
            return $fields;
        }
        
        function blindo_mostra_nascondi_campi_woocommerce() {
            
          wc_enqueue_js( "
              jQuery('input#billing_fattura').change(function(){
                   
                 if (! this.checked) {
                    // HIDE IF NOT CHECKED
                    jQuery('#billing_piva_field').fadeOut();
                    jQuery('#billing_piva_field input').val('');
                    
                    jQuery('#billing_cf_field').fadeOut();
                    jQuery('#billing_cf_field input').val('');
                    
                    jQuery('#billing_sdi_field').fadeOut();
                    jQuery('#billing_sdi_field input').val('');             
                 } else {
                    // SHOW IF CHECKED
                    jQuery('#billing_piva_field').fadeIn();
                    jQuery('#billing_cf_field').fadeIn();
                    jQuery('#billing_sdi_field').fadeIn();
                 }  
              }).change();
          ");      
        }   
0 Answers
Related