Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions app/code/MyCompany/IpRestrict/Block/Product/CountryCode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
namespace MyCompany\IpRestrict\Block\Product;

class CountryCode extends \Magento\Framework\View\Element\Template
{
protected $modelCountry = null;

public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\MyCompany\IpRestrict\Model\Country $country,
array $data = []
) {
$this->modelCountry = $country;
parent::__construct($context, $data);
}
public function getCountryCode()
{
return $this->modelCountry->getCountryCode();;
}
}
34 changes: 34 additions & 0 deletions app/code/MyCompany/IpRestrict/Model/Country.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
namespace MyCompany\IpRestrict\Model;


class Country extends \Magento\Framework\Model\AbstractModel
{

public function getCountryCode()
{
$ip = $this->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;
}
}
6 changes: 6 additions & 0 deletions app/code/MyCompany/IpRestrict/etc/module.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="MyCompany_IpRestrict" setup_version="1.0.0">
</module>
</config>
7 changes: 7 additions & 0 deletions app/code/MyCompany/IpRestrict/registration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'MyCompany_IpRestrict',
__DIR__
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<page layout="1column" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="product.info.main">
<container name="product.info.country" label="Product info country container" htmlTag="div" htmlClass="product-info-country" before="-">
<block class="MyCompany\IpRestrict\Block\Product\CountryCode" name="iprestrict_country" cacheable="false" template="MyCompany_IpRestrict::product/country.phtml" before="product.info.price"/>
</container>
</referenceContainer>
</body>
</page>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
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;
}
}