Support » Developing with WordPress » WordPress, woocommerce checkout

  • Resolved alicee23

    (@alicee23)


    I’m adding on checkout page custom fields – checkbox and on click show 2 fields for invoice – VAT Company name and VAT number. I would like to ask you for assistance with reviewing code. Code goes to functions.php and works properly.

    <?php
    //Add VAT Fields - Checkout
    add_action('woocommerce_after_order_notes', 'checkout_my_custom_checkbox', 61);
    function checkout_my_custom_checkbox( $checkout ) {
     
        echo '<div id="my-new-field"><h3>'.__('VAT invoice details: ').'</h3>';
     
        woocommerce_form_field( 'my_checkbox', array(
            'type'          => 'checkbox',
            'class'         => array('input-checkbox'),
            'label'         => __('Issue VAT invoice'),
            ), $checkout->get_value( 'my_checkbox' ));
     
        echo '</div>';
    }
    //Add VAT invoice details - Checkout
    add_action( 'woocommerce_after_order_notes', 'checkout_vat_fields', 62 );
    function checkout_vat_fields( $checkout ) {
    
        echo '<div id="checkout_vat_fields">';
        
        woocommerce_form_field( 'company_name', array(
            'type'          => 'text',
            'class'         => array( 'company-name-field form-row-wide') ,
            'label'         => __( 'Company name' ),
            'placeholder'   => __( 'Full name of the company' ),
            'required'      => true,
        ), $checkout->get_value( 'company_name' ));
        
        woocommerce_form_field( 'vat_number', array(
            'type'          => 'text',
            'class'         => array( 'vat-number-field form-row-wide') ,
            'label'         => __( 'VAT Number' ),
            'placeholder'   => __( 'Enter VAT number' ),
            'required'      => true,
        ), $checkout->get_value( 'vat_number' ));
        
        echo '</div>';
    }
    
    //Save checkbox value in the order meta
    add_action('woocommerce_checkout_update_order_meta', 'checkout_my_custom_checkbox_update_order_meta');
     
    function checkout_my_custom_checkbox_update_order_meta( $order_id ) {
        if ($_POST['my_checkbox']) update_post_meta( $order_id, '_my_checkbox', esc_attr($_POST['my_checkbox']));
    }
    
    //Save VAT details in the order meta
    add_action( 'woocommerce_checkout_update_order_meta', 'vat_details_update_order_meta' );
    function vat_details_update_order_meta( $order_id ) {
        
        if ( ! empty( $_POST['company_name'] ) ) {
            update_post_meta( $order_id, '_company_name', sanitize_text_field( $_POST['company_name'] ) );
        }
        if ( ! empty( $_POST['vat_number'] ) ) {
            update_post_meta( $order_id, '_vat_number', sanitize_text_field( $_POST['vat_number'] ) );
        }
    }
    
    // Display VAT invoice info - admin order
    add_action( 'woocommerce_admin_order_data_after_billing_address', 'vat_checkbox_display_admin_order_meta', 10, 1 );
    function vat_checkbox_display_admin_order_meta( $order ) {
     echo '<strong>' . __( 'VAT invoice', 'woocommerce' ) . ':</strong> ';
    if (get_post_meta( $order->id, '_my_checkbox', true ) =='1')  { 
         echo 'Yes';
      } 
      else { 
          echo 'No';
      }
    }
    
    // Display VAT invoice details - admin order
    add_action( 'woocommerce_admin_order_data_after_billing_address', 'vat_number_display_admin_order_meta', 10, 1 );
    function vat_number_display_admin_order_meta( $order ) {
        echo '<p><strong>' . __( 'Company name', 'woocommerce' ) . ':</strong> ' . get_post_meta( $order->id, '_company_name', true ) . '</p>';
        echo '<p><strong>' . __( 'VAT number', 'woocommerce' ) . ':</strong> ' . get_post_meta( $order->id, '_vat_number', true ) . '</p>';
    }
    
    //Show hide VAT fields
    add_action( 'woocommerce_after_checkout_form', 'hide_show_vat_invoice', 999 );
    function hide_show_vat_invoice() {
        
      wc_enqueue_js( "jQuery('input#my_checkbox').change(function(){
               if (! this.checked) {
                // hide not checked
                jQuery('#checkout_vat_fields').hide();     
             } else {
                // show checked
                jQuery('#checkout_vat_fields').show();
             }
               
          }).change();");       
    }
Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    I don’t see anything alarming on a quick read through. It all looks very well done. Nice work!

    Did you test with WP_DEBUG defined as true, or at least check the error log for any notices or warnings thrown? That can catch things I’d miss on a read through. I don’t expect anything, but ya never know…

    Thread Starter alicee23

    (@alicee23)

    Thank you for your nice words and help. Checked debugging and 0 errors / warning.

    • This reply was modified 1 year, 8 months ago by alicee23.
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘WordPress, woocommerce checkout’ is closed to new replies.