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
/**<br>
 * Generate list based on categories<br>
 *<br>
 * @param int $storeId<br>
 * @param Product $product<br>
 * @param ObjectRegistry $productCategories<br>
 * @return UrlRewrite[]<br>
 */
<br>
 public function generate($storeId, Product $product, ObjectRegistry $productCategories)<br>
 {<br>
 $urls = [];<br>
 foreach ($productCategories->getList() as $category) {<br>
 $anchorCategoryIds = $category->getAnchorsAbove();<br>
 if ($anchorCategoryIds) {<br>
 foreach ($anchorCategoryIds as $anchorCategoryId) {<br>
 //Default: $anchorCategory = $this->categoryRepository->get($anchorCategoryId);<br>
 $anchorCategory = $this->categoryRepository->get($anchorCategoryId, $storeId);<br>
 $urls[] = $this->urlRewriteFactory->create()<br>
 ->setEntityType(ProductUrlRewriteGenerator::ENTITY_TYPE)<br>
 ->setEntityId($product->getId())<br>
 ->setRequestPath(<br>
 $this->urlPathGenerator->getUrlPathWithSuffix(<br>
 $product,<br>
 $storeId,<br>
 $anchorCategory<br>
 )<br>
 )<br>
 ->setTargetPath(<br>
 $this->urlPathGenerator->getCanonicalUrlPath(<br>
 $product,<br>
 $anchorCategory<br>
 )<br>
 )<br>
 ->setStoreId($storeId)<br>
 ->setMetadata(['category_id' => $anchorCategory->getId()]);<br>
 }<br>
 }<br>
 }</p>
<p>return $urls;<br>
 }

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
/**<br>
 * Generate list of urls for global scope<br>
 *<br>
 * @param \Magento\Framework\Data\Collection $productCategories<br>
 *<br>
 * @return \Magento\UrlRewrite\Service\V1\Data\UrlRewrite[]<br>
 */
<br>
 protected function generateForGlobalScope($productCategories)<br>
 {<br>
 $urls = [];<br>
 $productId = $this->product->getEntityId();<br>
 foreach ($this->product->getStoreIds() as $id) {<br>
 if (!$this->isGlobalScope($id)<br>
 && !$this->storeViewService->doesEntityHaveOverriddenUrlKeyForStore($id, $productId, Product::ENTITY)<br>
 ) {<br>
 // Default: $urls = array_merge($urls, $this->generateForSpecificStoreView($id, $productCategories));</p>
<p> // before loading the category collection by looping it, clone it and set the correct store id,<br>
 // so we get the correct url_path & url_key for that specific store id<br>
 $storeSpecificProductCategories = clone $productCategories;<br>
 $storeSpecificProductCategories->setStoreId($id);</p>
<p>$urls = array_merge($urls, $this->generateForSpecificStoreView($id, $storeSpecificProductCategories));<br>
 }<br>
 }<br>
 return $urls;<br>
 }

 

Hope this helps!

Related posts:

SimiCart PWA solution

Migrate to Magento 2

SEE MORE:

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

Dom

A knowledge craver who always strive to be wiser everyday.

    avatar
    4 Comment threads
    0 Thread replies
    0 Followers
     
    Most reacted comment
    Hottest comment thread
    4 Comment authors
    Ovidiu CaramidaPhillipSafideveloper Recent comment authors
      Subscribe  
    newest oldest most voted
    Notify of
    developer
    Guest

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

    Safi
    Guest
    Safi

    Tried with Magento 2.2.3 but did not work.

    Phillip
    Guest
    Phillip

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

    Ovidiu Caramida
    Guest
    Ovidiu Caramida

    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