Concatenate two conditions in an if

1

I am developing a small script for woocommerce, where according to the geolocation and product category, show or hide the price.

In this case I managed to make it work by means of geolocation to hide the price, but this is very generic because it affects all products, my goal is to be able to select in which categories it applies.

The first option works without problem

 $userInfo = geoip_detect2_get_info_from_current_ip();
 if (($userInfo->city->name == 'Alicante' || $userInfo->city->name =='Bilbao')
 {
  codigo a ejecutar 
 }

else {

codigo a ejecutar 

     }

In this case with has term I can determine in what category I want it to be applied, but for some reason it tells me that the first bracket is left over {, I have reviewed documentation and I am not able to find the error.

 $userInfo = geoip_detect2_get_info_from_current_ip();
 if (($userInfo->city->name == 'Alicante' || $userInfo->city->name =='Bilbao') && ( has_term( array( 'pantalones',), 'product_cat')) 
{
codigo a ejecutar 
}

 else {

codigo a ejecutar 

 }
    
asked by Fran Cuesta 09.01.2018 в 13:11
source

4 answers

3

The following code has a correct structure:

if ( /*Bloque de condiciones */ ) {
    //Código a ejecutar

} else {

    //Código a ejecutar 
}

Your problem was this:

if (( /*Bloque de condiciones */ ) {
    //Código a ejecutar

} else {

    //Código a ejecutar 
}

A double parenthesis opening in the if statement. A sign of opening parentheses was left over. However, PHP gave you a syntax error, warning you that it found an opening sign {wrong, which happens because what you expect is a closing parenthesis and not the sign that is found.

Since the execution is sequential, in the syntax errors , if you do not see it at first glance, you have to look at what precedes it. what it tells you as an error .

    
answered by 09.01.2018 в 13:45
2

I would propose a global solution, which would have the following advantages:

  • You can use it from anywhere in the code.
  • It will not force you to modify the code (also writing infinite || ) if you had to compare many cities, since you will receive an array of cities ( $arrCiudades ) to evaluate them.
  • You will also use a $arrTerms , which offers the same advantage mentioned above, in case you want to evaluate several terms.

You will build the parameters outside the function, you will pass them to them and the function will return a boolean result (true or false), the result of the two comparisons you must make.

<?php 
    $userInfo = geoip_detect2_get_info_from_current_ip();  
    $strCiudad=$userInfo->city->name;
    $arrCiudades=array ('Alicante', 'Bilbao');
    $arrTerms=array('pantalones');
    $strCategoria='product_cat';

    /*Aquí tendrás el resultado de la comparación*/
    $bolMostrar=mostrarPrecio($strCiudad,$arrCiudades,$arrTerms,$strCategoria);

    /*Esta es la función en sí, que podrías incluir en una clase utilitaria si quisieras*/
    function mostrarPrecio($strCiudad,$arrCiudades,$arrTerms,$strCategoria)
    {
        $bolMostrar=in_array($strCiudad,$arrCiudades) && has_term($arrTerms,$strCategoria);
        return $bolMostrar;
    }

?>
    
answered by 09.01.2018 в 13:52
0

Good! The sentence is correct but you have an opening parenthesis (right after the IF). This code would be correct:

 $userInfo = geoip_detect2_get_info_from_current_ip();
 if ($userInfo->city->name == 'Alicante' || $userInfo->city->name =='Bilbao') && ( has_term( array( 'pantalones',), 'product_cat')) 
{
    codigo a ejecutar 
}

else {
    codigo a ejecutar 
 }

If you still have problems with the code try to use || instead of & & Greetings.

    
answered by 09.01.2018 в 13:20
0

The problem is not that on a parenthesis at the beginning, but that you are missing one at the end.

( has_term( array( 'pantalones',), 'product_cat')) )<-

If you notice, the if starts with the "global" parenthesis and then you open another parenthesis for the first condition, after & & amp; you open the parenthesis for the second condition. In this second condition after & amp ;, you open 3 parentheses but you close 3 too, you would be missing the parenthesis of the "global" if it encloses both conditions. It would look like this:

$userInfo = geoip_detect2_get_info_from_current_ip();
if (($userInfo->city->name == 'Alicante' || $userInfo->city->name =='Bilbao') && ( has_term( array( 'pantalones',), 'product_cat'))) //<- nuevo paréntesis 
{
codigo a ejecutar 
}

 else {

codigo a ejecutar 

}   

In the first block of code, you say that if it works, there is a parenthesis left over at the beginning

if (($userInfo->city->name == 'Alicante' || $userInfo->city->name =='Bilbao')
    
answered by 09.01.2018 в 21:41