使由插件创建的自定义结帐必填字段在 WooCommerce 中可选

问题描述

在woocommerce结帐页面需要删除特定的自定义字段验证。这个字段基本上是由插件使用 woocommerce_form_field() 生成的。有什么办法可以删除该特定字段的验证?代码

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,'options' => Woo_Delivery_Delivery_Option::delivery_option($delivery_option_settings),'required' => true,'custom_attributes' => [
                    'data-no_result_notice' => __($no_result_notice,],WC()->checkout->get_value('woo_delivery_delivery_selection_Box'));
        echo '</div>'

解决方法

您需要在您的 functions.php 子主题或活动主题中测试类似的代码


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;
}

实际上我们需要使用过滤钩woocommerce_checkout_fields,需要根据账单、发货、账户、订单进行修改。 有关更多详细信息,您可以访问 https://docs.woocommerce.com/document/tutorial-customising-checkout-fields-using-actions-and-filters/ 谢谢