0

I found code on Stackoverflow that adds 3 custom fields to the Checkout page in Woocommerce, everything works as expected, but I want them to also show up in the My Account>Edit Address form, but with other labels. My code:

function action_woocommerce_before_checkout_form() {
    ?>
    <style>
    p#new_billing_field_field.on { display:none !important; }
    p#new_billing_field2_field.on { display:none !important; }
    p#new_billing_field3_field.on { display:none !important; }
    </style>

<script type="text/javascript">
    jQuery(function($){
        var a = 'input#checkbox_trigger';
        var b = '#new_billing_field_field,#new_billing_field2_field,#new_billing_field3_field';

        $(a).change(function() {
            if ( $(this).prop('checked') === true && $(b).hasClass('on') ) {
                $(b).slideDown( "1000", function(){
                    $(b).css({'display':'none'}).removeClass('on').slideDown( "1000" );
                });
            }
            else if ( $(this).prop('checked') !== true && ! $(b).hasClass('on') ) {
                $(b).slideUp(function(){
                    $(b).addClass('on')
                });
                $(b+' input').val('');
            }
        });
    });
    </script>

<?php
}
add_action( 'woocommerce_before_checkout_billing_form', 'action_woocommerce_before_checkout_form' );

function filter_woocommerce_checkout_fields( $fields ) {
    $fields['billing']['checkbox_trigger'] = array(
        'type'          => 'checkbox',
        'label'         => __( 'Perka įmonė?', 'woocommerce' ),
        'class'         => array( 'form-row-wide' ),
        'clear'         => true
    );

    $fields['billing']['new_billing_field'] = array(
        'type'          => 'text',
        'label'         => __( 'Įmonės pavadinimas', 'woocommerce' ),
        'class'         => array( 'form-row-wide on' ),
        'clear'         => true
    );
    
    $fields['billing']['new_billing_field2'] = array(
        'type'          => 'text',
        'label'         => __( 'Įmonės kodas ', 'woocommerce' ),
        'class'         => array( 'form-row-wide on' ),
        'clear'         => true
    );
      $fields['billing']['new_billing_field3'] = array(
         'type'         => 'text',
        'label'         => __( 'Įmonės PVM kodas', 'woocommerce' ),
        'class'         => array( 'form-row-wide on' ),
        'clear'         => true
    );
    
    return $fields;
}

add_filter( 'woocommerce_checkout_fields', 'filter_woocommerce_checkout_fields', 10, 1 );

// Save fields
function action_woocommerce_checkout_create_order( $order, $data ) {
    // Isset and NOT empty, save
    if ( isset( $_POST['new_billing_field'] ) && ! empty( $_POST['new_billing_field'] ) ) {
        // Update meta data
        $order->update_meta_data( '_new_billing_field', sanitize_text_field( $_POST['new_billing_field'] ) );
    }
    
    if ( isset( $_POST['new_billing_field2'] ) && ! empty( $_POST['new_billing_field2'] ) ) {
        // Update meta data
        $order->update_meta_data( '_new_billing_field2', sanitize_text_field( $_POST['new_billing_field2'] ) );
    }
    
    if ( isset( $_POST['new_billing_field3'] ) && ! empty( $_POST['new_billing_field3'] ) ) {
        // Update meta data
        $order->update_meta_data( '_new_billing_field3', sanitize_text_field( $_POST['new_billing_field3'] ) );
    }
}
add_action( 'woocommerce_checkout_create_order', 'action_woocommerce_checkout_create_order', 10, 2 );

// Display field values on admin order pages after billing adress
function action_woocommerce_admin_order_data_after_billing_address( $order ) {  
    // Get meta
    $new_billing_field = $order->get_meta( '_new_billing_field' );
    
    // NOT empty
    if ( ! empty ( $new_billing_field ) ) {
        echo '<p><strong>' . __( 'Įmonės pavadinimas', 'woocommerce' ) . ':</strong> ' . $new_billing_field . '</p>';
    }
    
    // Get meta
    $new_billing_field2 = $order->get_meta( '_new_billing_field2' );
    
    // NOT empty
    if ( ! empty ( $new_billing_field2 ) ) {
        echo '<p><strong>' . __( 'Įmonės kodas', 'woocommerce' ) . ':</strong> ' . $new_billing_field2 . '</p>';
    }
    
    // Get meta
    $new_billing_field3 = $order->get_meta( '_new_billing_field3' );
    
    // NOT empty
    if ( ! empty ( $new_billing_field3 ) ) {
        echo '<p><strong>' . __( 'Įmonės PVM kodas', 'woocommerce' ) . ':</strong> ' . $new_billing_field3 . '</p>';
    }
}
add_action( 'woocommerce_admin_order_data_after_billing_address', 'action_woocommerce_admin_order_data_after_billing_address', 10, 1 );

But I don't know how I can add these 3 custom fields to My Account>Edit Address form while leaving them on the Checkout page.

Any guidance is much appreciated!

0 Answers0