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

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

  • Never try at live website, it will break your products links and images – Nadeem0035 Oct 18 '17 at 13:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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