I have multi stores enabled in my Magento instance. I am trying to get product url for each store as I need to index data in Elasticsearch.

What I have tried but not getting the result with store appended in url.

$productId = 2;
$storeId = 4;
$product = $this->productRepository->getById($productId, false, $storeId);
$product->setStoreId($storeId)->getUrlModel()->getUrlInStore($product, ['_escape' => true]);

I expect result as


Where as I get the result

  • Did you find any solution? I am also looking for the same query. – Patrick W Sep 24 '18 at 6:51
  • No. So I did a patchy solution which I have added as an answer. You can try it out. It worked out for me. – Priyank Sep 26 '18 at 11:04
  • Thanks for comment. I think below solution will work fine if you don't want to use object manager : $storeUrl = $this->storeManager->getStore($storeId)->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_WEB); $productUrl = $storeUrl .$productData->getUrlKey(); Here $productData should be filtered with store id to bring store vise data. – Patrick W Sep 26 '18 at 11:57

From the Magento2 root, you can run below code inside any custom php file.

require './app/bootstrap.php';
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
$_objectManager = $bootstrap->getObjectManager();
$state = $_objectManager->get('Magento\Framework\App\State');
$registry = $_objectManager->get('Magento\Framework\Registry');
$registry->register('isSecureArea', true);

$productId = 184294;
$storeId = 27;
$product = $_objectManager->create('Magento\Catalog\Api\ProductRepositoryInterface')->getById($productId, false, $storeId);
echo $product->setStoreId($storeId)->getUrlModel()->getUrlInStore($product, ['_escape' => true]);
  • How to get current product URL of current store in block? I am using public function getCurrentProduct() { return $this->_registry->registry('current_product'); } but it's give default URL. – Magecode Feb 14 '18 at 11:44

From Admin > Stores > Configuration > Web > Url Options > Set "Add Store Code to Urls" to "Yes".

  • This option is enabled. – Priyank Sep 1 '17 at 12:59
  • I have just checked in my local system and your code working fine for me. – Bhavin iFlair Sep 1 '17 at 13:10
  • What is your magento version ? Are you trying to load product Object In Backend ? – Priyank Sep 1 '17 at 13:10
  • My magetno version is 2.1.8 and yes i am run my code inside admin area in custom php file. – Bhavin iFlair Sep 1 '17 at 13:13
  • Sorry dude not working for me. If you are using some other code paste in your answer. – Priyank Sep 1 '17 at 13:14

They patchy solution for my working

$storeUrl = $this->storeManager->getStore($storeId)->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_WEB,$isSecure);
$productUrlKey = $product->getUrlKey();
$this->scopeConfig = $objectManager->create('\Magento\Framework\App\Config\ScopeConfigInterface');
$productSuffix = $this->scopeConfig->getValue('catalog/seo/product_url_suffix', \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
$productUrl = $storeUrl . $productUrlKey . $productSuffix;

Note: Not recommend to use object manager. This shows my lazyness :)

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.