Skip to content
Allows country redirect action on the storefront for the first customer's session.
PHP
Branch: master
Clone or download

Latest commit

Fetching latest commit…
Cannot retrieve the latest commit at this time.

Files

Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
Api
Block/Adminhtml/Config/Form/Field
Model
Plugin
Test/Unit/Model
etc
LICENSE
README.md
composer.json
registration.php

README.md

Country Redirect Module for Magento 2

Latest Stable Version License: MIT Packagist Packagist

This extension for Magento 2 redirects the customers regarding their origin country.

Setup

Magento 2 Open Source or Commerce edition is required.

Composer installation

Run the following composer command:

composer require opengento/module-country-redirect

Setup the module

Run the following magento command:

bin/magento setup:upgrade

If you are in production mode, do not forget to recompile and redeploy the static resources.

Features

First Session Redirect

Redirect the visitor on its first session visit, depending on its country. This feature can be disabled by store view scope, so the first redirect is only enabled on your main domain for example.

This feature can have non desirable behavior, for example for robots crawling your website. That's why you can set a list of user agents to ignore and to not process the redirection in this case. Moreover, a list of controller actions to ignore is also configurable.

Now you can select which visitor's country resolver to use, the available resolvers are:

  • Default Session Country (the country already set in session, or the default for the current store view).
  • CloudFare GEO-IP Country (use visitor'scountry available in the HTTP header value). CloudFare Country GEO-IP must be enabled, see more.

Any third party developers can add its own country resolver. Please read the developer section for further details.

Country to store mapping

Define the country to store relation. This configuration will allows Magento to redirect the visitor to the correct store view depending of the visitor's country. Multiple countries can be assigned to a single store view.

Settings

The configuration for this module is available in Stores > Configuration > General > Country Redirect.

Documentation

How to add a country resolver

Create a new final class and implement the following interface: Opengento\CountryRedirect\Model\Country\ResolverInterface. The method public function getCountryCode(): string should returns the visitor's country code of origin. The country should be compliant to ISO 3166-1 alpha-2 format.

Register the new country resolver in the method factory, Vendor/Module/etc/frontend/di.xml:

<type name="Opengento\CountryRedirect\Model\Country\ResolverFactory">
    <arguments>
        <argument name="resolvers" xsi:type="array">
            <item name="customCountryResolver" xsi:type="string">Vendor\Module\Model\Country\Resolver\CustomCountryResolver</item>
        </argument>
    </arguments>
</type>

If you want the resolver ba available in settings, register it in the resolver list Vendor/Module/etc/adminhtml/di.xml:

<virtualType name="Opengento\CountryRedirect\Model\Config\Source\CountryResolver">
    <arguments>
        <argument name="options" xsi:type="array">
            <item name="customCountryResolver" xsi:type="array">
                <item name="label" xsi:type="string" translate="true">Custom Country Resolver</item>
                <item name="value" xsi:type="string">customCountryResolver</item>
            </item>
        </argument>
    </arguments>
</virtualType>

The country resolver is ready to use.

Legacy Country Store Redirect

Some of the Magento versions have a known issue and the store redirect does not work as intended. In that specific case you have to edit the redirect plugin pengento_CountryRedirect::dispatch in the file Vendor/Module/etc/frontend/di.xml:

<type name="Magento\Framework\App\FrontControllerInterface">
    <plugin name="Opengento_CountryRedirect::dispatch" type="Opengento\CountryRedirect\Plugin\LegacyAction" sortOrder="-1"/>
</type>

Support

Raise a new request to the issue tracker.

Authors

  • Opengento Community - Lead - Twitter Follow
  • Thomas Klein - Maintainer - GitHub followers
  • Contributors - Contributor - GitHub contributors

License

This project is licensed under the MIT License - see the LICENSE details.

That's all folks!

You can’t perform that action at this time.