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
http://example.com/mystorecode/product1.html
Where as I get the result
http://example.com/product1.html
Best Answer
From the Magento2 root, you can run below code inside any custom php file.