0

I have a code for woocommerce checkout fields. My code is okay but I want the checkbox to appear before all billing detail fields. I tried to change woocommerce hooks but it doesn't change anything. I'm confused where should I change the hook so the checkbox goes before fields. I took hooks from here https://woocommerce.github.io/code-reference/files/woocommerce-templates-checkout-form-checkout.html#source-view.36

add_action( 'woocommerce_checkout_before_customer_details', 'conditionally_show_hide_checkout_field' );
function conditionally_show_hide_checkout_field() {
    ?>
    <style>
    p#billing_company_field.on { display:none !important; }
    p#billing_company_code_field.on { display:none !important; }
    p#billing_company_vat_code_field.on { display:none !important; }
    </style>

    <script type="text/javascript">
    jQuery(function($){
        var a = 'input#checkbox_trigger',   b = '#billing_company_field, #billing_company_code_field, #billing_company_vat_code_field'

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

add_filter( 'woocommerce_checkout_fields', 'add_custom_checkout_fields' );
function add_custom_checkout_fields( $fields ) {

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

    $fields['billing']['billing_company'] = array(
        'label'     => __('Įmonės pavadinimas', 'woocommerce'),
        'placeholder'   => _x('Įmonės pavadinimas', 'placeholder', 'woocommerce'),
        'class'     => array('form-row-wide on'),
        'clear'     => true
    );
        $fields['billing']['billing_company_code'] = array(
        'label'     => __('Įmonės kodas / Ind. veiklos nr. ', 'woocommerce'),
        'placeholder'   => _x('Įmonės kodas / Ind. veiklos nr. ', 'placeholder', 'woocommerce'),
        'class'     => array('form-row-wide on'),
        'clear'     => true
    );
            $fields['billing']['billing_company_vat_code'] = array(
        'label'     => __('Įmonės kodas / Ind. veiklos nr.', 'woocommerce'),
        'placeholder'   => _x('Įmonės kodas / Ind. veiklos nr.', 'placeholder', 'woocommerce'),
        'class'     => array('form-row-wide on'),
        'clear'     => true
    );

    return $fields;
}
Tomas
  • 1
  • 1
  • After you push new fields to the array, you can set their priority as mentioned here https://stackoverflow.com/questions/45200360/reorder-woocommerce-checkout-fields – Rakesh Mehta Sep 22 '22 at 18:31
  • Thanks, it's working for data input fields, but doesn't work for checkbox – Tomas Sep 22 '22 at 19:06

0 Answers0