skip to Main Content

I have created address_2 as a dropdown but the placeholder is not showing.

/* address_2 dropdown */


add_filter( 'woocommerce_default_address_fields' , 'address2_dropdown' );

function address2_dropdown( $address_fields ) {

    // address_2
    $address_fields['address_2']['type'] = 'select';
    $address_fields['address_2']['placeholder'] = 'Adresse';
    $address_fields['address_2']['options'] = array(
        '' => '',
        'villa' => 'Villa',
        'apartment'    => 'Apartment',
        'other'  => 'Other',        
    );
    
    // Sort
    ksort($address_fields['address_2']['options']);

    return $address_fields;
}

I used the code above to make the dropdown.

2

Answers


  1. Chosen as BEST ANSWER

    I got it from this https://stackoverflow.com/a/47015385/12536243

    here is the code if someone wants to use for address_2

    // Change "address"  to a dropdown
    add_filter( 'woocommerce_default_address_fields' , 'override_address_2_dropdown' );
    function override_address_2_dropdown( $address_fields ) {
    
        // Define here in the array your desired name 
        $option_unit_type = array(
             '' => __( 'Select unit type' ),
            'villa' => 'Villa',
            'apartment' => 'Apartment',
            'other' => 'Other',
        );
    
        $address_fields['address_2']['type'] = 'select';
        $address_fields['address_2']['options'] = $option_unit_type;
    
    
        return $address_fields;
    }
    

  2. In select box placeholder is first blank option.

    add_filter( 'woocommerce_default_address_fields' , 'address2_dropdown' );
    
    function address2_dropdown( $address_fields ) {
    
        // address_2
        $address_fields['address_2']['type'] = 'select';
        $address_fields['address_2']['options'] = array(
            '' => 'Address',
            'villa' => 'Villa',
            'apartment'    => 'Apartment',
            'other'  => 'Other',        
        );
    
        // Sort
        ksort($address_fields['address_2']['options']);
    
        return $address_fields;
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search