skip to Main Content

I’m trying to move custom fields that I created on my checkout page in the Shipping block.

What I’ve tried is this code: thise are the new fields

// Add a new checkout field
function filter_checkout_fields($fields){
    
    $fields['extra_fields'] = array(
            'some_field' => array(
                'type' => 'text',
                'required'      => false,
                'label' => __( 'Field 1:' )
                ),
            'another_field' => array(
                'type' => 'text',
                'required'      => false,
                'label' => __( 'Field 2:' )
                ),

    return $fields;

    
}
add_filter( 'woocommerce_checkout_fields', 'filter_checkout_fields' );

// display the extra field on the checkout form
function extra_checkout_fields(){ 

    $checkout = WC()->checkout(); ?>

    <div class="extra-fields">
    <h3><?php _e( 'Title' ); ?></h3>

    <?php 
    // because of this foreach, everything added to the array in the previous function will display automagically
    foreach ( $checkout->checkout_fields['extra_fields'] as $key => $field ) : ?>

            <?php woocommerce_form_field( $key, $field, $checkout->get_value( $key ) ); ?>

        <?php endforeach; ?>
    </div>

   <?php }
   add_action( 'woocommerce_checkout_after_customer_details' ,'extra_checkout_fields' );

And this is how I’m trying to move them (replace current fields)

add_filter( 'woocommerce_checkout_fields', 'another_group' );
 
function another_group( $checkout_fields ){
 
    // 1. We assign a field array to another group here
    $checkout_fields['some_field'] = $checkout_fields['shipping']['shipping_first_name'];
    $checkout_fields['another_field'] = $checkout_fields['shipping']['shipping_last_name'];
    
    // 2. Remove a field from a previous location
    unset( $checkout_fields['shipping']['shipping_first_name'] );
    unset( $checkout_fields['shipping']['shipping_last_name'] );
 
    return $checkout_fields;
 
}

What is happening is that the ['shipping_first_name'] and ['shipping_last_name'] are removed (unset) but nothing appeared on their place.

Is this possible to happen at all?

2

Answers


  1. Chosen as BEST ANSWER

    Just unset the fields that you don't need as you do already but don't try to override them in this function

    add_filter( 'woocommerce_checkout_fields', 'another_group' );
     
    function another_group( $checkout_fields ){
             
        // 2. Remove a field from a previous location
        unset( $checkout_fields['shipping']['shipping_first_name'] );
        unset( $checkout_fields['shipping']['shipping_last_name'] );
    
         // ... more fields for unset here
     
        return $checkout_fields;
     
    }
    

    Then in your function where you have [extra_fields] change it to [shipping] this will place the custom fields in the shipping block.

    // Add a new checkout field
    function filter_checkout_fields($fields){
        
        $fields['shipping'] = array(
            'some_field' => array(
                'type' => 'text',
                'required'      => false,
                'label' => __( 'Field 1:' )
                ),
                  ... so on
    

  2. you can set priority of custom field with your old field priority like below
    $fields[‘shipping’][‘some_field’][‘priority’] = 10;
    $fields[‘shipping’][‘another_field’][‘priority’] = 20;

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search