admin管理员组

文章数量:1122846

I have created custom fields using acf and created a form to get inputs stored in this fields as user meta, now i used those fields value and displayed on checkout form as custom checkout fields all fields value are displayed correctly only issue i am facing with checkbox field type where passed value is not showing it in checked or unchecked state, i tried to change the field type to radio also same issue based on value it should be in selected state but its in not selected state

ACF value is stored in ndis_authority_to_leave_shipment

Radio button custom field

// Radio buttons for Authority to Leave Shipment
        $ndis_authority_to_leave_shipment = get_user_meta($user->ID, 'ndis_authority_to_leave_shipment', true);

 woocommerce_form_field('ndis_authority_to_leave_shipment', array(
            'type' => 'radio',
            'class' => array('ndis-authority-to-leave-shipment form-row-wide'),
            'label' => __('Authority to Leave Shipment (Yes/No)'),
            'options' => array(
                'yes' => 'Yes',
                'no' => 'No',
            ),
            'default' => $ndis_authority_to_leave_shipment,
        ), $ndis_authority_to_leave_shipment);

Jquery to handle value

add_action('wp_footer', 'ndis_custom_checkout_fields_jquery');
function ndis_custom_checkout_fields_jquery() {
    if (is_checkout()) {
        ?>
        <script type="text/javascript">
            jQuery(document).ready(function($) {
                var $radioYes = $('input[name="ndis_authority_to_leave_shipment"][value="yes"]');
                var $radioNo = $('input[name="ndis_authority_to_leave_shipment"][value="no"]');
                var storedValue = '<?php echo get_user_meta(get_current_user_id(), 'ndis_authority_to_leave_shipment', true); ?>';

                // Set initial state
                if (storedValue === 'yes') {
                    $radioYes.prop('checked', true);
                } else if (storedValue === 'no') {
                    $radioNo.prop('checked', true);
                }

                // Update value on change
                $('input[name="ndis_authority_to_leave_shipment"]').on('change', function() {
                    if ($radioYes.is(':checked')) {
                        $radioYes.val('yes');
                        $radioNo.val('no');
                    } else if ($radioNo.is(':checked')) {
                        $radioNo.val('no');
                        $radioYes.val('yes');
                    }
                });
            });
        </script>
        <?php
    }
}


Checkbox custom field

 // Checkbox field
        $ndis_authority_to_leave_shipment = get_user_meta($user->ID, 'ndis_authority_to_leave_shipment', true);

        woocommerce_form_field('ndis_authority_to_leave_shipment', array(
            'type' => 'checkbox',
            'class' => array('ndis-authority-to-leave-shipment form-row-wide'),
            'label' => __('Authority to Leave Shipment (Yes/No)'),
            'default' => $ndis_authority_to_leave_shipment === 'yes' ? '1' : '0',
        ), $ndis_authority_to_leave_shipment === 'yes' ? '1' : '0');

jQuery to handle checkbox value

add_action('wp_footer', 'ndis_custom_checkout_fields_jquery');
function ndis_custom_checkout_fields_jquery() {
    if (is_checkout()) {
        ?>
        <script type="text/javascript">
            jQuery(document).ready(function($) {
                var $checkbox = $('#ndis_authority_to_leave_shipment');

                // Set initial state
                if ($checkbox.val() === '1') {
                    $checkbox.prop('checked', true);
                } else {
                    $checkbox.prop('checked', false);
                }

                // Update value on change
                $checkbox.on('change', function() {
                    if ($(this).is(':checked')) {
                        $(this).val('1');
                    } else {
                        $(this).val('0');
                    }
                });

                // Trigger change to set initial value
                $checkbox.trigger('change');
            });
        </script>
        <?php
    }
}

And when i am using as checkbox i get checked status when value is 'yes' but when i change the value to 'no' then it is not changing to uncheck state.

Where i am doing wrong please let me know

本文标签: