Replace the city text field by a dropdown of cities for a specific country in Woocommerce

后端 未结 1 1171
Happy的楠姐
Happy的楠姐 2021-01-22 20:45

The existing list of countries is excellent, but we need to select Saudi Arabia in particular and show it another list with the names of the main cities such as Riyadh, Jeddah,

1条回答
  •  情深已故
    2021-01-22 21:23

    Updated (Additional city text field when "Others" is the selected value from the dropdown)

    The following code (jQuery powered) will replace the city text field by a custom dropdown of cities for a specific country only and for this specific country, if the city selected value is "Others", an additional text field will appear under the cities dropdown, where customer can enter manually a different city.

    The code works for shipping and billing fields independently.

    When "Others" is selected for the defined country, the two last functions will:

    • Validate that the city additional field is filled,
    • Save the city value as billing or shipping city value.

    The code:

    // HERE are is the array of cities for Saudi Arabia (SA)
    function get_cities_options(){
        $domain = 'woocommerce'; // The domain text slug
    
        return array(
            ''          => __('Select a city', $domain),
            'Abhā'      => 'Abhā',      'Abqaiq'    => 'Abqaiq',
            'Al-Baḥah'  => 'Al-Baḥah',  'Al-Dammām' => 'Al-Dammām',
            'Al-Hufūf'  => 'Al-Hufūf',  'Al-Jawf'   => 'Al-Jawf',
            'Al-Kharj'  => 'Al-Kharj',  'Al-Khubar' => 'Al-Khubar',
            'Al-Qaṭīf'  => 'Al-Qaṭīf',  'Al-Ṭaʾif'  => 'Al-Ṭaʾif',
            'ʿArʿar'    => 'ʿArʿar',    'Buraydah'  => 'Buraydah',
            'Dhahran'   => 'Dhahran',   'Ḥāʾil'     => 'Ḥāʾil',
            'Jiddah'    => 'Jiddah','Jīzān'     => 'Jīzān',
            'Khamīs Mushayt'            => 'Khamīs Mushayt',
            'King Khalīd Military City' => 'King Khalīd Military City',
            'Mecca'     => 'Mecca',     'Medina'    => 'Medina',
            'Najrān'    => 'Najrān',    'Ras Tanura'=> 'Ras Tanura',
            'Riyadh'    => 'Riyadh',    'Sakākā'    => 'Sakākā',
            'Tabūk'     => 'Tabūk',     'Yanbuʿ'    => 'Yanbuʿ',
            'Other'     => __('Other cities (not listed)', $domain),
        );
    }
    
    // add an additional field
    add_filter( 'woocommerce_checkout_fields' , 'additional_checkout_city_field' );
    function additional_checkout_city_field( $fields ) {
        // Inline CSS To hide the fields on start
        ?> _x('Other city', 'placeholder', 'woocommerce'),
            'required'  => false,
            'priority'  => 75,
            'class'     => array('form-row-wide hidden'),
            'clear'     => true
        );
    
        $fields['shipping']['shipping_city2'] = array(
            'placeholder'   => _x('Other city', 'placeholder', 'woocommerce'),
            'required'  => false,
            'priority'  => 75,
            'class'     => array('form-row-wide hidden'),
            'clear'     => true
        );
    
        return $fields;
    }
    
    // Add checkout custom select fields
    add_action( 'wp_footer', 'custom_checkout_city_field', 20, 1 );
    function custom_checkout_city_field() {
        // Only checkout page
        if( is_checkout() && ! is_wc_endpoint_url() ):
    
        $country = 'SA'; //  <=== <=== The country code
    
        $b_city  = 'billing_city';
        $s_city  = 'shipping_city';
        $billing_city_compo    = 'name="'.$b_city.'" id="'.$b_city.'"';
        $shipping_city_compo   = 'name="'.$s_city.'" id="'.$s_city.'"';
        $end_of_field          = ' autocomplete="address-level2" value="">';
        $billing_text_field    = '
        
        set_billing_city(sanitize_text_field( $_POST['billing_city2'] ) );
        }
    
        // Updating shipping city
        if( isset($_POST['shipping_city2']) && ! empty($_POST['shipping_city2']) && $_POST['shipping_city'] == 'Other' ){
            $order->set_shipping_city(sanitize_text_field( $_POST['shipping_city'] ) );
        }
    }
    

    Code goes in function.php file of your active child theme (active theme). Tested and works.

    Please Remember that a customer can be in a foreign country outside Soudi Arabia (billing country) and buy something that will be shipped in Soudi Arabia (shipping country).

    0 讨论(0)
提交回复
热议问题