From 53f9df85646dd48329f42666d47dd8725169ba7d Mon Sep 17 00:00:00 2001 From: Abhijit Date: Tue, 28 Aug 2018 22:33:46 +1000 Subject: [PATCH 1/2] GeoIP Country Code On Product Page GeoIP Country Code On Product Page --- .../IpRestrict/Block/Product/CountryCode.php | 20 +++++++++++ .../MyCompany/IpRestrict/Model/Country.php | 34 +++++++++++++++++++ app/code/MyCompany/IpRestrict/etc/module.xml | 6 ++++ .../MyCompany/IpRestrict/registration.php | 7 ++++ .../frontend/layout/catalog_product_view.xml | 16 +++++++++ .../frontend/templates/product/country.phtml | 7 ++++ 6 files changed, 90 insertions(+) create mode 100644 app/code/MyCompany/IpRestrict/Block/Product/CountryCode.php create mode 100644 app/code/MyCompany/IpRestrict/Model/Country.php create mode 100644 app/code/MyCompany/IpRestrict/etc/module.xml create mode 100644 app/code/MyCompany/IpRestrict/registration.php create mode 100644 app/code/MyCompany/IpRestrict/view/frontend/layout/catalog_product_view.xml create mode 100644 app/code/MyCompany/IpRestrict/view/frontend/templates/product/country.phtml diff --git a/app/code/MyCompany/IpRestrict/Block/Product/CountryCode.php b/app/code/MyCompany/IpRestrict/Block/Product/CountryCode.php new file mode 100644 index 0000000..646b3cb --- /dev/null +++ b/app/code/MyCompany/IpRestrict/Block/Product/CountryCode.php @@ -0,0 +1,20 @@ +modelCountry = $country; + parent::__construct($context, $data); + } + public function getCountryCode() + { + return $this->modelCountry->getCountryCode();; + } +} \ No newline at end of file diff --git a/app/code/MyCompany/IpRestrict/Model/Country.php b/app/code/MyCompany/IpRestrict/Model/Country.php new file mode 100644 index 0000000..00f9a7b --- /dev/null +++ b/app/code/MyCompany/IpRestrict/Model/Country.php @@ -0,0 +1,34 @@ +get_client_ip(); + $ipdat = @json_decode(file_get_contents("http://www.geoplugin.net/json.gp?ip=" . $ip)); + + return $ipdat; + } + + protected function get_client_ip() { + $ipaddress = ''; + if (isset($_SERVER['HTTP_CLIENT_IP'])) + $ipaddress = $_SERVER['HTTP_CLIENT_IP']; + else if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])) + $ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR']; + else if(isset($_SERVER['HTTP_X_FORWARDED'])) + $ipaddress = $_SERVER['HTTP_X_FORWARDED']; + else if(isset($_SERVER['HTTP_FORWARDED_FOR'])) + $ipaddress = $_SERVER['HTTP_FORWARDED_FOR']; + else if(isset($_SERVER['HTTP_FORWARDED'])) + $ipaddress = $_SERVER['HTTP_FORWARDED']; + else if(isset($_SERVER['REMOTE_ADDR'])) + $ipaddress = $_SERVER['REMOTE_ADDR']; + else + $ipaddress = 'UNKNOWN'; + return $ipaddress; + } +} \ No newline at end of file diff --git a/app/code/MyCompany/IpRestrict/etc/module.xml b/app/code/MyCompany/IpRestrict/etc/module.xml new file mode 100644 index 0000000..3c1bb3f --- /dev/null +++ b/app/code/MyCompany/IpRestrict/etc/module.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/app/code/MyCompany/IpRestrict/registration.php b/app/code/MyCompany/IpRestrict/registration.php new file mode 100644 index 0000000..a971ba9 --- /dev/null +++ b/app/code/MyCompany/IpRestrict/registration.php @@ -0,0 +1,7 @@ + + + + + + + + + + + diff --git a/app/code/MyCompany/IpRestrict/view/frontend/templates/product/country.phtml b/app/code/MyCompany/IpRestrict/view/frontend/templates/product/country.phtml new file mode 100644 index 0000000..b2d1cca --- /dev/null +++ b/app/code/MyCompany/IpRestrict/view/frontend/templates/product/country.phtml @@ -0,0 +1,7 @@ +getCountryCode())) +{ + $countryName = $this->getCountryCode()->geoplugin_countryName; + $countryCode = $this->getCountryCode()->geoplugin_countryCode; + echo "Your Country: ".$countryName." (".$countryCode.")"; +} \ No newline at end of file From 20456941da0ce10b44b3754076ae90f1fca3cc87 Mon Sep 17 00:00:00 2001 From: Abhijit Date: Tue, 28 Aug 2018 22:49:04 +1000 Subject: [PATCH 2/2] Static Block Depending Upon Country Static Block Depending Upon Country --- .../view/frontend/layout/catalog_product_view.xml | 2 +- .../view/frontend/templates/product/country.phtml | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/app/code/MyCompany/IpRestrict/view/frontend/layout/catalog_product_view.xml b/app/code/MyCompany/IpRestrict/view/frontend/layout/catalog_product_view.xml index fced55d..1069222 100644 --- a/app/code/MyCompany/IpRestrict/view/frontend/layout/catalog_product_view.xml +++ b/app/code/MyCompany/IpRestrict/view/frontend/layout/catalog_product_view.xml @@ -9,7 +9,7 @@ - + diff --git a/app/code/MyCompany/IpRestrict/view/frontend/templates/product/country.phtml b/app/code/MyCompany/IpRestrict/view/frontend/templates/product/country.phtml index b2d1cca..e7fc84f 100644 --- a/app/code/MyCompany/IpRestrict/view/frontend/templates/product/country.phtml +++ b/app/code/MyCompany/IpRestrict/view/frontend/templates/product/country.phtml @@ -4,4 +4,12 @@ if(is_object($this->getCountryCode())) $countryName = $this->getCountryCode()->geoplugin_countryName; $countryCode = $this->getCountryCode()->geoplugin_countryCode; echo "Your Country: ".$countryName." (".$countryCode.")"; + if(isset($countryCode) && $countryCode!='') + { + if($countryCode=='US'): + echo $block->getLayout()->createBlock('Magento\Cms\Block\Block')->setBlockId('country_block_us')->toHtml(); + else: + echo $block->getLayout()->createBlock('Magento\Cms\Block\Block')->setBlockId('country_block_global')->toHtml(); + endif; + } } \ No newline at end of file