0

I have an array of cities. Depending on the city chosen by the client, the shipping time will vary. I am trying to do it with the following code:

$city = WC()->customer->get_shipping_city();
    if( $city == 'adam' && 'aderet' && 'trom' && 'oranit' && 'shilo'){
        echo 'משלוח תוך 5 ימי עסקים';
    }elseif( $city == 'reyhan' && 'rimonim' && 'roy' && 'kedar' && 'ktora' ){
        echo 'משלוח תוך 3 ימי עסקים';
    }else{
        echo 'test';
    }

But I am not getting the desired result. The comparison works only for the first city. If I choose Adam I see the message: "משלוח תוך 5 ימי עסקים" But when I select any other city from the comparison list, I see "test" message. When I use || instead && then it doesn't matter at all what I choose, only this message is always triggered "משלוח תוך 5 ימי עסקים". Please help me figure out what I'm doing wrong in the code.

Victor Sokoliuk
  • 435
  • 4
  • 17
  • 1
    The code as you have written it will never work. Assuming `$city` is an array? then you have to compare the 2 arrays. There are plenty of [examples](https://stackoverflow.com/questions/2304436/how-can-i-use-in-array-if-the-needle-is-an-array) on stack overflow on how to do this. If `$city` is a string, you can use [`in_array`](https://www.php.net/manual/en/function.in-array.php) – 7uc1f3r Aug 05 '20 at 09:35
  • 1
    @7uc1f3r Variable $city is a string. Got I it right, I need to use the following construction: `$array = array('adam', 'aderet', 'trom', 'oranit', 'shilo'); if( in_array($city, $array))` – Victor Sokoliuk Aug 05 '20 at 09:45
  • Yes indeed, I believe this solves your question? – 7uc1f3r Aug 06 '20 at 09:29
  • @7uc1f3r Yes, thank you, now the code works perfectly. – Victor Sokoliuk Aug 06 '20 at 10:19

0 Answers0