1

I'm have been searching for solution for this in google but I have not found any. Is there a way to hide some state if a coupon its active?

I found "Remove specific states of a country on Woocommerce Checkout" answer code that works with payment gateways and I´m trying to adapt the code for states:

add_filter('woocommerce_states', 'applied_coupons_hide_states', 20, 1 ); 
function applied_coupons_hide_states( $states){

    if( sizeof( WC()->cart->get_applied_coupons() ) > 0 ){

        foreach ( $states as $state_key => $state_value ) {

            if( $state_key != 'LMA,LIM' )
                unset($states[$state_key]);
        }
    }

    return $states;
}

But I can't make it work. What I am doing wrong?

Any help is appreciated.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399

1 Answers1

1

There are some mistakes in your code… I suppose that the related country is Peru (PE). Try the following:

add_filter('woocommerce_states', 'applied_coupons_hide_states', 10, 1 ); 
function applied_coupons_hide_states( $states){
    // On cart or checkout pages, if any coupon is applied
    if( ( is_cart() || is_checkout() ) && sizeof( WC()->cart->get_applied_coupons() ) > 0 ){
        $country = 'PE'; // Defined country: Peru
        $targered_states = array('LMA','LIM'); // States to be removed

        foreach ( $targered_states as $state_code ) {
            if( isset($states['US'][$state_code]) ) {
                unset( $states['US'][$state_code] );
            }
        }
    }
    return $states;
}

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

Related tread: Remove specific states of a country on Woocommerce Checkout

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Thanks for the help, i read some of your answer for similar topics i was trying my best to make this work but i couldn't, you are awesome, muchas gracias de verdad. – Elizabeth parodi roman Mar 04 '19 at 21:39