I need to add website selector on my Magento 2 site header. I am using Magento 2.0.9. How can I implement this?
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;
}
-
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 Nov 10 '16 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 Nov 11 '16 at 5:33
-
1I also want website switcher on my header. In which should I put this code? In switch/stores.phtml ? – user9839 Jan 19 '17 at 12:51
-
-
1ok, how can I make block file for Magento2? where can I find an instruction? – user9839 Jan 20 '17 at 7:25