Magento – Get URL of Current Store View Store Magento 2

base-urlmagento2multistorestore-view

I have created multiple stores in one. Now when I change the store view it definitely changes and I have set the Base URLs for this store view accurately.
But when I try to get the current store view URL or Name I got the default store view name and URL.
I am trying to get using $storeManager->getStore()->getBaseUrl()
Any idea how I can get this ?

Best Answer

What you have to understand is the baseUrl is the same even you have multiple store views. the value that changes is for example : StoreId, StoreCode, storeName etc...

If you want to get the baseUrl, it will be the same in all stores :

$storeManager->getStore()->getBaseUrl() //domaine.com

if you want to get the currentUrl, it will be different in each store :

$storeManager->getStore()->getCurrentUrl() //domaine.com/?___store=default___from_store=pro
$storeManager->getStore()->getCurrentUrl($storeManager->getStore()->getId()) //domaine.com/?___store=default___from_store=2

If you want to set à specific store view, you append at the url ?___store=storeviewcode :

domaine.com/?___store=pro
Related Topic