4

How can we get the Website names dropdown (Website Switcher) in any phtml (Ex : header.phtml) in Magento 2

5

To get the list of all websites, you indeed need to use the storeManager, which is a native variable of every block extending \Magento\Framework\View\Element\Template:.

You can access this easily by creating a custom block inside any custom module. The block code goes here..

<?php
namespace Magento2\Custom\Block;
class WebsiteSwitcher extends \Magento\Framework\View\Element\Template{
    public function getWebsites() {
        return $this->_storeManager->getWebsites();
    }
    public function getCurrentWebsiteId()
    {
        return $this->_storeManager->getWebsite()->getId();
    }

}

After this call this inside your layout using a phtml file. Create a phtml file inside your theme folder. Path will be similar to

magento2\app\design\frontend\CompanyName\ThemeName\Magento_Store\templates\switch\websites.phtml

Put contents like this..

<?php
$websites = $block->getWebsites();
$websiteId = $block->getCurrentWebsiteId();
?>
<?php if (count($block->getWebsites()) > 1): ?>
<div class="switcher website switcher-website" id="switcher-website">
<strong class="label switcher-label"><span><?php echo __('Select Website') ?></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-website-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>
</div>
<?php endif; ?>

After adding the phtml file then call the block and phtml inside your layout. The path will be magento2\app\design\frontend\CompanyName\ThemeName\Magento_Theme\layout\default.xml

Add this inside <referenceContainer name="header.panel"> for getting the dropdown in global header.

<block class="Magento2\Custom\Block\Websiteswitcher" name="website_switcher" as="website_switcher"  template="Magento_Store::switch/websites.phtml"  before="store_language"/>

class name is nothing but your custom block's class name.

2

Magento 2 already provide functionality to Store Switcher. For that you need to look into this file.

Magento\Store\Block\Switcher

and it relavent template file is here

Magento\Store\view\frontend\templates\switch\stores.phtml
1

Also,you can use the websites name using Object Manager.

//Get Object Manager Instance
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$storeManager = $objectManager->create('Magento\Store\Model\StoreManagerInterface');
$websiteGroups = $storeManager->getWebsite()->getGroups();

foreach ($websiteGroups as $group) {
 $group->getName();
}

Note: direct use of Object Manager is not practice.

best Practice

Also better idea is that at your phtml's block class use Magento\Store\Model\Group; then add a function

use Magento\Store\Model\Group;

public function getWebsitesList(){
 return $websiteGroups = $this->_storeManager->getWebsite()->getGroups();
}

And call this function at phtml

$websiteGroups = $block->getWebsitesList();
foreach ($websiteGroups as $group) {
 $group->getName();
}
1

This is how I made it, just changed the code of Asheem Patro because there were some mistakes on it:

 <?php
 $websites = $block->getWebsites();
 $websiteid = $block->getCurrentWebsiteId();
 ?>
 <?php if (count($block->getWebsites())>1): ?>
<div class="switcher website switcher-website" id="switcher-website">
  <strong class="label switcher-label"><span><?php echo __('Select Website') ?></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-website-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>
</div>
<?php endif; ?>
0

You can get website collection website related data using programmatical way by Magento\Store\Model\ResourceModel\Website\CollectionFactory Website collection,

Refer blog, Get Website Name and other information programatically

public function __construct(
        \Magento\Store\Model\ResourceModel\Website\CollectionFactory $websiteCollectionFactory
    ) {
        $this->_websiteCollectionFactory = $websiteCollectionFactory
    }

     /**
     * Retrieve websites collection of system
     *
     * @return Website Collection
     */
    public function getWebsiteCollection()
    {
        $collection = $this->_websiteCollectionFactory->create();
        return $collection;
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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