Please override with below two files :
module-catalog-url-rewrite/Model/Product/AnchorUrlRewriteGenerator.php (Line 58)
/**
* Generate list based on categories
*
* @param int $storeId
* @param Product $product
* @param ObjectRegistry $productCategories
* @return UrlRewrite[]
*/
public function generate($storeId, Product $product, ObjectRegistry $productCategories)
{
$urls = [];
foreach ($productCategories->getList() as $category) {
$anchorCategoryIds = $category->getAnchorsAbove();
if ($anchorCategoryIds) {
foreach ($anchorCategoryIds as $anchorCategoryId) {
//Default: $anchorCategory = $this->categoryRepository->get($anchorCategoryId);
$anchorCategory = $this->categoryRepository->get($anchorCategoryId, $storeId);
$urls[] = $this->urlRewriteFactory->create()
->setEntityType(ProductUrlRewriteGenerator::ENTITY_TYPE)
->setEntityId($product->getId())
->setRequestPath(
$this->urlPathGenerator->getUrlPathWithSuffix(
$product,
$storeId,
$anchorCategory
)
)
->setTargetPath(
$this->urlPathGenerator->getCanonicalUrlPath(
$product,
$anchorCategory
)
)
->setStoreId($storeId)
->setMetadata(['category_id' => $anchorCategory->getId()]);
}
}
}
return $urls;
}
step2 : module-catalog-url-rewrite/Model/ProductUrlRewriteGenerator.php (Line 146)
/**
* Generate list of urls for global scope
*
* @param \Magento\Framework\Data\Collection $productCategories
*
* @return \Magento\UrlRewrite\Service\V1\Data\UrlRewrite[]
*/
protected function generateForGlobalScope($productCategories)
{
$urls = [];
$productId = $this->product->getEntityId();
foreach ($this->product->getStoreIds() as $id) {
if (!$this->isGlobalScope($id)
&& !$this->storeViewService->doesEntityHaveOverriddenUrlKeyForStore($id, $productId, Product::ENTITY)
) {
// Default: $urls = array_merge($urls, $this->generateForSpecificStoreView($id, $productCategories));
// before loading the category collection by looping it, clone it and set the correct store id,
// so we get the correct url_path & url_key for that specific store id
$storeSpecificProductCategories = clone $productCategories;
$storeSpecificProductCategories->setStoreId($id);
$urls = array_merge($urls, $this->generateForSpecificStoreView($id, $storeSpecificProductCategories));
}
}
return $urls;
}
URL key for specified store already exists.