Last active
October 2, 2018 13:27
-
-
Save mrpsiho/12cadb35d84d21d844d10385f1c9f70d to your computer and use it in GitHub Desktop.
Exclude/change attributtes for billing/shipping fields on the WooCommerce checkout page
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add_action( 'wp', 'uni_checkout_under_certain_circumstances' ); | |
function uni_checkout_under_certain_circumstances() { | |
if ( function_exists( 'is_checkout' ) && ( is_checkout() || is_ajax() ) ) { | |
remove_action( 'woocommerce_before_checkout_form', 'woocommerce_checkout_coupon_form', 10 ); | |
// the following line disables "notes" field | |
add_filter( 'woocommerce_enable_order_notes_field', '__return_false' ); | |
// an example of unsetting checkout fields | |
add_filter( 'woocommerce_checkout_fields', 'uni_unset_billing_checkout_fields', 10, 1 ); | |
// an example of modifying atributtes of checkout fields | |
add_filter( 'woocommerce_checkout_fields', 'uni_set_required_shipping_checkout_fields', 10, 1 ); | |
} | |
} | |
// | |
function uni_unset_billing_checkout_fields( $fields ) { | |
$billing_keys = array( | |
'billing_company', | |
'billing_phone', | |
'billing_address_1', | |
'billing_address_2', | |
'billing_city', | |
'billing_postcode', | |
'billing_country', | |
'billing_state', | |
); | |
foreach ( $billing_keys as $key ) { | |
unset( $fields['billing'][ $key ] ); | |
} | |
return $fields; | |
} | |
// | |
function uni_set_required_shipping_checkout_fields( $fields ) { | |
$shipping_keys = array( | |
'shipping_phone', | |
'shipping_address_1', | |
'shipping_city', | |
'shipping_postcode', | |
'shipping_country', | |
'shipping_state', | |
); | |
foreach ( $shipping_keys as $key ) { | |
$fields['shipping'][ $key ]['required'] = true; | |
} | |
return $fields; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment