Friday, October 4, 2013

Woocommerce - Removing or changing fields in edit shipping or billing addresses.

Have you had problems with alter/remove/change or add fields for the edit shipping or edit billing addresses? Doing a search on this topic seems like I wasn't the only one who had the problem. But I've the solution for how to do this.

The first problem I had was to to know what function that controlled this fields. But pretty soon I found out that they where in fact described in the woocommerce docs.
The shipping fields exists in this function : woocommerce_shipping_fields and the billing fields exists in this function: woocommerce_billing_fields.

So the method to change these fields are then fairly easy to do. This is an example code in the functions.php-file on how to remove an unnecessary field "shipping state" and to set an label to the "shipping address 2":

add_filter( 'woocommerce_shipping_fields' , 'custom_override_shipping_fields' );
function custom_override_shipping_fields( $fields ) {

unset($fields['shipping_state']);
$fields['shipping_address_2']['label'] = 'Address Field 2';

     return $fields;
}

The same solution goes for billing fields. 
If you want to alter any fields on the checkout page you could use this method also. The function for those fields lays in this function "woocommerce_checkout_fields". An heads up: the array $fields is one step deeper and therefore if you want to remove the shipping_state like in the example above then you would need to to it like this: "unset($fields['shipping']['shipping_state']);" - notice the extra bracket. 

If you would like to know which fields exist for e.g. the shipping fields function, then use the php function var_dump(). IE: Put "var_dump($fields)" inside the custom override function and it will show you how the array looks like and all the settings for all fields.