Home > front end >  Remove countries/states CDATA from WooCommerce Checkout (html code)
Remove countries/states CDATA from WooCommerce Checkout (html code)

Time:07-04

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?

CodePudding user response:

Try by creating an empty array

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

CodePudding user response:

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 );
  • Related