bertassa
bertassa

Reputation: 685

The issue with in_array statement

I want to display certain URL links on a product page only for certain products and for certain countries. Initially, I retrieve the IP of the country then check, the product ID. If the product ID is in the array I display Link 1 if users are from Canada and Link 2 if they are from the US. The problem is that it is not showing anything. Nothing happens within the if statement that contains in_array and not sure what I am doing wrong.

<?php
if ($currentCountry =="CA" || $currentCountry =="US") 
{
    $p_id = $_product->getId();   
    $include_id = array(546, 125,135);
    ?>
    <style>
    .choose-local {
        display: flex;
    }
    .data-section {
        min-width: 33%;
    }
    .choose-local .data-section span {
        float: left;
    }
    </style>
    <?php
    if (in_array($p_id,$include_id, TRUE)) 
    {

        ?>
        <div class='choose-local'>
            <div class='data-section'>
                <span style="">Available Locally from :</span>
            </div>
            <div class="data-section">
                <?php 
                    if ($currentCountry =="CA") 
                    { 
                        ?>
                        <span>Link 1<a href='https://www.link1.com'>[Order from here]</a></span>
                        <?php
                    }

                    if ($currentCountry =="US") 
                    { 
                        ?>        
                        <span>Link 2<a href='https://www.link2.com'>[Order from here]</a></span>
                        <?php 
                    } 
                ?>      
            </div>
        </div>
    <?php 
    }
}
?>

Upvotes: 0

Views: 75

Answers (1)

yogesh sharma
yogesh sharma

Reputation: 36

check weather your $p_id is string or int

<?php
                                $currentCountry = "CA";
if ($currentCountry =="CA" || $currentCountry =="US") 
{
    $p_id = 125;   
    $include_id = array(546, 125,135);
    ?>
    <style>
    .choose-local {
        display: flex;
    }
    .data-section {
        min-width: 33%;
    }
    .choose-local .data-section span {
        float: left;
    }
    </style>
    <?php
    if (in_array($p_id,$include_id, TRUE)) 
    {

        ?>
        <div class='choose-local'>
            <div class='data-section'>
                <span style="">Available Locally from :</span>
            </div>
            <div class="data-section">
                <?php 
                    if ($currentCountry =="CA") 
                    { 
                        ?>
                        <span>Link 1<a href='https://www.link1.com'>[Order from here]</a></span>
                        <?php
                    }

                    if ($currentCountry =="US") 
                    { 
                        ?>        
                        <span>Link 2<a href='https://www.link2.com'>[Order from here]</a></span>
                        <?php 
                    } 
                ?>      
            </div>
        </div>
    <?php 
    }
}
?>

Upvotes: 1

Related Questions