0

I am working on Woocommerce. On the checkout page, "Ship to a different address?" If the checkbox is checked, a new form opens. If the "Copy" checkbox is checked after this form is filled, it transfers data to the above invoice details form. I want this Copy checkbox to be visible after clicking "Ship to a different address?" I made a definition like the one below, but it doesn't work. Another problem is billing_city and shipping_city. Data is only transferred when the page is reloaded. So I have to refresh the page. Others can be transferred instantly. I wrote a trigger for this but this doesn't work (https://i.ibb.co/1fgD1By/a023046.png) . I've been dealing with this for a long time, I have reviewed many codes, but I could not do it. I would be glad if you could help.

function add_checkout_script() { ?>
    <script type="text/javascript">
   
    var $checkbox = $('#ship-to-different-address input');
    if ($checkbox.prop('checked')) {
            
        $("#copy_to_form").css('display', 'block');
    }
});
jQuery(document).on('change', '#copy_to_billing', function() {
        if(this.checked) {
            jQuery("[name='billing_first_name']").val(jQuery("[name='shipping_first_name']").val());
            jQuery("[name='billing_last_name']").val(jQuery("[name='shipping_last_name']").val());
            jQuery("[name='billing_address_1']").val(jQuery("[name='shipping_address_1']").val());
            jQuery("[name='billing_address_2']").val(jQuery("[name='shipping_address_2']").val());
            jQuery("[name='billing_city']").filter(":selected").val(jQuery("[name='shipping_city']").filter(":selected").val());
            jQuery("[name='billing_state']").val(jQuery("[name='shipping_state']").val());
            jQuery("[name='billing_phone']").val(jQuery("[name='shipping_phone']").val());
            jQuery("[name='billing_country']").val(jQuery("[name='shipping_country']").val());
        }
    });
           
    </script>
<form  id="copy_to_form" class="copy_to_form" style ="display:none">
<input id="copy_to_billing" type ="checkbox" name="copy_to_billing" class="transfer-to-billing" 
value ="1" class="copy_billing woocommerce-form__input woocommerce-form__input-checkbox input-checkbox"><span class="transfer-to-billing" ><?php esc_html_e( ' Copy', 'woocommerce' ); ?></span>
</form>
<?php       
}
add_action( 'woocommerce_after_checkout_form', 'add_checkout_script' );