I am migrating my Magento 1.9 store to Magento 2.1.
I am still quite new in Magento 2.1 and I can't seem to find some things.
Any guidelines on how to replace the textual store switcher with flags?
languagemagento2multistorestoresswitcher
I am migrating my Magento 1.9 store to Magento 2.1.
I am still quite new in Magento 2.1 and I can't seem to find some things.
Any guidelines on how to replace the textual store switcher with flags?
Best Answer
I was struggling with the same problem for some time and finally managed to solve it. Maybe you already solved it by now, but it can definitely be useful for others.
Summary
The needed code can be found at the end of this post.
/var/www/magento/app/design/frontend/{namespace}/{name}/Magento_Store/templates/switch
language.phtml
and add it to this folder/var/www/magento/app/code/{theme_dir}/Magento_Theme/view/frontend/layout/default_head_blocks.xml
.- If it doesn't exist, copy it from
/var/www/magento/vendor/magento/module-theme/view/frontend/layout/default_head_blocks.xml
<css src="css/languageSwitcher.css"/>
languageSwitcher.css
and add it to folder/var/www/magento/app/design/frontend/{namespace}/{name}/web/css
/var/www/magento/app/design/frontend/{namespace}/{name}/web/images/flags
and add flags with naming conventionflag_{country_ID}.png
e.g.flag_en.png
. All images need to have the same resolution.Extended explanation and extra information
First, you need to find, which template is responsible for the language switcher. Therefore, head to the admin-site of your Magento installation, go to
Store > Configuration > Advanced > Developer > Debug > Enabled Template Path Hints for Storefront > Yes
. Now reload the page of your store and you will see a lot of red boxes with names in it. The box which contains the drop-down language-switcher has a path like:/var/www/magento/vendor/magento/module-store/view/frontend/templates/switch/languages.phtml
.What you want to do is to create your own template and add the changes you want. Therefore, NEVER change the aforementioned template. It is not safe because every Magento update will overwrite the changes (and maybe something else will do as well....). The recommended way is:
Copy the template to the path:
/var/www/magento/app/design/frontend/{namespace}/{name}/{vendor_name}_{module_name}/templates/{path_to_template}/template.phtml
.{namespace} = the module's namespace (e.g. your company/domain/...)
{name} = name of the module
{vendor_name} = the name of the vendor, which provided the template
{module_name} = the module in which the template lies
{path_to_template} = the path to the right of
templates
In our case, this results to:
/var/www/magento/app/design/frontend/{namespace}/{name}/Magento_Store/templates/switch/languages.phtml
Now, this should already work, if you make some changes to the copy, they should appear on the site.
CSS
In my solution I also used a css file to style the template. Therefore we need to add the path to the
default_head_blocks.xml
. The default file lies inapp/code/Magento/Theme/view/frontend/layout/default_head_blocks.xml
To make changes to this file, copy it to your corresponding folder and add the path to the css file. Path:
/var/www/magento/app/code/{theme_dir}/{Magento_Theme}/view/frontend/layout
Therein you can also add extra *.js files and other sources.... add this line to the end of default_head_blocks.xml (but inside the
<head>
tags):<css src="css/languageSwitcher.css"/>
.Navigate to the folder
/var/www/magento/app/design/frontend/{namespace}/{name}/web/css
and add thelanguageSwitcher.css
.Next, head to
.../web/images
and create the folderflags
. Add images to the folder with the naming convention:flag_{country_ID}.png
. E.g. for English useflag_en.png
. They all need to have the same resolution.Code (languages.phtml & languageSwitcher.css)
languages.phtml
languageSwitcher.css