Jump to content

Prestashop module for hiding products in specific countries


Recommended Posts

Update note:
Hi community members. We have come to a solution for our problem. Actually I can not share the module due to our clients restriction. Because we have been paid to develop this module. But I want to share the idea and design for this module. If you are a developer you can create this module pretty easy.

What is the need?

  • Imagine you need to hide some products for some countries.
  • For example we have a product100
  • And we have a shop which is open to all countries in the world
  • But we want product100 to be available to order only in Canada and no where else
  • We also have another product called product200
  • We want to sell this product in any country except Germany and USA

What was our Idea?

  • Set custom feature to product
  • Custom feature indicates that this product is hidden in some coutnries
  • Set ISO code of countries as the custom value of product feature

How to filter products?

  • In order to filter products and remove some from listing, use this hook: filterProductSearch

How to identify the country of visitor?

  • There are a lot of ways to translate visitor's IP address into ISO code of the country
  • We have used MaxMind database

 

Edited by stifler97 (see edit history)
Link to comment
Share on other sites

  • 3 weeks later...
On 4/20/2022 at 4:58 PM, idnovate.com said:

Hi. I checked this module demo. This is not practical because our client has more than 40,000 products. And a lot of customers. But in this module there is only one box of products and customers without any search option. So we had to figure this out by our self. Thanks for your reply.

Link to comment
Share on other sites

On 4/17/2022 at 7:57 PM, ndiaga said:

Hi,

I  can  help   customize  that   one  to  hide   product.

It  should  be   a  big  issue  since  they  did  the  most  important  part  of the  job.

We needed some ideas. We had figured it out. I will upload module here.

 

Link to comment
Share on other sites

 I and my teammate created one module which works with this hook:

filterProductSearch

And a function to translate ip address of visitor to country iso code which you can find on stackoverflow.
This is the function
 

<?php

function ip_info($ip = NULL, $purpose = "location", $deep_detect = TRUE) {
    $output = NULL;
    if (filter_var($ip, FILTER_VALIDATE_IP) === FALSE) {
        $ip = $_SERVER["REMOTE_ADDR"];
        if ($deep_detect) {
            if (filter_var(@$_SERVER['HTTP_X_FORWARDED_FOR'], FILTER_VALIDATE_IP))
                $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
            if (filter_var(@$_SERVER['HTTP_CLIENT_IP'], FILTER_VALIDATE_IP))
                $ip = $_SERVER['HTTP_CLIENT_IP'];
        }
    }
    $purpose    = str_replace(array("name", "\n", "\t", " ", "-", "_"), NULL, strtolower(trim($purpose)));
    $support    = array("country", "countrycode", "state", "region", "city", "location", "address");
    $continents = array(
        "AF" => "Africa",
        "AN" => "Antarctica",
        "AS" => "Asia",
        "EU" => "Europe",
        "OC" => "Australia (Oceania)",
        "NA" => "North America",
        "SA" => "South America"
    );
    if (filter_var($ip, FILTER_VALIDATE_IP) && in_array($purpose, $support)) {
        $ipdat = @json_decode(file_get_contents("http://www.geoplugin.net/json.gp?ip=" . $ip));
        if (@strlen(trim($ipdat->geoplugin_countryCode)) == 2) {
            switch ($purpose) {
                case "location":
                    $output = array(
                        "city"           => @$ipdat->geoplugin_city,
                        "state"          => @$ipdat->geoplugin_regionName,
                        "country"        => @$ipdat->geoplugin_countryName,
                        "country_code"   => @$ipdat->geoplugin_countryCode,
                        "continent"      => @$continents[strtoupper($ipdat->geoplugin_continentCode)],
                        "continent_code" => @$ipdat->geoplugin_continentCode
                    );
                    break;
                case "address":
                    $address = array($ipdat->geoplugin_countryName);
                    if (@strlen($ipdat->geoplugin_regionName) >= 1)
                        $address[] = $ipdat->geoplugin_regionName;
                    if (@strlen($ipdat->geoplugin_city) >= 1)
                        $address[] = $ipdat->geoplugin_city;
                    $output = implode(", ", array_reverse($address));
                    break;
                case "city":
                    $output = @$ipdat->geoplugin_city;
                    break;
                case "state":
                    $output = @$ipdat->geoplugin_regionName;
                    break;
                case "region":
                    $output = @$ipdat->geoplugin_regionName;
                    break;
                case "country":
                    $output = @$ipdat->geoplugin_countryName;
                    break;
                case "countrycode":
                    $output = @$ipdat->geoplugin_countryCode;
                    break;
            }
        }
    }
    return $output;
}

