简体   繁体   中英

Make custom checkout required field created by plugin optional in WooCommerce

In woocommerce checkout page need to remove a specific custom field validation. This field is basically generated by a plugin using woocommerce_form_field().Is there any way to remove the validation for that specific field?The piece of code block

echo '<div id="woo_delivery_delivery_selection_field" style="display:none;">';
            woocommerce_form_field('woo_delivery_delivery_selection_box',
            [
                'type' => 'select',
                'class' => [
                    'woo_delivery_delivery_selection_box form-row-wide'
                ],
                'label' => __($delivery_option_field_label, 'woo-delivery'),
                'placeholder' => __($delivery_option_field_label, 'woo-delivery'),
                'options' => Woo_Delivery_Delivery_Option::delivery_option($delivery_option_settings),
                'required' => true,
                'custom_attributes' => [
                    'data-no_result_notice' => __($no_result_notice, 'woo-delivery'),
                ],
            ], WC()->checkout->get_value('woo_delivery_delivery_selection_box'));
        echo '</div>'

you need to test the code like it into your functions.php of child or active theme


add_filter( 'woocommerce_checkout_fields', 'remove_validation_from_field', PHP_INT_MAX, 1 );
 
function remove_validation_from_field( $woo_checkout_fields_array ) {
    // use unset to remove the validation from your desired field
    // like I am removing the phone number validation from billing address
    unset( $woo_checkout_fields_array['billing']['billing_phone']['validate'] );
    return $woo_checkout_fields_array;
}
add_filter( 'woocommerce_checkout_fields' , 'woocommerce_form_fields' );
                
function  woocommerce_form_fields( $fields ) {
$fields['billing']['billing_first_name']['required'] = false;
return $fields;
}

Actually we need use filter hook woocommerce_checkout_fields and need to modified based on billing,shiping,account,orders. For more details you may visit https://docs.woocommerce.com/document/tutorial-customising-checkout-fields-using-actions-and-filters/ thank you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM