0

I add the following code in in functions.php (wordpress), but it doesn't work correctly:

function change_city_to_dropdown( $fields ) {

    $city_args = wp_parse_args( array(
        'type' => 'select',
        'options' => array(
            'city1' => 'City1',
            'city2' => 'City2',
            'city3'   => 'City3',
            'city4' => 'City4',
            'city5'    => 'City5',
            'city6'  => 'City6',
        ),
        'input_class' => array(
            'wc-enhanced-select',
        )
    ), $fields['shipping']['shipping_city'] );

    $fields['shipping']['shipping_city'] = $city_args;
    $fields['billing']['billing_city'] = $city_args; // Also change for billing field

    wc_enqueue_js( "
    jQuery( ':input.wc-enhanced-select' ).filter( ':not(.enhanced)' ).each( function() {
        var select2_args = { minimumResultsForSearch: 5 };
        jQuery( this ).select2( select2_args ).addClass( 'enhanced' );
    });" );

    return $fields;

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


function change_address_1_to_dropdown( $fields ) {

    $address_1_args = wp_parse_args( array(
        'type' => 'select',
        'options' => array(
            'street1' => 'Street1',
            'street2' => 'Street2',
            'street3'   => 'Street3',
            'street4' => 'Street4',
            'street5'    => 'Street5',
            'street6'  => 'Street6',    
        ),
        'input_class' => array(
            'wc-enhanced-select',
        )
    ), $fields['shipping']['shipping_address_1'] );

    $fields['shipping']['shipping_address_1'] = $address_1_args;
    $fields['billing']['billing_address_1'] = $address_1_args; // Also change for billing field

    wc_enqueue_js( "
    jQuery( ':input.wc-enhanced-select' ).filter( ':not(.enhanced)' ).each( function() {
        var select2_args = { minimumResultsForSearch: 5 };
        jQuery( this ).select2( select2_args ).addClass( 'enhanced' );
    });" );

    return $fields;

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

I would like "street1" and "street2" to be assigned only to "city1", not to all cities. Likewise, "street3" and "street4" should be assigned to the city "city2". And, "street5" and "street6" should be assigned to the citys "city3", "city4", "city5" and "city6". Can you help me with these changes?

Thank you very much!

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
s3btqr
  • 1
  • 1
  • 1
    [This thread](https://stackoverflow.com/questions/48725121/dynamic-synched-custom-checkout-select-fields-in-woocommerce/48727517#48727517) may help you to understand how to do it. – LoicTheAztec Mar 16 '21 at 12:39
  • Thanks, but unfortunately I don't know how to proceed. – s3btqr Mar 17 '21 at 10:21

0 Answers0