From fa16c12b673e48cf22d4317d6998a3f59e6e57cf Mon Sep 17 00:00:00 2001 From: Malith Date: Tue, 19 Feb 2019 10:48:34 +1100 Subject: [PATCH] EX-01 GeoIP bases content in PDP --- .../Mindarc/GeoIP/Block/Product/View/Info.php | 52 ++++++++ app/code/Mindarc/GeoIP/Model/GeoIP.php | 125 ++++++++++++++++++ app/code/Mindarc/GeoIP/README.md | 3 + app/code/Mindarc/GeoIP/Setup/InstallData.php | 76 +++++++++++ app/code/Mindarc/GeoIP/composer.json | 24 ++++ app/code/Mindarc/GeoIP/etc/module.xml | 14 ++ app/code/Mindarc/GeoIP/registration.php | 14 ++ .../frontend/layout/catalog_product_view.xml | 32 +++++ .../templates/product/view/info.phtml | 26 ++++ 9 files changed, 366 insertions(+) create mode 100755 app/code/Mindarc/GeoIP/Block/Product/View/Info.php create mode 100755 app/code/Mindarc/GeoIP/Model/GeoIP.php create mode 100644 app/code/Mindarc/GeoIP/README.md create mode 100755 app/code/Mindarc/GeoIP/Setup/InstallData.php create mode 100644 app/code/Mindarc/GeoIP/composer.json create mode 100644 app/code/Mindarc/GeoIP/etc/module.xml create mode 100644 app/code/Mindarc/GeoIP/registration.php create mode 100755 app/code/Mindarc/GeoIP/view/frontend/layout/catalog_product_view.xml create mode 100644 app/code/Mindarc/GeoIP/view/frontend/templates/product/view/info.phtml diff --git a/app/code/Mindarc/GeoIP/Block/Product/View/Info.php b/app/code/Mindarc/GeoIP/Block/Product/View/Info.php new file mode 100755 index 0000000..d38d87d --- /dev/null +++ b/app/code/Mindarc/GeoIP/Block/Product/View/Info.php @@ -0,0 +1,52 @@ + + * @copyright Copyright (c) 2019 Mindarc Pty Ltd. (https://www.mindarc.com.au/) + */ + +namespace Mindarc\GeoIP\Block\Product\View; + +/** + * Class Info + * @package Mindarc\GeoIP\Model + */ +class Info implements \Magento\Framework\View\Element\Block\ArgumentInterface +{ + /** + * @var \Mindarc\GeoIP\Model\GeoIP + */ + private $geoIP; + + /** + * Info constructor. + * @param \Mindarc\GeoIP\Model\GeoIP $geoIP + */ + public function __construct( + \Mindarc\GeoIP\Model\GeoIP $geoIP + ) { + $this->geoIP = $geoIP; + } + + /** + * is the accessing user from US + * + * @return bool + */ + public function isUsUser() + { + $countryCode = $this->geoIP->getCountryCode(); + return $countryCode == 'US'; + } + + /** + * @return string + */ + public function getCountryCode() + { + return $countryCode = $this->geoIP->getCountryCode(); + } +} diff --git a/app/code/Mindarc/GeoIP/Model/GeoIP.php b/app/code/Mindarc/GeoIP/Model/GeoIP.php new file mode 100755 index 0000000..da2fb0c --- /dev/null +++ b/app/code/Mindarc/GeoIP/Model/GeoIP.php @@ -0,0 +1,125 @@ + + * @copyright Copyright (c) 2019 Mindarc Pty Ltd. (https://www.mindarc.com.au/) + */ + +namespace Mindarc\GeoIP\Model; + +/** + * Class GeoIP + * @package Mindarc\GeoIP\Model + */ +class GeoIP +{ + /** + * ip check service + */ + const GEO_IP_SERVICE_URL = 'http://ip-api.com/json/'; + + /** + * curl connection timeout + */ + const CURL_CONNECTION_TIMEOUT = '15'; + + /** + * curl reponse timeout + */ + const CURL_RESPONSE_TIMEOUT = '15'; + + /** + * @var array + */ + private $countryData; + + /** + * get country code + * + * @return mixed|null + */ + public function getCountryCode() + { + if (empty($this->countryData)) { + $this->getUserCountry(); + } + if (!empty($this->countryData['countryCode'])) { + return $this->countryData['countryCode']; + } + return null; + } + + /** + * get and store country data + */ + private function getUserCountry() + { + $ip = $this->getIP(); + $this->countryData = $this->getCountryByIP($ip); + } + + /** + * get the IP address of the user + * + * @return string + */ + private function getIP() + { + $ip = ''; + if (isset($_SERVER['HTTP_CLIENT_IP'])) { + $ip = $_SERVER['HTTP_CLIENT_IP']; + } elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) { + $ip = $_SERVER['HTTP_X_FORWARDED_FOR']; + } elseif (isset($_SERVER['HTTP_X_FORWARDED'])) { + $ip = $_SERVER['HTTP_X_FORWARDED']; + } elseif (isset($_SERVER['HTTP_FORWARDED_FOR'])) { + $ip = $_SERVER['HTTP_FORWARDED_FOR']; + } elseif (isset($_SERVER['HTTP_FORWARDED'])) { + $ip = $_SERVER['HTTP_FORWARDED']; + } elseif (isset($_SERVER['REMOTE_ADDR'])) { + $ip = $_SERVER['REMOTE_ADDR']; + } + return $ip; + } + + /** + * retrieve country data for the ip + * + * @param $ip + * @return mixed|string + */ + private function getCountryByIP($ip) + { + $country = ''; + try { + $ch = curl_init(); + if (false === $ch) { + $error = __('failed to initialize service'); + } + + curl_setopt($ch, CURLOPT_URL, self::GEO_IP_SERVICE_URL . $ip); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); + curl_setopt($ch, CURLOPT_TIMEOUT, self::CURL_RESPONSE_TIMEOUT); + curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, self::CURL_CONNECTION_TIMEOUT); + + $content = curl_exec($ch); + if ($content === false) { + $error = __('Service not responding'); + } + + if ($content) { + $country = json_decode($content, true); + } else { + $error = __('Encode Error'); + } + curl_close($ch); + } catch (\Exception $e) { + $error = sprintf('Curl failed with error #%d: %s', $e->getCode(), $e->getMessage()); + } + + return $country; + } +} diff --git a/app/code/Mindarc/GeoIP/README.md b/app/code/Mindarc/GeoIP/README.md new file mode 100644 index 0000000..8c49734 --- /dev/null +++ b/app/code/Mindarc/GeoIP/README.md @@ -0,0 +1,3 @@ +

Mindarc_GeoIP Module

+ +This module will manage the content based on the IP and country where the users are accessing from. \ No newline at end of file diff --git a/app/code/Mindarc/GeoIP/Setup/InstallData.php b/app/code/Mindarc/GeoIP/Setup/InstallData.php new file mode 100755 index 0000000..19fe95b --- /dev/null +++ b/app/code/Mindarc/GeoIP/Setup/InstallData.php @@ -0,0 +1,76 @@ + + * @copyright Copyright (c) 2019 Mindarc Pty Ltd. (https://www.mindarc.com.au/) + */ + +namespace Mindarc\GeoIP\Setup; + +use Magento\Cms\Model\BlockFactory; +use Magento\Framework\Setup\InstallDataInterface; +use Magento\Framework\Setup\ModuleContextInterface; +use Magento\Framework\Setup\ModuleDataSetupInterface; + +/** + * Class InstallData + * @package Mindarc\GeoIP\Setup + */ +class InstallData implements InstallDataInterface +{ + /** + * @var BlockFactory + */ + private $blockFactory; + + /** + * InstallData constructor. + * @param BlockFactory $modelBlockFactory + */ + public function __construct( + BlockFactory $modelBlockFactory + ) { + $this->blockFactory = $modelBlockFactory; + } + + /** + * {@inheritdoc} + */ + public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context) + { + $setup->startSetup(); + + $cmsBlocks = [ + [ + 'title' => 'Product Information - US', + 'identifier' => 'product_information_us', + 'content' => '
Static block content for US users
', + + ], + [ + 'title' => 'Product Information - Global', + 'identifier' => 'product_information_global', + 'content' => '
Static block content for Global users
', + ], + + ]; + + foreach ($cmsBlocks as $data) { + $cmsBlock = $this->blockFactory->create(); + $cmsBlock->getResource()->load($cmsBlock, $data['identifier']); + if (!$cmsBlock->getData()) { + $cmsBlock->setData($data); + } else { + $cmsBlock->addData($data); + } + $cmsBlock->setStores([\Magento\Store\Model\Store::DEFAULT_STORE_ID]); + $cmsBlock->setIsActive(1); + $cmsBlock->save(); + } + + $setup->endSetup(); + } +} diff --git a/app/code/Mindarc/GeoIP/composer.json b/app/code/Mindarc/GeoIP/composer.json new file mode 100644 index 0000000..4cdd1f8 --- /dev/null +++ b/app/code/Mindarc/GeoIP/composer.json @@ -0,0 +1,24 @@ +{ + "name": "mindarc/module-geoip", + "description": "Mindarc Magento 2 module for IP restrictions", + "require": { + "php": "~5.5.0|~5.6.0|~7.0.0|~7.1.0", + "magento/module-config": "*", + "magento/framework": "*", + "geoip/geoip": "~1.16" + }, + "type": "magento2-module", + "version": "100.23.0", + "license": [ + "OSL-3.0", + "AFL-3.0" + ], + "autoload": { + "files": [ + "registration.php" + ], + "psr-4": { + "Mindarc\\GeoIP\\": "" + } + } +} diff --git a/app/code/Mindarc/GeoIP/etc/module.xml b/app/code/Mindarc/GeoIP/etc/module.xml new file mode 100644 index 0000000..d46481b --- /dev/null +++ b/app/code/Mindarc/GeoIP/etc/module.xml @@ -0,0 +1,14 @@ + + + + + \ No newline at end of file diff --git a/app/code/Mindarc/GeoIP/registration.php b/app/code/Mindarc/GeoIP/registration.php new file mode 100644 index 0000000..f0546e3 --- /dev/null +++ b/app/code/Mindarc/GeoIP/registration.php @@ -0,0 +1,14 @@ + + * @copyright Copyright (c) 2019 Mindarc Pty Ltd. (https://www.mindarc.com.au/) + */ +\Magento\Framework\Component\ComponentRegistrar::register( + \Magento\Framework\Component\ComponentRegistrar::MODULE, + 'Mindarc_GeoIP', + __DIR__ +); \ No newline at end of file diff --git a/app/code/Mindarc/GeoIP/view/frontend/layout/catalog_product_view.xml b/app/code/Mindarc/GeoIP/view/frontend/layout/catalog_product_view.xml new file mode 100755 index 0000000..d518c20 --- /dev/null +++ b/app/code/Mindarc/GeoIP/view/frontend/layout/catalog_product_view.xml @@ -0,0 +1,32 @@ + + + + + + + + Mindarc\GeoIP\Block\Product\View\Info + + + + product_information_us + + + + + product_information_global + + + + + + diff --git a/app/code/Mindarc/GeoIP/view/frontend/templates/product/view/info.phtml b/app/code/Mindarc/GeoIP/view/frontend/templates/product/view/info.phtml new file mode 100644 index 0000000..357e5dd --- /dev/null +++ b/app/code/Mindarc/GeoIP/view/frontend/templates/product/view/info.phtml @@ -0,0 +1,26 @@ + + * @copyright Copyright (c) 2019 Mindarc Pty Ltd. (https://www.mindarc.com.au/) + */ + +/** @var $viewModel \Mindarc\GeoIP\Block\Product\View\Info */ +$viewModel = $block->getData('viewModel'); +$countryCode = $viewModel->getCountryCode(); +if (!empty($countryCode)): +?> +
+ getCountryCode()) ?> + + isUsUser()): ?> + getChildHtml('product.geo.info.us') ?> + + getChildHtml('product.geo.info.global') ?> + + +
+ \ No newline at end of file