Magento supports multi-website feature and each website support multi-store view to display store-based eCommerce site to visitors.
If a website has multiple store views and wants to set different URLs for the product by store view level,
you can do it in Magento by just setting the URL based on the store view.
You can get the store view product URL by using given code snippets,
<?php declare(strict_types=1); namespace Rbj\ProductUrlByStoreView\Model; use Magento\Catalog\Api\ProductRepositoryInterface; use Magento\Framework\Exception\NoSuchEntityException; /** * Used to get product url by store view. */ class ProductUrlStoreView { /** * @var ProductRepositoryInterface */ private $productRepository; /** * ProductUrlStoreView constructor * @param ProductRepositoryInterface $productRepository */ public function __construct( ProductRepositoryInterface $productRepository ) { $this->productRepository = $productRepository; } /** * Get Product URL * @param int $productId * @return string|null * @throws NoSuchEntityException */ public function getProductUrlByStoreView(int $productId): ?string { $websiteId = 2; $productUrl = null; try { $product = $this->productRepository->getById($productId, false, $websiteId); $productUrl = $product->getProductUrl(); } catch (NoSuchEntityException $exception) { throw new $exception; } return $productUrl; } }
When you check the Product Repository Interface with getById() method,
public function getById($sku, $editMode = false, $storeId = null, $forceReload = false);
you will see the third argument will be the store Id.
store Id indicates the store view/website id to get a store-specific URL.