skip to Main Content

There is a list of all states and cities in html code. Actually I do not need states and cities.

enter image description here

I can remove them by adding this code to the functions.php

   add_filter( 'woocommerce_states', function( $states ){
       unset( $states['AF'] );
       unset( $states['AL'] );
       unset( $states['AO'] );
       unset( $states['AR'] );

       // AND SO ON

       return $states;
   }, 999);

But if I want to remove them all with code, I get an error:

add_filter( 'woocommerce_states', function( $states ){
       unset( $states );
        return $states;
   }, 999);

Warning: array_merge(): Expected parameter 1 to be an array, null given in /public_html/wp-content/plugins/woocommerce/includes/class-wc-frontend-scripts.php on line 580

Is listing all the countries the only way to remove them from the html code?

2

Answers


  1. Try by creating an empty array

    add_filter( 'woocommerce_states', function( $states ){
            $states = array();
            return $states;
       }, 999);
    
    Login or Signup to reply.
  2. I know there is already an accepted answer, but this could easily be resolved with one line, since __return_empty_array already does what you want.

    add_filter( 'woocommerce_states', '__return_empty_array', 99 );
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search