0

M 2.3.1

We are importing via Firebear. When I import I am getting success.. the product imports but I get this error:

URL key for the specified store already exists.

Whenever I try and SAVE products...

We had previously deleted products from the table with "delete from catalog_product_entity;"

If I change the URL key in the feed.. the import works ok and I can save the product...

How can I "clean up" my database so I can import the original feed with the original URL key and have it allow me to save the product.. without "URL key for specified store already exists" error?

6
  • Roughly how many products? Aug 16, 2019 at 20:11
  • Original feed was a few thousand.... Now after deleting them all am just trying to import one product to test the import and its giving me the error
    – Matt Ayres
    Aug 16, 2019 at 20:14
  • If this is on a dev store may be worth truncating url rewrite table and letting magento index process regenerate url keys and then see what happens. I believe there have been a few issues resolved in journey up to version you are currently using so there could be an underlying issue github.com/magento/magento2/issues/7298 Aug 16, 2019 at 21:06
  • Delete the WHOLE table? Won't that break other things? Yer is dev site.....
    – Matt Ayres
    Aug 16, 2019 at 21:12
  • Try to truncate catalog_url_rewrite table. Aug 16, 2019 at 23:51

2 Answers 2

0

Yep removing the product url rewrites fixed it for me.

0
0

I have fixed this issue in more than 3-4 projects.

If you want to resolve this issue, you can upgrade your version with latest one or try below code logic:

Try this solution by overriding below function in your custom module :)

Path: vendor/magento/module-url-rewrite/Model/Storage

protected function doReplace(array $urls){ 
    $this->deleteOldUrls($urls); 
    $data = [];
    $storeId_requestPaths = [];
    foreach ($urls as $url) {
        $storeId = $url->getStoreId();
        $requestPath = $url->getRequestPath();
        $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]);
            }
        }
    }
    try {
        $this->insertMultiple($data);
    } catch (\Magento\Framework\Exception\AlreadyExistsException $e) {
        /** @var \Magento\UrlRewrite\Service\V1\Data\UrlRewrite[] $urlConflicted */
        $urlConflicted = [];
        foreach ($urls as $url) {
            $urlFound = $this->doFindOneByData(
                [
                    UrlRewriteData::REQUEST_PATH => $url->getRequestPath(),
                    UrlRewriteData::STORE_ID => $url->getStoreId(),
                ]
            );
            if (isset($urlFound[UrlRewriteData::URL_REWRITE_ID])) {
                $urlConflicted[$urlFound[UrlRewriteData::URL_REWRITE_ID]] = $url->toArray();
            }
        }
        if ($urlConflicted) {
            throw new \Magento\UrlRewrite\Model\Exception\UrlAlreadyExistsException(
                __('URL key for specified store already exists.'),
                $e,
                $e->getCode(),
                $urlConflicted
            );
        } else {
            throw $e->getPrevious() ?: $e;
        }
    }

    return $urls;
}

Happy Coding :)

Thanks,

Ketan T.

Your Answer

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

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