?>

This is how we use it

echo ip_info("Visitor", "Country Code"); // IN

And thats it. There are some bugs in the module and we are going to add some new features. After all i will upload it here.

Link to comment
Share on other sites

3 minutes ago, ndiaga said:

Witch   module?   Is   it  your  own  code  or  paid  module's   code?

Don't   share   people's    paid   module  codes  here.

 

It is not a paid module. It is a custom module which we developed.

  • Thanks 1
Link to comment
Share on other sites

On 5/8/2022 at 6:28 PM, stifler97 said:

Hi. I checked this module demo. This is not practical because our client has more than 40,000 products. And a lot of customers. But in this module there is only one box of products and customers without any search option. So we had to figure this out by our self. Thanks for your reply.

There was a bug in some browsers 😅

It's fixed now:

imagen.png.c3a72747ddfcf14b5833c16c8261e624.png

  • Thanks 1
Link to comment
Share on other sites

On 5/8/2022 at 6:34 PM, stifler97 said:

 I and my teammate created one module which works with this hook:

filterProductSearch

And a function to translate ip address of visitor to country iso code which you can find on stackoverflow.
This is the function
 

<?php

function ip_info($ip = NULL, $purpose = "location", $deep_detect = TRUE) {
    $output = NULL;
    if (filter_var($ip, FILTER_VALIDATE_IP) === FALSE) {
        $ip = $_SERVER["REMOTE_ADDR"];
        if ($deep_detect) {
            if (filter_var(@$_SERVER['HTTP_X_FORWARDED_FOR'], FILTER_VALIDATE_IP))
                $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
            if (filter_var(@$_SERVER['HTTP_CLIENT_IP'], FILTER_VALIDATE_IP))
                $ip = $_SERVER['HTTP_CLIENT_IP'];
        }
    }
    $purpose    = str_replace(array("name", "\n", "\t", " ", "-", "_"), NULL, strtolower(trim($purpose)));
    $support    = array("country", "countrycode", "state", "region", "city", "location", "address");
    $continents = array(
        "AF" => "Africa",
        "AN" => "Antarctica",
        "AS" => "Asia",
        "EU" => "Europe",
        "OC" => "Australia (Oceania)",
        "NA" => "North America",
        "SA" => "South America"
    );
    if (filter_var($ip, FILTER_VALIDATE_IP) && in_array($purpose, $support)) {
        $ipdat = @json_decode(file_get_contents("http://www.geoplugin.net/json.gp?ip=" . $ip));
        if (@strlen(trim($ipdat->geoplugin_countryCode)) == 2) {
            switch ($purpose) {
                case "location":
                    $output = array(
                        "city"           => @$ipdat->geoplugin_city,
                        "state"          => @$ipdat->geoplugin_regionName,
                        "country"        => @$ipdat->geoplugin_countryName,
                        "country_code"   => @$ipdat->geoplugin_countryCode,
                        "continent"      => @$continents[strtoupper($ipdat->geoplugin_continentCode)],
                        "continent_code" => @$ipdat->geoplugin_continentCode
                    );
                    break;
                case "address":
                    $address = array($ipdat->geoplugin_countryName);
                    if (@strlen($ipdat->geoplugin_regionName) >= 1)
                        $address[] = $ipdat->geoplugin_regionName;
                    if (@strlen($ipdat->geoplugin_city) >= 1)
                        $address[] = $ipdat->geoplugin_city;
                    $output = implode(", ", array_reverse($address));
                    break;
                case "city":
                    $output = @$ipdat->geoplugin_city;
                    break;
                case "state":
                    $output = @$ipdat->geoplugin_regionName;
                    break;
                case "region":
                    $output = @$ipdat->geoplugin_regionName;
                    break;
                case "country":
                    $output = @$ipdat->geoplugin_countryName;
                    break;
                case "countrycode":
                    $output = @$ipdat->geoplugin_countryCode;
                    break;
            }
        }
    }
    return $output;
}

?>

This is how we use it

echo ip_info("Visitor", "Country Code"); // IN

And thats it. There are some bugs in the module and we are going to add some new features. After all i will upload it here.

The problem when using this hook is that all not the modules use it, so you will find these "hidden" products in some places.

  • Sad 1
Link to comment
Share on other sites

  • Ali Samie changed the title to Prestashop module for hiding products in specific countries

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...