4

I need to add website selector on my Magento 2 site header. I am using Magento 2.0.9. How can I implement this?

1 Answer 1

4

You can get website name and url using this snippet.

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

public function getWebsites() 
{
    $_websites = $this->_storeManager->getWebsites();
    $_websiteData = array();
    foreach($_websites as $website){
        foreach($website->getStores() as $store){
            $wedsiteId = $website->getId();
            $storeObj = $this->_storeManager->getStore($store);
            $name = $website->getName();
            $url = $storeObj->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_WEB);
            array_push($_websiteData, array('name' => $name,'url' => $url));
        }
    }

    return $_websiteData;
}
5
  • Thank you, Patel. I have tried this method, it worked fine. One change that I have made is, here you are fetching store name I changed it to $name = $website->getName(); .
    – chinchu
    Commented Nov 10, 2016 at 14:01
  • Ok, Sure. I need one more help. I have created a block to list this. Could you please suggest me a way to display it in the header. I want to know the XML format.
    – chinchu
    Commented Nov 11, 2016 at 5:33
  • 1
    I also want website switcher on my header. In which should I put this code? In switch/stores.phtml ?
    – user9839
    Commented Jan 19, 2017 at 12:51
  • Above code is for Block file. Commented Jan 19, 2017 at 13:17
  • 1
    ok, how can I make block file for Magento2? where can I find an instruction?
    – user9839
    Commented Jan 20, 2017 at 7:25

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.