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
本文标签:
版权声明:本文标题:custom post types - Pre defined checkboxradio button state based on value stored in ACF fields in checkout form WooCommerce 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736299602a1930514.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论