3

I have multiple stores on my Magento 2 installation. So I have store 1 base url as http://mystore1.com and store 2 base url as http://mystore2.com. How do i get the base url of store 2 dynamically on a phtml file in store 1.

Note: $block->getBaseUrl() will only return the current store url. Is there a function like getBaseUrl of store id = 2 ?

  • You tried $block->getBaseUlr() or $this->getBaseUrl() in your phtml? – Khoa TruongDinh Nov 10 '16 at 14:04
  • if i use $block->getBaseUrl() while im on store 1 I will get mystore1.com. But that's not what I want. I need to get the store 2 url which is mystore2.com while im on store 1. – jjordao Nov 10 '16 at 14:07
  • Try my answer below. – Khoa TruongDinh Nov 10 '16 at 17:19
3

In your custom block, the below code will get the store Base Url by using store id:

namespace Vendor\Module\Block;

class Store extends \Magento\Framework\View\Element\Template
{
    public function getStoreUrl($storeId)
    {
        return $this->_storeManager->getStore($storeId)->getBaseUrl();
    }
}

In our custom phtml, we can call $block->getStoreUrl($storeId);.

1

Try This

protected function generateForAllStores()
    {
        $urls = [];
        foreach($this->storeManager->getStores() as $store) {
            $urls[] = $this->setUrlRewrite($store->getStoreId());
        }

        return $urls;
    }
  • Hi @Deexit, what classes do i need to extend or construct using this codes? – jjordao Nov 12 '16 at 11:54
  • Yes @jjordao you can use This interface in construct code - - Magento\Store\Model\StoreManagerInterface – user44966 Nov 12 '16 at 12:13
  • Thanks @Deexit, your answer gives me more ideas to work on. Although Khoa's answer is more applicable in my situation right now. But great answer I will use this in the future. – jjordao Nov 15 '16 at 2:40
-1

In phtml file you can call base url with the help of store id :

$block->getStoreUrl($store_id);

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.