3

I working on a project on which I get the error

URL key for the specified store already exists

While editing category on store 2. I get this error only on store 2 Arabic first category and also in its subcategories.

I have surfed a lot. According to github, it is bug of Magento. However, I tried by truncating catalog_category_product_index and catalog_category_product. It works, but I cannot truncate it because of a large number of products in categories. Is there any way to resolve this error?

https://github.com/magento/magento2/issues/6671

https://github.com/magento/magento2/issues/7298

https://github.com/magento/magento2/issues/3268

enter image description here

15
  • change URL Key key of category where you get this error. this will fix your issue Commented Apr 27, 2017 at 12:44
  • by default magento generate url key based on name and due to same name it happens Commented Apr 27, 2017 at 12:46
  • I changed but it doesn't work. I am getting error only in second store. Thanks @MineshPatel Commented Apr 27, 2017 at 12:52
  • make sure your new key is not there for any category :) Commented Apr 27, 2017 at 12:55
  • I am sure, i tried random strings :) @MineshPatel Commented Apr 27, 2017 at 12:56

1 Answer 1

0

I have fixed this issue. And I have 2 solutions for that.

The first one: Clean up your database in table url_rewrite (Change the url_key of all category). You can write UpgradeData script for this solution.

The second one: Remove the duplication data when saving category. This data is throw in method doReplace($urls) in \vendor\magento\module-url-rewrite\Model\Storage\DbStorage.php file.

protected function doReplace($urls)
    {
        foreach ($this->createFilterDataBasedOnUrls($urls) as $type => $urlData) {
            $urlData[UrlRewrite::ENTITY_TYPE] = $type;
            $this->deleteByData($urlData);
        }
        $data = [];
        foreach ($urls as $url) {
            $data[] = $url->toArray();
        }
        $this->insertMultiple($data);
    } 

After debugging, I found out $data variable have duplicate record. If you want this method to work without any errors, rewrite this method above to:

protected function doReplace($urls)
{
    foreach ($this->createFilterDataBasedOnUrls($urls) as $type => $urlData) {
        $urlData[UrlRewrite::ENTITY_TYPE] = $type;
        $this->deleteByData($urlData);
    }
    $data = [];
    $storeId_requestPaths = [];
    foreach ($urls as $url) {
        $storeId = $url->getStoreId();
        $requestPath = $url->getRequestPath();
        // Skip if is exist in the database
        $sql = "SELECT * FROM url_rewrite where store_id = $storeId and request_path = '$requestPath'";
        $exists = $this->connection->fetchOne($sql);

        if ($exists) continue;

        $storeId_requestPaths[] = $storeId . '-' . $requestPath;
        $data[] = $url->toArray();
    }

    // Remove duplication data;
    $n = count($storeId_requestPaths);
    for ($i = 0; $i < $n - 1; $i++) {
        for ($j = $i + 1; $j < $n; $j++) {
            if ($storeId_requestPaths[$i] == $storeId_requestPaths[$j]) {
                unset($data[$j]);
            }
        }
    }
    $this->insertMultiple($data);
}

If you want to get more details, please read my comment in:
https://github.com/magento/magento2/issues/7298

Hope this will help you.

2
  • Never try at live website, it will break your products links and images
    – Nadeem0035
    Commented Oct 18, 2017 at 13:57
  • @ToanTam Facing issue in child category. child category not updated parent key there is showing old key of parent category. Let me know if any idea ? Commented Mar 16, 2021 at 4:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.