• Resolved Oded Talmon

    (@odedta)


    Hello,
    I’m trying to modify & re-order the checkout fields using 3 hooks (all with priority 1):

    woocommerce_checkout_fields
    woocommerce_billing_fields
    woocommerce_default_address_fields

    After all the modifications the $fields in each hook look like this: https://pastebin.com/QPW4wqiJ

    The fields in checkout look like this: https://ibb.co/1MPNGVz

    This is the code I’ve used:

    /*
     * Checkout Fields
     */
    function schechter_woocommerce_checkout_fields( $fields ) {
    	unset( $fields['billing']['billing_company'] );
    	
    	// Priority
    	$fields['billing']['billing_first_name']['priority'] = 10;
    	$fields['billing']['billing_last_name']['priority']  = 20;
    	$fields['billing']['billing_phone']['priority']      = 30;
    	$fields['billing']['billing_email']['priority']      = 40;
    	$fields['billing']['billing_address_1']['priority']  = 50;
    	$fields['billing']['billing_address_2']['priority']  = 60;
    	$fields['billing']['billing_city']['priority']       = 70;
    	$fields['billing']['billing_country']['priority']    = 80;
    	$fields['billing']['billing_postcode']['priority']   = 90;
    	
    	// Placeholders
    	$fields['billing']['billing_country']['placeholder']   = '';
    	$fields['billing']['billing_address_1']['placeholder'] = '';
    	$fields['billing']['billing_address_2']['placeholder'] = '';
    	$fields['order']['order_comments']['placeholder']      = '';
    	
    	/*
    	 * Tab Index
    	 */
    	$fields['billing']['billing_first_name']['custom_attributes'] = array(
    		'tabindex' => 1,
    	);
    	$fields['billing']['billing_last_name']['custom_attributes']  = array(
    		'tabindex' => 2,
    	);
    	$fields['billing']['billing_country']['custom_attributes']    = array(
    		'tabindex' => 3,
    	);
    	$fields['billing']['billing_city']['custom_attributes']       = array(
    		'tabindex' => 4,
    	);
    	$fields['billing']['billing_state']['custom_attributes']      = array(
    		'tabindex' => 5,
    	);
    	$fields['billing']['billing_address_1']['custom_attributes']  = array(
    		'tabindex' => 6,
    	);
    	$fields['billing']['billing_address_2']['custom_attributes']  = array(
    		'tabindex' => 7,
    	);
    	$fields['billing']['billing_postcode']['custom_attributes']   = array(
    		'tabindex' => 8,
    	);
    	$fields['billing']['billing_phone']['custom_attributes']      = array(
    		'tabindex' => 9,
    	);
    	$fields['billing']['billing_email']['custom_attributes']      = array(
    		'tabindex' => 10,
    	);
    	$fields['order']['order_comments']['custom_attributes']       = array(
    		'tabindex' => 11,
    		'rows'     => 1,
    	);
    	
    	return $fields;
    }
    add_filter( 'woocommerce_checkout_fields', 'schechter_woocommerce_checkout_fields', 1 );
    
    /*
     * Customize billing fields
     */
    function schechter_woocommerce_billing_fields( $billing_fields ) {
    	unset( $billing_fields['billing_company'] );
    	
    	// Set labels and placeholders
    	$billing_fields['billing_address_2']['label']       = 'Apartment, suite, unit, etc. (optional)';
    	$billing_fields['billing_address_2']['placeholder'] = '';
    	$billing_fields['billing_address_2']['label_class'] = array();
    	
    	// Set the order of the fields
    	$fields_order = array(
    		'first_name',
    		'last_name',
    		'country',
    		'state',
    		'city',
    		'address_1',
    		'address_2',
    		'postcode',
    		'phone',
    		'email',
    	);
    	
    	$count    = 1;
    	$priority = 10;
    	
    	foreach( $fields_order as $field ) {
    		if( isset( $billing_fields[ 'billing_' . $field ] ) ) {
    			$billing_fields[ 'billing_' . $field ]['priority'] = $count * $priority;
    			$ordered_fields[ 'billing_' . $field ]             = $billing_fields[ 'billing_' . $field ];
    		}
    		
    		$count++;
    	}
    	
    	return $ordered_fields;
    }
    add_filter( 'woocommerce_billing_fields', 'schechter_woocommerce_billing_fields', 1 );
    
    /*
     * Customize default address fields
     */
    function schechter_woocommerce_default_address_fields( $address_fields ) {
    	unset( $address_fields['company'] );
    	
    	// Set labels and placeholders
    	$address_fields['address_2']['label']       = 'Apartment, suite, unit, etc. (optional)';
    	$address_fields['address_2']['placeholder'] = '';
    	$address_fields['address_2']['label_class'] = array();
    	
    	// Set the order of the fields
    	$fields_order = array(
    		'first_name',
    		'last_name',
    		'country',
    		'state',
    		'city',
    		'address_1',
    		'address_2',
    		'postcode',
    		'phone',
    		'email',
    	);
    	
    	$count    = 1;
    	$priority = 10;
    	
    	foreach( $fields_order as $field ) {
    		if( isset( $address_fields[ $field ] ) ) {
    			$address_fields[ $field ]['priority'] = $count * $priority;
    			$ordered_fields[ $field ]             = $address_fields[ $field ];
    		}
    		
    		$count++;
    	}
    	
    	return $ordered_fields;
    }
    add_filter( 'woocommerce_default_address_fields', 'schechter_woocommerce_default_address_fields', 1 );

    The order modification is not working, why is that?

    Thanks

Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Checkout fields order’ is closed to new replies.