How to pass store code to custom URL in magento 2

by Chirag Patel   Last Updated May 16, 2019 05:09 AM

I would like to add store code to a custom URL in Magento 2.3.0

Actually, I want to pass store code to a custom URL in my custom module based on relevant store order.

Let's take an example:

public function getCustomUrl($orderData) {        
    if($this->emailHelper->dynamicUrl()){
        return $this->urlInterface->getUrl($this->emailHelper->dynamicUrl()).'oId/'.$orderData->getIncrementId().'/fId/'.$this->getFollowUpId();
    }else {
        return $this->urlInterface->getUrl('route/controller/index/'.'oId/'.$orderData->getIncrementId().'/fId/'.$this->getFollowUpId());
    }
}

In the above code

  • $orderData = order object i can get storeId in this object
  • $this->emailHelper->dynamicUrl() = get custom string from system config
  • $orderData->getIncrementId() = pass orderId to url params
  • $this->getFollowUpId() = module custom table ID pass to url params.

Above code, the result is http://example.com/default/seo/oId/000000112/fId/179 where /default/ is store code. I would like to pass /store2Code/ instead of/default/where/store2Code/` is order placed on store 2.

So in short pass store code based on the placed order store.

Note : I have enabled pass store code to URL from system config. so the reason for this store code is showing in URL.

Any help would be appreciated! Thanks.



Answers 2


Try below code.

return $this->storeManager->getStore($orderData->getStoreId())->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_WEB).'oId/'.$orderData->getIncrementId().'/fId/'.$this->getFollowUpId();

It should work.

Yash Shah
Yash Shah
May 16, 2019 04:53 AM

I guess this can help you https://magento.stackexchange.com/a/84982/45214 .

In the above link, the BaseUrl is already obtained from storemanager and it will return the store's baseurl. If you have enabled the configuration from the backend to display store_code (for ex. en_US) along with the BaseUrl in the URL, then it will return the BaseUrl/store_code/ as Base Url.

Hope this will be helpful.

Kazim Noorani
Kazim Noorani
May 16, 2019 04:54 AM

Related Questions


Updated March 19, 2017 23:09 PM

Updated October 31, 2017 12:09 PM

Updated June 03, 2016 08:04 AM

Updated September 21, 2016 09:03 AM

Updated October 17, 2016 09:03 AM