2

Before I use one website and mutli language view: enter image description here

then this language.phtml switcher work and display in heder:

<?php
/**
 * Copyright © 2016 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */

// @codingStandardsIgnoreFile

?>
<?php
/**
 * Language switcher template
 */
?>
<?php if (count($block->getStores())>1): ?>
    <?php $id = $block->getIdModifier() ? '-' . $block->getIdModifier() : ''?>
    <div class="dropdown dropdown-switcher language switcher-language" id="switcher-language<?php /* @escapeNotVerified */ echo $id?>">
        <?php foreach ($block->getStores() as $_lang): ?>
            <?php if ($_lang->getId() == $block->getCurrentStoreId()): ?>
                <button class="switcher-toggle dropdown-toggle" type="button" data-toggle="dropdown">
                    <?php echo $block->escapeHtml($_lang->getName()) ?>
                    <i class="fa fa-angle-down" aria-hidden="true"></i>
                </button>
            <?php endif; ?>
        <?php endforeach; ?>
        <ul class="dropdown-menu">
            <?php foreach ($block->getStores() as $_lang): ?>
                <?php if ($_lang->getId() != $block->getCurrentStoreId()): ?>
                    <li class="view-<?php echo $block->escapeHtml($_lang->getCode()); ?> switcher-option">
                        <a href="#" data-post='<?php /* @escapeNotVerified */ echo $block->getTargetStorePostData($_lang); ?>'>
                            <img alt="<?php echo $_lang->getCode(); ?>" src="<?php /* @escapeNotVerified */ echo $block->getViewFileUrl('images/flags/' . $_lang->getCode() . '.png') ?>" /><?php echo $block->escapeHtml($_lang->getName()) ?>
                        </a>
                    </li>
                <?php endif; ?>
            <?php endforeach; ?>
        </ul>
    </div>
<?php endif; ?>

But now I changed and add mutli website for multi language (separate domains): enter image description here

and now the language switcher has stopped displaying. Does anyone know how I can change it?

@Update3 I found file default.xml:

I changed this block from:

<block class="Magento\Theme\Block\Html\Header" name="header" as="header" after="-">
                    <arguments>
                        <argument name="show_part" xsi:type="string">welcome</argument>
                    </arguments>
                    <block class="Magento\Store\Block\Switcher" name="store_language" as="store_language" template="switch/languages.phtml"/>
                    <block class="Magento\Directory\Block\Currency" name="currency" before="store_language" template="currency.phtml"/>
                    <block class="Magento\Framework\View\Element\Html\Links" name="top.links">
                        <arguments>
                                <argument name="css_class" xsi:type="string">links</argument>
                            </arguments>
                    </block>
                    <block class="Magento\Theme\Block\Html\Header\Logo" name="logo">
                        <arguments>
                            <argument name="logo_img_width" xsi:type="number">189</argument>
                            <argument name="logo_img_height" xsi:type="number">64</argument>
                        </arguments>
                    </block>

TO:

    <block class="Magento\Theme\Block\Html\Header" name="header" as="header" after="-">
        <arguments>
            <argument name="show_part" xsi:type="string">welcome</argument>
        </arguments>
        <block class="Your\Custom\Block\Class" name="store_language" as="store_language" template="Magento_Store::switch/languages.phtml"/>
        <block class="Magento\Directory\Block\Currency" name="currency" before="store_language" template="currency.phtml"/>
        <block class="Magento\Framework\View\Element\Html\Links" name="top.links">
            <arguments>
                    <argument name="css_class" xsi:type="string">links</argument>
                </arguments>
        </block>
        <block class="Magento\Theme\Block\Html\Header\Logo" name="logo">
            <arguments>
                <argument name="logo_img_width" xsi:type="number">189</argument>
                <argument name="logo_img_height" xsi:type="number">64</argument>
            </arguments>
        </block>

But still this issues on line 19:

<?php foreach ($websites as $website): ?>

2 Answers 2

3

Language switcher shows for multiple languages per website. It does not show if each website has only one language.

In this case, if you want to show switcher, you can show the website switcher.

Please try with the following code:

<?php $websites = $block->getAllWebsites();?>
<div class="switcher store switcher-store" id="switcher-store">
<strong class="label switcher-label"><span><?php echo __('Select Store') ?></span></strong>
<div class="actions dropdown options switcher-options">
    <?php foreach ($websites as $website): ?>
    <?php if ($websiteid == $website->getId()): ?>
        <div class="action toggle switcher-trigger"
             role="button"
             tabindex="0"
             data-mage-init='{"dropdown":{}}'
             data-toggle="dropdown"
             data-trigger-keypress-button="true"
             id="switcher-store-trigger">
            <strong>
                <span><?php echo $block->escapeHtml($website->getName()) ?></span>
            </strong>
        </div>
    <?php endif; ?>
    <?php endforeach; ?>
    <ul class="dropdown switcher-dropdown" data-target="dropdown">
        <?php foreach ($websites as $website): ?>
        <?php if (!($websiteid == $website->getId())): ?>
            <li class="switcher-option">
                <a href='<?php echo $website->getDefaultStore()->getCurrentUrl() ?>'>
                    <?php echo $block->escapeHtml($website->getName()) ?>
                </a>
            </li>
        <?php endif; ?>
        <?php endforeach; ?>
    </ul>
</div>

Update: As you are using the existing language.phtml file, you also need to create a custom block file and use it in the XML layout file for calling the PHTML file.

The code block file should be like below:

public function __construct(
        \Magento\Store\Model\StoreManagerInterface $storeManager,
) {
    $this->_storeManager = $storeManager;
}

public function getWebsites() 
{
    $_websites = $this->_storeManager->getWebsites();
    return $_websites;
}

Now while calling the PHTML file from the layout XML, use the new block class.

Update 2: Location of layout XML file should be under app/design/frontend/{current-theme-package}/{{current-theme}}/Magento_Theme/layout/default.xml

In this file you should see something like:

<block class="Magento\Store\Block\Switcher" name="store_language" as="store_language" template="Magento_Store::switch/languages.phtml"/>

If yes, you need to change it to:

<block class="Your\Custom\Block\Class" name="store_language" as="store_language" template="Magento_Store::switch/languages.phtml"/>
12
  • Mohit I updated my topic. Can you please check I change code correct?
    – Sylvester
    Commented Nov 30, 2018 at 13:02
  • You first need to put $websites = $block->getAllWebsites(); in <?php ?> tags. Commented Nov 30, 2018 at 13:06
  • Can you please check update2? 1 exception(s): Exception #0 (Exception): Warning: Invalid argument supplied for foreach() in
    – Sylvester
    Commented Nov 30, 2018 at 13:10
  • @Sylvester, updated my answer. Please check. Commented Nov 30, 2018 at 13:17
  • Ok Mohit thank you, but last question. How to name this block xml and where I should put this? (in which folder) ?
    – Sylvester
    Commented Nov 30, 2018 at 13:19
0

Because now you have one store view for a particular website.

language switcher when one website has multiple store view.

3
  • Yes Dear I know this : ) But how to set switcher for multiple website? That's why I ask
    – Sylvester
    Commented Nov 30, 2018 at 12:51
  • For website switcher? Commented Dec 1, 2018 at 5:07
  • Sweety, issues solved
    – Sylvester
    Commented Dec 1, 2018 at 7:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.