mirror of
https://gh.wpcy.net/https://github.com/mainwp/Code-Snippets-Functions.git
synced 2026-04-30 11:42:22 +08:00
https://stackoverflow.com/questions/66140229/add-a-checkbox-that-show-hide-order-notes-on-woocommerce-checkout/66162649
41 lines
1.3 KiB
Text
41 lines
1.3 KiB
Text
add_filter( 'woocommerce_checkout_fields', 'custom_checkout_order_notes' );
|
|
function custom_checkout_order_notes( $fields ) {
|
|
$fields['order']['order_comments']['class'][] = 'off';
|
|
return $fields;
|
|
}
|
|
|
|
add_action( 'woocommerce_before_order_notes', 'checkout_checkbox_show_hide_order_notes' );
|
|
function checkout_checkbox_show_hide_order_notes( $fields ) {
|
|
$target_id = 'order_comments';
|
|
?>
|
|
<style> p#<?php echo $target_id; ?>_field.off { display:none;}</style>
|
|
<script type="text/javascript">
|
|
jQuery(function($){
|
|
var a = 'input#checkbox_trigger', b = '#<?php echo $target_id; ?>_field';
|
|
|
|
$(b).hide('fast');
|
|
|
|
$(a).change(function(){
|
|
if( $(b).hasClass('off') ) {
|
|
$(b).removeClass('off');
|
|
}
|
|
|
|
if ( $(this).prop('checked') ) {
|
|
$(b).show('fast');
|
|
} else {
|
|
$(b).hide('fast', function(){
|
|
$(b+' input').val('');
|
|
});
|
|
}
|
|
});
|
|
});
|
|
</script>
|
|
<?php
|
|
|
|
woocommerce_form_field( 'checkbox_trigger', array(
|
|
'type' => 'checkbox',
|
|
'label' => __('Add a note to your order?', 'woocommerce'),
|
|
'class' => array('form-row-wide'),
|
|
'clear' => true,
|
|
), false );
|
|
}
|