How to Edit Wocoomerce Checkout Fields?

Total
0
Shares

As you know, there are standard payment fields in the Woocommerce infrastructure. Separate fields can be entered for shipping and separate fields for billing. However, in some cases we need to reorganise these fields. We may need to remove some fields or make new additions.

There are many plugins to manage Woocommerce payment fields. Here I will explain how to make simple edits you may need without using plugins.

How do we remove the fields we don’t want from Woocommerce payment fields?

With the code below, we remove the fields we do not need from the payment form. You can edit this code according to your needs. Simply add this code to the functions.php file in your Child Theme folder.

add_filter( 'woocommerce_checkout_fields', 'remove_company_checkout_field' );

function remove_company_checkout_field( $fields ) {
    unset( $fields['billing']['billing_company'] );
    unset( $fields['shipping']['shipping_company'] );
	
	unset( $fields['billing']['billing_city'] );
    unset( $fields['shipping']['shipping_city'] );
	
	unset( $fields['billing']['billing_state'] );
    unset( $fields['shipping']['shipping_state'] );
	
	unset( $fields['billing']['billing_postcode'] );
    unset( $fields['shipping']['shipping_postcode'] );
	
	unset( $fields['billing']['billing_country'] );
	unset( $fields['shipping']['shipping_country'] );
	

    return $fields;
}


add_filter( 'woocommerce_checkout_fields', 'custom_checkout_fields_order' );

How do we cancel the option to send to an address other than the billing address from the Woocommerce payment area?

Sometimes we don’t need this option and we don’t want it to make an unnecessary crowd. Again, it will be enough to add this code to your functions.php file.

add_filter( 'woocommerce_checkout_fields', 'remove_shipping_address_option' );

function remove_shipping_address_option( $fields ) {
    // Fatura adresi alanları içerisindeki "Farklı bir adrese gönderilsin mi?" seçeneğini kaldır
    unset( $fields['billing']['ship_to_different_address'] );
	 unset( $fields['shipping']['ship_to_different_address'] );

    return $fields;
}

How do I add a new field to Woocommerce checkout fields and how do I sort them?

This is also a much needed feature. By editing the code I have given below, you can add and sort the payment form address information as you wish.

I used this code for a food site. Since they only ship to certain neighbourhoods, I added a field to make it easier for users to select the neighbourhood. Then I set the order of the fields. You can also revise this code according to your own needs.

add_filter( 'woocommerce_checkout_fields', 'custom_checkout_fields_order' );

add_filter( 'woocommerce_ship_to_different_address_checked', '__return_false' );

function custom_checkout_fields_order( $fields ) {
     $selected_districts = array( 
'First Neighbourhood.', 
'Second Neighbourhood.', 
'Third Neighbourhood' 

); // Add the neighbourhoods that can be selected here

    // Create neighbourhood area
    $fields['billing']['billing_district'] = array(
        'type'     => 'select',
        'label'    => __('Neighbourhood', 'woocommerce'),
        'required' => true,
        'class'    => array('form-row-wide'),
        'options'  => array_combine( $selected_districts, $selected_districts ),
    );

    // Set the order of other fields
    $order = array(
		
        "billing_state",
	"billing_district",
        "billing_first_name",
        "billing_last_name",
	"billing_address_1",
        "billing_address_2",
        "billing_phone",
        "billing_email",
		
    );

    // Update queue
    foreach( $order as $field_name ) {
        $ordered_fields[$field_name] = $fields['billing'][$field_name];
    }

    $fields['billing'] = $ordered_fields;

    return $fields;
}

How do I hide cities that are outside my delivery area?

For example, you only send to Istanbul from your website and you don’t want users to select other cities. In this case, you can hide other cities very simply with the code below.

/** No elections outside Istanbul */
add_filter( 'woocommerce_states', 'limit_billing_states_to_istanbul', 10, 1 );

function limit_billing_states_to_istanbul( $states ) {
    // Sadece İstanbulun posta kodu ve ismi
    $istanbul_state_code = '34';
    $istanbul_state_name = 'İstanbul';

    // Remove cities other than Istanbul
    if ( isset( $states['TR'] ) && is_array( $states['TR'] ) ) {
        foreach ( $states['TR'] as $state_code => $state_name ) {
            if ( $state_code !== $istanbul_state_code && $state_name !== $istanbul_state_name ) {
                unset( $states['TR'][$state_code] );
            }
        }
    }

    return $states;
}

I hope these codes will be useful to you. Please do not hesitate to write a comment if you see missing or incorrect places. Thanks for your help.

Leave a Reply

Your email address will not be published. Required fields are marked *

Sign Up for Our Newsletters

Get notified of the best deals on our WordPress themes.

You May Also Like

Best WHMCS alternative

After using WHMCS for years, I started looking for an automation software alternative to WHMCS due to increasing problems, rising prices and a complex interface. After many attempts, I met…
View Post