Skip to content

Commit

Permalink
0.1.6: #2
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitrii-fediuk committed May 11, 2018
1 parent 3b1ef37 commit d0b6a83
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 1 deletion.
67 changes: 67 additions & 0 deletions Plugin/Framework/App/FrontControllerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php
namespace Dfe\Frugue\Plugin\Framework\App;
use Df\Customer\Model\Session as DfSession;
use Magento\Customer\Model\Session;
use Magento\Framework\App\Area as A;
use Magento\Framework\App\FrontControllerInterface as Sb;
use Magento\Framework\App\Request\Http;
use Magento\Framework\App\RequestInterface as IRequest;
use Magento\Framework\HTTP\PhpEnvironment\Response;
use Magento\Store\Api\StoreResolverInterface as IStoreResolver;
// 2018-04-13
final class FrontControllerInterface {
/**
* 2018-04-13
* «Redirect customers to a proper store by checking his IP address»:
* https://github.com/mage2pro/frugue.com/issues/2
* @see \Magento\Framework\App\FrontController::dispatch()
* https://github.com/magento/magento2/blob/2.1.9/lib/internal/Magento/Framework/App/FrontController.php#L34-L74
* @param Sb $sb
* @param \Closure $f
* @param IRequest|Http $r
* @return Sb
*/
function aroundDispatch(Sb $sb, \Closure $f, IRequest $r) {
$res = null; /** @var Response|null $res */
if (df_area_code_is(A::AREA_FRONTEND)) {
$s = df_customer_session(); /** @var Session|DfSession $s */
if (!$s->getDfeFrugueRedirected()) {
$urlSwitch = 'stores/store/switch'; /** @const string $urlSwitch */
if (df_url_path_contains($urlSwitch)) {
$s->setDfeFrugueRedirected(true);
}
else {
if (!($c = $s->getDfeFrugueCountry())) /** @var string $c */ {
$s->setDfeFrugueCountry($c = 'HR'/*df_visitor()->iso2()*/);
}
/**
* «При первичном посещении клиента с IP адресом из Германии, Австрии, Швейцарии -
* направляем во frugue Германия.
*
* При первичном посещении клиента с IP адресом из Франции, Бельгии, Люксембурга -
* направляем во frugue Франция.
*
* При первичном посещении клиента с IP адресом из Великобритании, Ирландии,
* а также Italy, Latvia, Bulgaria, Lithuania, Croatia, Cyprus, Malta, Czech Republic,
* Netherlands, Poland, Estonia, Portugal, Finland, Romania, Slovakia, Slovenia, Greece,
* Spain, Hungary, Sweden - направляем во frugue Великобритания.
*
* При первичном посещении клиента с IP адресом из всех других стран -
* направляем его во frugue США.»
*/
$c = in_array($c, ['AT', 'CH', 'DE']) ? 'de' : (
in_array($c, ['BE', 'FR', 'LU']) ? 'fr' : (
df_eu($c) ? 'uk' : $c
)
);
if ($c !== df_store_code()) {
$res = df_o(Response::class);
$res->setRedirect(df_url_frontend($urlSwitch, [IStoreResolver::PARAM_NAME => $c]));
}
$s->setDfeFrugueRedirectStarted(!!$res);
}
}
}
return $res ?: $f($r);
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "mage2pro/frugue.com"
,"description": "A custom module for frugue.com"
,"type": "magento2-module"
,"version": "0.1.5"
,"version": "0.1.6"
,"license": "MIT"
,"authors": [{
"name": "Dmitry Fedyuk"
Expand Down
10 changes: 10 additions & 0 deletions etc/frontend/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@
type='Dfe\Frugue\Plugin\Catalog\Block\Product\View\Attributes'
/>
</type>
<!-- 2018-04-13
«Redirect customers to a proper store by checking his IP address»:
https://github.com/mage2pro/frugue.com/issues/2
-->
<type name='Magento\Framework\App\FrontControllerInterface'>
<plugin
name='Dfe\Frugue\Plugin\Framework\App\FrontControllerInterface'
type='Dfe\Frugue\Plugin\Framework\App\FrontControllerInterface'
/>
</type>
<!-- 2018-03-11 https://www.upwork.com/d/contracts/19688867 -->
<type name='Magento\Store\Block\Switcher'>
<plugin name='Dfe\Frugue\Plugin\Store\Block\Switcher' type='Dfe\Frugue\Plugin\Store\Block\Switcher'/>
Expand Down

0 comments on commit d0b6a83

Please sign in to comment.