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) {        
        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.

  • Maybe you can try like this $this->storeManager->getStore()->getStoreUrl().'/'.$this->storeManager->getStore()->getCode().'oId/'.$orderData->getIncrementId().'/fId/'.$this->getFollowUpId(); – fmsthird May 16 '19 at 5:37
  • It is working fine when you use this in frontend but when you use for backend operation like cron or admin area it is not working. – Chirag Patel May 16 '19 at 5:52
  • haven't tried it before but maybe setting the current store first using setCurrentStore($storeId); will make it work? – fmsthird May 16 '19 at 5:55

Use store emulation (Magento\Store\Model\App\Emulation) to set store for backend operation and Try below code.

return $this->storeManagerInterface->getStore()->getBaseUrl().$this->emailHelper->dynamicUrl().'/oId/'.$orderData->getIncrementId().'/fId/'.$this->getFollowUpId();

It should work.

| improve this answer | |
  • 1
    Try this but it will return without store code like this http://example.com/oId/2000000058/fId/192 – Chirag Patel May 16 '19 at 5:24

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.

| improve this answer | |
  • I have already seen this reference url but it's not relevant what i want. – Chirag Patel May 16 '19 at 5:25
  • +1 for your efforts :) – Chirag Patel May 16 '19 at 5:26

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.