0

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 
    }
}
?>
4
  • what a result of $p_id = $_product->getId(); if it's string, cast to integer Commented Aug 8, 2019 at 11:01
  • If you var_dump($p_id), what do you get? Is it a string? If so, try remove the third parameter from in_array(). That bool is strict mode which checks type Commented Aug 8, 2019 at 11:01
  • check your this variable $p_id = $_product->getId(); this might be String type int and in your in_array($p_id,$include_id, TRUE) you strictly checking the type. Try this $p_id = intval($_product->getId()); will work Commented Aug 8, 2019 at 11:02
  • $p_id = intval($_product->getId()); works!!! Thanks a lot guys for pointing me in the right direction. Commented Aug 8, 2019 at 11:18

1 Answer 1

1

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 
    }
}
?>

Sign up to request clarification or add additional context in comments.

1 Comment

thanks the users who commented yesterday had already suggested that. It was returning a string not an integer. This is why it was not working.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.