php - WooCommerce: Disabling checkout fields with a filter hook -
i have try disable "required" property of several checkout fields @ same time using woocommerce_checkout_fields
filter hook, no success.
plugins not working either. code:
// hook in add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' ); // our hooked in function - $fields passed via filter! function custom_override_checkout_fields( $fields ) { $fields['billing']['billing_address_1']['required'] = false; $fields['billing']['billing_address_2']['required'] = false; $fields['billing']['billing_postcode']['required'] = false; $fields['billing']['billing_city']['required'] = false; $fields['billing']['billing_phone']['required'] = false; return $fields; }
what wrong?
how can achieve this?
you need use unset()
function purpose , can do-it way:
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' ); function custom_override_checkout_fields( $fields ) { unset($fields['billing']['billing_address_1']); unset($fields['billing']['billing_address_2']); unset($fields['billing']['billing_postcode']); unset($fields['billing']['billing_city']); unset($fields['billing']['billing_phone']); return $fields; }
you need paste code in function.php file located in active child theme or theme.
you have changed question an update: here new answer
making address fields optional (regarding update):
you have use different filter hook purpose. there 2 different filter hooks:
1) billing fields:
add_filter( 'woocommerce_billing_fields', 'wc_optional_billing_fields', 10, 1 ); function wc_optional_billing_fields( $address_fields ) { $address_fields['billing_address_1']['required'] = false; $address_fields['billing_address_2']['required'] = false; $address_fields['billing_postcode']['required'] = false; $address_fields['billing_city']['required'] = false; $address_fields['billing_phone']['required'] = false; return $address_fields; }
2) shipping fields:
add_filter( 'woocommerce_shipping_fields', 'wc_optional_shipping_fields', 10, 1 ); function wc_optional_shipping_fields( $address_fields ) { $address_fields['shipping_phone']['required'] = false; return $address_fields; }
to make address fields required:
you can use same hooks , functions above, changing each field false
true
.
you need paste code in function.php file located in active child theme or theme.
reference: customizing checkout fields using hooks
Comments
Post a Comment