Sell in only some cities woocommerce

In my account page,
I want to use a similar code as below to sell in a specific “cities” or “postcodes”, the below code is used to sell in a specific “states”:

/**
 * Sell only in Alger & Zeralda
 */

    function wc_sell_only_states( $states ) {
        $states['DZ'] = array(
            'ALG' => __( 'Alger', 'woocommerce' ),
            'ZLD' => __( 'Zeralda', 'woocommerce' ),
        );
        return $states;
    }
    add_filter( 'woocommerce_states', 'wc_sell_only_states' );

What shall I use or modify?

Read More

I tried this, but it displays “Error code 500”:

// define the woocommerce_countries_base_city callback 
function filter_woocommerce_countries_base_city( $var ) { 
    // make filter magic happen here... 
    $var['DZ'] = array(
        'ALG' => __( 'Alger', 'woocommerce' ),
        'ZLD' => __( 'Zeralda', 'woocommerce' ),
    );
    return $var; 
}; 

// add the filter 
add_filter( 'woocommerce_countries_base_city', 'filter_woocommerce_countries_base_city', 10, 1 );

Thank you in advanced!

Related posts

Leave a Reply

2 comments

  1. Try This it worked for me!!

                add_filter( 'woocommerce_form_field_args', 'custom_form_field_args', 10, 3 );
                function custom_form_field_args( $args, $key, $value ) { 
                    if ( $args['id'] == 'billing_city' ) {
                     $args = array(
                            'label' => __( 'Town / City', 'woocommerce' ),
                            'required' => TRUE,
                            'clear' => TRUE,
                            'type' => 'select',
                            'options' =>  array(
                                '' => ('Select City' ),
                        'Bangalore' => ('Bangalore' ),
                        'Mysore' => ('Mysore' )
                        ),
                            'class' => array( 'update_totals_on_change' )
                        );
                    } // elseif … and go on 
    
                    return $args;
                };
    
  2. There is a simpler solution to this. The approach also depends on the some assumption which are listed below:

    Assumption : If there is only one City which you want the City field to be set to then you can use jQuery. Using jQuery you will be setting the value of the field and make the fields disabled to avoid change in City field value.

    A rough example will be :

    jQuery('#billing_city').val('USA').attr('disabled','disabled');
    

    You just need to add this line such that it is execute on Checkout page.