I want to make a WooCommerce Checkout Field that only shows up when a specific payment (Cash On Delivery) is chosen. This field should only show up and only be required when cash on delivery is chosen. I have managed to make the field and have it show up conditionally, but I cannot figure out how to make it required only when COD is chosen.
/* Only show EAN when COD chosen */
// Conditional Show hide checkout fields based on chosen payment methods
add_action( 'wp_footer', 'conditionally_show_hide_billing_custom_field' );
function conditionally_show_hide_billing_custom_field(){
// Only on checkout page
if ( is_checkout() && ! is_wc_endpoint_url() ) :
?>
<script>
jQuery(function($){
var a = 'input[name="payment_method"]',
b = a + ':checked',
c = '#billing_options_field'; // The checkout field <p> container selector
// Function that shows or hide checkout fields
function showHide( selector = '', action = 'show' ){
if( action == 'show' )
$(selector).show( 200, function(){
$(this).addClass("validate-required");
});
else
$(selector).hide( 200, function(){
$(this).removeClass("validate-required");
});
$(selector).removeClass("woocommerce-validated");
$(selector).removeClass("woocommerce-invalid woocommerce-invalid-required-field");
}
// Initialising: Hide if choosen payment method is "cod"
if( $(b).val() !== 'cod' )
showHide( c, 'hide' );
else
showHide( c );
// Live event (When payment method is changed): Show or Hide based on "cod"
$( 'form.checkout' ).on( 'change', a, function() {
if( $(b).val() !== 'cod' )
showHide( c, 'hide' );
else
showHide( c );
});
});
</script>
<?php
endif;
}
/* Add extra field and make it required */
add_filter('woocommerce_billing_fields', 'custom_woocommerce_billing_fields');
function custom_woocommerce_billing_fields($fields)
{
$fields['billing_options'] = array(
'label' => __('– EAN-nummer – ', 'woocommerce'), // Add custom field label
'placeholder' => _x('EAN nummer', 'placeholder', 'woocommerce'), // Add custom field placeholder
'required' => true, // if field is required or not
'clear' => false, // add clear or not
'type' => 'text', // add field type
'class' => array('my-css') // add class name
);
return $fields;
}
2
Answers
Remove the validation error from the error object if the payment method selected is not COD. The error object will be like the below.
It’s not advised to parse out the woocomerce error post-validation, it’s a much better idea to Handle The Validation Correctly In the First Place
To conditionally set a field as required at the checkout it’s much better to use the
woocommerce_checkout_fields
hook, so we can make any changes prior to the form being processed, this function allows you to programmatically access and set the values of each checkout field, and can be copy/pasted into your functions.php file to achieve what you are looking for.Additionally here are a few other tips:
It’s always a good idea to validate any form data, especially checkout data to ensure it’s valid, after a quick google, if by EAN number you are referring to This I have made an additional function for you to validate that the field is 13 numbers long, if you would like to use it just place it in your functions file as well
It’s also a good idea not to put too much javascript into your php files, so I would recommend using the
wp_enqueue_scripts
hook to conditionally load your javascript files as-neededyou can replace the entire
wp-footer
block with thisand create a file called eancheckout.js in your theme/assets/js folder, such as
So if you would like to impliment all of these, just make that js file and this is the complete functions.php file result for this