Here we will learn about how to modify billing and shipping field’s In WooCommerce.
Custom code copied and paste into your child theme’s functions.php file.
woocommerce_checkout_fields:
Using this hook you can override any field on the checkout page.
Here is some example:
If You can change the placeholder fields on the checkout page.
<?php add_filter( 'woocommerce_checkout_fields' , 'notes_placeholder_checkout_fields' ); function notes_placeholder_checkout_fields( $fields ) { $fields['order']['order_comments']['placeholder'] = 'Please add new placeholder'; return $fields; } ?>
If You can override placeholder labels on the checkout page.
<?php add_filter( 'woocommerce_checkout_fields' , 'notes_label_checkout_fields' ); function notes_label_checkout_fields( $fields ) { $fields['order']['order_comments']['label'] = 'Add new label'; return $fields; } ?>
If You remove comments fields on the checkout page.
<?php add_filter( 'woocommerce_checkout_fields' , 'comments_removed_checkout_fields' ); function comments_removed_checkout_fields( $fields ) { unset( $fields['order']['order_comments'] ); return $fields; } ?>
If You remove required fields on the checkout page.
<?php add_filter( 'woocommerce_default_address_fields' , 'comments_required_removed_checkout_fields' ); function comments_required_removed_checkout_fields( $address_fields ) { $address_fields['address_1']['required'] = false; return $address_fields; } ?>
woocommerce_default_address_fields – It is applied for all billing and shipping fields.
The following list of fields in the array passed to Woocommerce modification:
- Billing
- $fields[‘billing’][‘billing_first_name’]
- $fields[‘billing’][‘billing_last_name’]
- $fields[‘billing’][‘billing_company’]
- $fields[‘billing’][‘billing_address_1’]
- $fields[‘billing’][‘billing_address_2’]
- $fields[‘billing’][‘billing_city’]
- $fields[‘billing’][‘billing_postcode’]
- $fields[‘billing’][‘billing_country’]
- $fields[‘billing’][‘billing_state’]
- $fields[‘billing’][‘billing_email’]
- $fields[‘billing’][‘billing_phone’]
- Shipping
- $fields[‘shipping’][‘shipping_first_name’]
- $fields[‘shipping’][‘shipping_last_name’]
- $fields[‘shipping’][‘shipping_company’]
- $fields[‘shipping’][‘shipping_address_1’]
- $fields[‘shipping’][‘shipping_address_2’]
- $fields[‘shipping’][‘shipping_city’]
- $fields[‘shipping’][‘shipping_postcode’]
- $fields[‘shipping’][‘shipping_country’]
- $fields[‘shipping’][‘shipping_state’]
- Account
- $fields[‘account’][‘account_username’]
- $fields[‘account’][‘account_password’]
- $fields[‘account’][‘account_password-2’]
- Order
- $fields[‘order’][‘order_comments’]
Each field contains an array of properties:
- type – the field type likes: (text, textarea, password, select).
- label – label for the input field.
- placeholder – placeholder for the input.
- class – class for the input.
- required – the field is required to pass value true or false.
- clear – it is a clear fix to the field/label.
- label_class – add a class for the label.
- options – array of options (key => value).