Magento Tutorials

[SOLVED] Magento 2 Error “URL key for specified store already exists.”

magento-2-url-key-for-specified-store-already-exists

In Magento 2, there is a common bug that when you try to create a new category or update the name of an existing category, you get the error message “URL key for specified store already exists.” and the category can not be saved. This annoying problem happens when you have assigned product and category with the same url key.

To solve this problem permanently, you need to modify some of Magento core files. Before making these changes, remember to backup these files first.

module-catalog-url-rewrite/Model/Product/AnchorUrlRewriteGenerator.php (Line 58)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/**
 * 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;
 }

module-catalog-url-rewrite/Model/ProductUrlRewriteGenerator.php (Line 146)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/**
 * 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;
 }

 

Hope this helps!

[SOLVED] Magento 2 Error “URL key for specified store already exists.”
4 (80%) 4 votes
4 Comments

Dom

A knowledge craver who always strive to be wiser everyday.

    4 Comments

    1. developer
      03/22/2018 at 2:21 am

      Maaaaan!!! Whaat??? Modify “some core files Magento” ?? Nooo, never do that.

    2. Safi
      11/20/2018 at 5:12 pm

      Tried with Magento 2.2.3 but did not work.

    3. Phillip
      04/04/2019 at 6:47 am

      If everything is in SCM, I am wondering why core files can not be changed. There is history to be chased anyway.

    4. Ovidiu Caramida
      04/12/2019 at 12:11 am

      This extension can solve the duplicate URL issues by adding any attribute you want in the product URL structure and make it unique(for ex the SKU). It will then generate the URLs automatically on product save or import. You can also regenerate the URL rewrites from admin product listing.

      https://marketplace.magento.com/webpanda-catalog-url-rewrites.html

    Leave a Comment

    Your email address will not be published. Required fields are marked*